What are Redis streams and how do they compare to pub/sub?
Learn how Redis Streams differ from Pub/Sub — persistence, consumer groups, acknowledgement, and replay — so you pick the right messaging tool with confidence.
Expected Interview Answer
Redis Streams are an append-only log data structure that persists messages, assigns each entry a unique ID, and lets consumers replay history and acknowledge processing, whereas Pub/Sub is fire-and-forget: it delivers a message only to clients connected at that moment and keeps nothing.
With Streams you use XADD to append and XREAD/XRANGE to read from any point in time, so a consumer that reconnects can resume where it left off. Consumer groups (XREADGROUP, XACK) distribute entries across workers with per-message acknowledgement and a pending-entries list for retries, giving at-least-once delivery. Pub/Sub (PUBLISH/SUBSCRIBE) has no storage, no acknowledgement, and no replay — an offline subscriber simply misses everything published while it was away.
- Streams persist messages so consumers can replay and resume
- Consumer groups enable load-balanced, acknowledged processing
- Pending-entries list allows retry of unacknowledged work
- Pub/Sub is simpler and lower-latency for ephemeral broadcasts
- Streams cap memory growth with MAXLEN/MINID trimming
AI Mentor Explanation
Pub/Sub is like a live stadium announcement over the loudspeaker: only fans in their seats right then hear it, and once the words fade they are gone forever. A Redis Stream is the official scorebook where every ball is written down with a number, so a fan who arrives late can flip back and read exactly what happened, over by over, and mark which entries they have already reviewed.
Step-by-Step Explanation
Step 1
Append with XADD
Add an entry to a stream key; Redis returns a monotonically increasing ID like 1700000000000-0.
Step 2
Read history with XRANGE
Fetch entries by ID range for replay or auditing, since the log is durable in memory.
Step 3
Create a consumer group
Use XGROUP CREATE so multiple workers can share the stream with independent progress tracking.
Step 4
Consume with XREADGROUP
Each worker receives a distinct subset of new entries, forming its pending-entries list until acknowledged.
Step 5
Acknowledge with XACK
Confirm successful processing; unacked entries can be reclaimed with XCLAIM/XAUTOCLAIM for retries.
Step 6
Trim with MAXLEN
Cap the stream length so persisted history does not grow unbounded in memory.
What Interviewer Expects
- Clear grasp that Streams persist and Pub/Sub does not
- Knowledge of consumer groups and acknowledgement
- Understanding of at-least-once vs fire-and-forget delivery
- Awareness of the pending-entries list for retries
- When to pick each: durable work queue vs ephemeral broadcast
Common Mistakes
- Claiming Pub/Sub stores messages for offline subscribers
- Confusing consumer groups with simple XREAD
- Forgetting to XACK, causing the pending list to grow forever
- Not trimming streams, leading to unbounded memory use
- Assuming Streams guarantee exactly-once instead of at-least-once
Best Answer (HR Friendly)
“Redis Pub/Sub is like a live announcement — only people listening right now hear it and it is not saved. Redis Streams are like a recorded logbook that keeps every message, so workers can catch up on what they missed, share the work, and confirm each item is done.”
Code Example
# Append entries to a durable stream
XADD orders * item "book" qty 2
XADD orders * item "pen" qty 5
# Create a consumer group starting from the beginning
XGROUP CREATE orders workers 0
# Worker reads its share of new entries
XREADGROUP GROUP workers worker-1 COUNT 10 STREAMS orders >
# Acknowledge after successful processing
XACK orders workers 1700000000000-0# Subscriber must be connected NOW to receive anything
SUBSCRIBE orders-channel
# Publisher broadcasts; offline subscribers miss it
PUBLISH orders-channel "new order: book x2"Follow-up Questions
- How does the pending-entries list support message retries?
- What is the difference between XREAD and XREADGROUP?
- How would you cap a stream's memory footprint?
- When would you still prefer Pub/Sub over Streams?
- How does XAUTOCLAIM help recover from a dead consumer?
MCQ Practice
1. What happens to a Pub/Sub message if no subscriber is connected?
Pub/Sub is fire-and-forget; with no connected subscriber the message is simply dropped and never stored.
2. Which command acknowledges a processed entry in a Redis Stream consumer group?
XACK removes an entry from the consumer's pending-entries list, marking it successfully processed.
3. What delivery guarantee do Redis Stream consumer groups provide?
Unacknowledged entries stay pending and can be reclaimed and reprocessed, giving at-least-once delivery.
Flash Cards
Does Pub/Sub persist messages? — No. It delivers only to currently connected subscribers and stores nothing.
What structure underlies a Redis Stream? — An append-only log where each entry gets a unique, increasing ID.
What is the pending-entries list? — Per-consumer set of delivered but not-yet-acknowledged entries, used for retries.
How do you cap stream memory? — Trim with MAXLEN or MINID on XADD to bound retained history.
Which command load-balances a stream across workers? — XREADGROUP with a consumer group created via XGROUP CREATE.
Continue Learning
Related Interview Questions
How does pub/sub messaging work in Redis?
medium
Can you build an event pipeline on Redis keyspace notifications, and what are their delivery guarantees?
hard
A stream consumer crashes mid-processing — how do its pending messages get reclaimed and eventually retired?
hard
How Would You Design a Real-Time Chat App?
medium