Learn to Code
All tracks
Learn to Code

Free lessons from scratch to pro — every language, databases and DevOps.

AboutContactPrivacy PolicyTerms of Use

Your progress is saved in this browser. © 2026 Learn to Code

RD
Redis
0/10 completed
Beginner
What is Redis?Strings, numbers and keysExpiry and TTL
Intermediate
Lists and hashesSets and sorted setsCaching patterns
Advanced
Rate limiters, locks and transactionsPub/Sub and Streams
Pro
Memory, eviction and persistenceReplication, Sentinel and Cluster
Tracks / Redis / Advanced

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:

redis-cli (simulator) · editable
  • PUBLISH returns 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:

redis-cli (simulator) · editable

Pattern subscriptions

PSUBSCRIBE subscribes to every channel matching a glob pattern:

redis-cli (simulator) · editable

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.

redis-cli (simulator) · editable

* 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:

redis-cli (simulator) · editable

(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:

redis-cli (simulator) · editable

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.

redis-cli (simulator) · editable

XPENDING shows 2 pending entries, one per worker. Now worker1 finishes and acknowledges its email; worker2 "crashes" without acknowledging:

redis-cli (simulator) · editable

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

  1. Subscribe two clients to alerts, and a third to the pattern alerts:*. Publish to alerts and alerts:disk, and predict the receiver counts first.
  2. Create a stream tasks with 4 entries, a consumer group workers, and have two workers read 2 entries each. Acknowledge all of them and confirm XPENDING shows 0.
redis-cli (simulator) · editable
✎ Check your understanding

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?

← Rate limiters, locks and transactions