Pub/Sub and Streams
Redis can also move messages between parts of a system, in real time. It has two very different tools:
- Pub/Sub: broadcast a message to everyone listening right now. Fire and forget.
- Streams: an append-only log of messages that are stored, can be read later, replayed, and processed by groups of workers with acknowledgements.
The editors here use multiple clients: start a line with [name] to send it from that connection.
Pub/Sub
Subscribers listen on channels; publishers send messages to a channel; Redis delivers each message to every current subscriber:
PUBLISHreturns how many subscribers received the message.- Messages to a channel with no subscribers simply vanish.
Once subscribed, a connection is in subscribe mode: it can only (un)subscribe and PING. That's why apps use a separate connection for subscribing:
Pattern subscriptions
PSUBSCRIBE subscribes to every channel matching a glob pattern:
What Pub/Sub is good for, and its limits
✅ Live notifications, chat, "invalidate your local cache" broadcasts between app servers, live dashboards, WebSocket fan-out (e.g. Socket.IO's Redis adapter).
❌ No storage. If a subscriber is offline or disconnects, the message is gone forever. There's no acknowledgement and no replay. When messages must not be lost (orders, payments, emails), use Streams.
Streams
A stream is an append-only log. Each entry has an ID (a millisecond timestamp plus a sequence number) and some field-value pairs.
* asks Redis to generate the ID. - and + mean the smallest and largest possible IDs. Entries stay in the stream after being read: any number of readers can read the same history.
Reading new entries
XREAD returns entries after a given ID, so a reader remembers the last ID it saw:
(Here IDs were given explicitly to keep the example readable; normally you use *.) In real apps, XREAD BLOCK 5000 STREAMS events $ waits for new entries, much like BLPOP.
Capping a stream
Streams grow forever unless trimmed. Cap them when you add:
In production, write MAXLEN ~ 100000: the ~ lets Redis trim approximately, which is much more efficient.
Consumer groups: work queues with acknowledgements
A consumer group splits a stream's entries between several workers. Each entry goes to one consumer in the group, and stays pending until that consumer acknowledges it with XACK. If a worker crashes, its pending entries can be claimed by another worker, so nothing is lost.
XPENDING shows 2 pending entries, one per worker. Now worker1 finishes and acknowledges its email; worker2 "crashes" without acknowledging:
Entry 2-0 now belongs to worker1, which processes and acknowledges it. In production, XAUTOCLAIM automates this recovery.
Choosing a messaging tool
| Pub/Sub | Lists (BLPOP) |
Streams | |
|---|---|---|---|
| Messages stored | ❌ | ✅ until popped | ✅ until trimmed |
| Several independent readers of the same messages | ✅ (only live) | ❌ | ✅ |
| Work shared between workers | ❌ | ✅ | ✅ consumer groups |
| Acknowledgement and recovery | ❌ | Manual (LMOVE) |
✅ built in |
| Replay history | ❌ | ❌ | ✅ |
For very high-volume event pipelines across many services (with long retention and partitioning), dedicated systems like Apache Kafka are common. Redis Streams cover a huge range of needs with far less to operate.
Practice
- Subscribe two clients to
alerts, and a third to the patternalerts:*. Publish toalertsandalerts:disk, and predict the receiver counts first. - Create a stream
taskswith 4 entries, a consumer groupworkers, and have two workers read 2 entries each. Acknowledge all of them and confirmXPENDINGshows 0.
1.A subscriber was offline when a message was published. What happens with Pub/Sub?
2.What does > mean in XREADGROUP GROUP g c STREAMS mystream >?
3.Why use Streams with consumer groups instead of Pub/Sub for order processing?