Module F-8·12 min read

SUBSCRIBE, PUBLISH, PSUBSCRIBE for pattern channels, the no-persistence guarantee, keyspace notifications, horizontal scaling across app servers, and when to use Streams instead of Pub/Sub.

Written by Jatin Jain Saraf · Architect of SupraScan

F-8 — Pub/Sub and the Message Fanout Model

Who this module is for: You have heard of Redis Pub/Sub and may have used it for real-time notifications or chat. But you have not understood its most important limitation — messages are not persisted and any subscriber that is offline loses them permanently. This module covers the full Pub/Sub model, pattern-based subscriptions, its correct use cases, and when to use Streams instead.


What Pub/Sub Is

Redis Pub/Sub is a message fanout system. Publishers send messages to named channels. Subscribers listening on those channels receive every message in real time. Redis acts as the broker — it receives the message from the publisher and immediately delivers it to all connected subscribers.

Publisher → PUBLISH notifications:user:1001 "new message"
                  ↓
Redis broker
                  ↓
Subscriber A ← receives "new message"
Subscriber B ← receives "new message"
Subscriber C ← receives "new message"

The key characteristic: delivery is best-effort and immediate. If no subscriber is connected when a message is published, the message is gone. If a subscriber disconnects and reconnects, it misses all messages published during the disconnection window.


Commands

Subscribing

SUBSCRIBE channel [channel ...]

Once a client issues SUBSCRIBE, it enters a special "subscriber mode." In this mode, the only commands it can issue are SUBSCRIBE, UNSUBSCRIBE, PSUBSCRIBE, PUNSUBSCRIBE, PING, and RESET. The connection is dedicated to receiving messages.

127.0.0.1:6379> SUBSCRIBE notifications:global
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "notifications:global"
3) (integer) 1          ← number of channels now subscribed to

Publishing

PUBLISH channel message

Returns the number of subscribers that received the message (0 if nobody is subscribed).

127.0.0.1:6379> PUBLISH notifications:global "server maintenance at 02:00 UTC"
(integer) 3   ← 3 subscribers received it

Unsubscribing

UNSUBSCRIBE [channel ...] → unsubscribe from channels; no args = unsubscribe from all

Pattern Subscriptions

PSUBSCRIBE subscribes to channels matching a glob pattern:

PSUBSCRIBE pattern [pattern ...]
PUNSUBSCRIBE [pattern ...]
# Subscribe to all user-specific notification channels
PSUBSCRIBE notifications:user:*
 
# This will receive messages published to:
# notifications:user:1001
# notifications:user:1002
# notifications:user:99999
# ... any channel matching the pattern

Pattern messages include the matched channel name in the received message:

1) "pmessage"
2) "notifications:user:*"   ← the pattern that matched
3) "notifications:user:1001"   ← the actual channel
4) "your order has shipped"    ← the message body

Inspecting Active Pub/Sub State

PUBSUB CHANNELS [pattern]      → list all active channels (with at least 1 subscriber)
PUBSUB NUMSUB [channel ...]    → subscriber count per channel
PUBSUB NUMPAT                  → number of active pattern subscriptions
PUBSUB SHARDCHANNELS [pattern] → list active shard channels (for Redis Cluster, 7.0+)
PUBSUB SHARDNUMSUB [channel]   → subscriber count for shard channels
127.0.0.1:6379> PUBSUB CHANNELS
1) "notifications:global"
2) "events:trade"
 
127.0.0.1:6379> PUBSUB NUMSUB notifications:global events:trade
1) "notifications:global"
2) (integer) 3
3) "events:trade"
4) (integer) 1

Message Delivery Guarantees (and the Lack Thereof)

This is the most important thing to understand about Redis Pub/Sub:

There are no delivery guarantees.

  • If a subscriber is offline when a message is published → message lost
  • If a subscriber's connection drops mid-delivery → message lost (TCP retransmission handles byte-level loss, but Redis does not retry at the application level)
  • If the Redis server goes down → all pending messages lost
  • Messages are not stored anywhere — there is no way to replay past messages
  • A slow subscriber receiving messages faster than it can process them will have messages buffered in Redis's output buffer. If the buffer exceeds client-output-buffer-limit, Redis disconnects the client and the remaining buffered messages are lost.

PUBLISH returns immediately regardless of whether subscribers received the message.

This is fundamentally different from a message queue (BullMQ, RabbitMQ, Kafka). A queue persists messages until a consumer acknowledges them. Pub/Sub does not.


Building a Real-Time Notification System

Sign in to keep reading

The rest of this module is free — sign in with Google to unlock it and track your progress.

Sign in & Register

See it in production

Discussion

0

Join the discussion

Loading comments...

© 2026 Jatin Jain Saraf (JJS). All rights reserved.