Redis Pub/Sub
Redis Pub/Sub delivers real-time messages to subscribers. Learn how to publish, subscribe, and choose between Pub/Sub and Streams.
Redis Pub/Sub is a messaging system built into Redis. Publishers send messages to named channels. Any client subscribed to that channel receives the message immediately. There is no queue, no persistence, and no delivery confirmation.
How it works
When a client subscribes to a channel, Redis registers that connection as a listener. When another client publishes a message to the same channel, Redis pushes it to every active subscriber in real time.
Messages are not stored. If no subscriber is connected when a message is published, the message is gone. Subscribers that connect later, or that reconnect after a disconnect, will not see messages they missed. This is the core trade-off of Pub/Sub: low latency in exchange for no durability.
Basic commands
Open a subscriber in one terminal:
The three-part reply confirms the subscription. The fields are: the message type, the channel name, and the number of channels currently subscribed.
Once subscribed, the connection enters a blocking listen mode. It cannot issue other commands. In a separate terminal, publish a message:
The return value is the number of subscribers that received the message. Back in the subscriber terminal:
Again, three parts: the message type (message), the channel name, and the message body.
To unsubscribe from a specific channel:
Calling UNSUBSCRIBE with no arguments removes all subscriptions on that connection.
Pattern subscriptions
PSUBSCRIBE subscribes to all channels matching a glob pattern. This is useful when channels follow a naming convention:
This will match notifications.email, notifications.sms, notifications.push, and so on. When a message arrives, the reply includes four parts instead of three:
The extra field is the actual channel name that matched the pattern. To remove the pattern subscription:
Listing subscriptions
The PUBSUB command family lets you inspect active subscriptions on the server.
List all active channels (channels with at least one subscriber):
You can pass a pattern to filter results:
Check how many subscribers a channel has:
Count the number of active pattern subscriptions:
Using Pub/Sub in Python
The redis-py library provides a PubSub object that manages a dedicated connection for subscriptions.
Subscriber:
The connection used by a PubSub object is dedicated to subscriptions. You cannot issue regular Redis commands on it while it is subscribed. Use a separate redis.Redis() instance for other commands.
pubsub.listen() is a blocking generator. Running it on a background thread keeps your main program free. For pattern subscriptions, replace subscribe() with psubscribe().
Publisher:
Publishing uses an ordinary connection and returns the subscriber count.
Using Pub/Sub in Node.js
With ioredis, create a separate client instance for subscriptions:
The subscriber client is dedicated to subscriptions once subscribe() is called. All other commands go through publisher (or any other client instance).
With node-redis, the pattern is similar: call client.subscribe(channel, handler) on a client that was created with client.duplicate() so it gets its own connection.
The fire-and-forget limitation
Pub/Sub is a broadcast mechanism, not a message delivery system. There is no acknowledgment, no retry, and no persistence. A message published when zero subscribers are connected produces no error. The message is discarded.
Subscribers that disconnect and reconnect will not receive messages published during the gap. If your application cannot tolerate message loss, Pub/Sub is the wrong tool.
Pub/Sub vs Redis Streams
Redis Streams (XADD, XREAD) were added to address the limitations of Pub/Sub. The right choice depends on whether you need delivery guarantees.
| Pub/Sub | Redis Streams | |
|---|---|---|
| Message persistence | No | Yes |
| Missed messages on reconnect | Lost | Replayable |
| Consumer groups | No | Yes |
| Acknowledgment | No | Yes (with consumer groups) |
| Pattern matching | Yes (PSUBSCRIBE) | No (use key naming) |
| Latency | Lower | Slightly higher |
Use Pub/Sub when you are broadcasting real-time events where missing a message is acceptable: live dashboards, chat presence indicators, cache invalidation signals, or pushing config changes to a fleet of workers.
Use Streams when you need reliable delivery: event sourcing, job queues, audit logs, or any case where a consumer must process every message exactly once.
Pub/Sub vs message queues
Pub/Sub broadcasts to all active subscribers at once. It is not a work queue. If three workers are subscribed to a channel, all three receive every message. There is no built-in way to distribute work across them.
If you need task distribution, at-least-once delivery, dead letter queues, or integration with other systems, a dedicated message broker (RabbitMQ, Amazon SQS, Google Pub/Sub) is a better fit. Redis Streams with consumer groups can also fill this role if you are already running Redis and the feature set is sufficient.
Quick reference
| Command | Description |
|---|---|
SUBSCRIBE channel [channel ...] | Subscribe to one or more channels |
UNSUBSCRIBE [channel ...] | Unsubscribe from channels (all if omitted) |
PUBLISH channel message | Publish a message to a channel |
PSUBSCRIBE pattern [pattern ...] | Subscribe using a glob pattern |
PUNSUBSCRIBE [pattern ...] | Unsubscribe from patterns |
PUBSUB CHANNELS [pattern] | List active channels |
PUBSUB NUMSUB [channel ...] | Subscriber count per channel |
PUBSUB NUMPAT | Number of active pattern subscriptions |