Quick revision
The short answer
Redis pub/sub is fire-and-forget: PUBLISH delivers a message only to subscribers connected at that moment, so an offline subscriber simply never sees it. Redis Streams store messages in a persistent append-only log, so a consumer that comes back can continue from the last id it read. At-least-once processing comes from consumer groups: XGROUP CREATE, XREADGROUP with the > id, XACK after processing, with XPENDING and XAUTOCLAIM to recover messages from consumers that died.
What to remember
In this answer, we saw that pub/sub only reaches subscribers connected at that moment, which suits cache invalidation and live signals, while Streams keep an append-only log that consumers can resume from an id. Consumer groups add server-side tracking: XREADGROUP with > hands out new entries, XACK confirms them, the PEL and XPENDING show what is unconfirmed, and XAUTOCLAIM moves stuck entries to a live consumer. We also saw why processing must be idempotent and why every stream needs a MAXLEN or MINID cap.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Redis pub/sub is fire-and-forget: PUBLISH delivers a message only to subscribers connected at that moment, so an offline subscriber simply never sees it. Redis Streams store messages in a persistent append-only log, so a consumer that comes back can continue from the last id it read. At-least-once processing comes from consumer groups: XGROUP CREATE, XREADGROUP with the > id, XACK after processing, with XPENDING and XAUTOCLAIM to recover messages from consumers that died.
Why this question is asked
Many Spring services already have Redis, so it is tempting to use it for messaging as well. Redis offers two very different features for that, and choosing the wrong one is a classic cause of lost events: a team builds order notifications on pub/sub, a pod restarts during a deploy, and every message sent in those seconds is gone. Interviewers ask this to check that we know what each feature promises when things go wrong. In this answer, we will look at how pub/sub delivers messages, how Streams store them, how consumer groups give at-least-once processing, how to recover messages from dead consumers, and how to keep a stream from eating all the memory.
1How Pub/Sub Delivers a Message
In pub/sub, a client subscribes to a channel, a name such as cache-invalidation, and keeps its connection open. Another client publishes to that channel. Redis looks up which connections are subscribed right now and pushes the message to each of them. Nothing is written anywhere.
Notice the return value of PUBLISH: it is the number of clients that received the message. When it is 0, the message has vanished. There is no queue, no history and no acknowledgement. Even a connected subscriber that is too slow can be disconnected when its output buffer passes the client-output-buffer-limit for pubsub clients, losing whatever was in flight.
That sounds bad, but it is exactly right for some jobs. Cache invalidation broadcasts are a good example: when an instance reconnects after a restart, its local cache is empty anyway, so missed invalidations do not matter. Live notifications such as typing indicators or dashboard ticks are the same. If missing a message is fine, pub/sub is the simplest and fastest option.
2How Streams Keep Messages
A stream is a Redis key holding an append-only log. XADD adds an entry, a small map of fields, and Redis gives it an id made of a millisecond timestamp and a sequence number, such as 1730900000000-0. Entries stay in the stream after they are read. Any number of readers can read the same entries, and a reader can start from any id.
So the offline subscriber problem disappears: a consumer that was down for ten minutes remembers the last id it processed and asks for everything after it. The catch is that plain XREAD leaves all the bookkeeping to us. Each reader must store its own position, and if we run three instances of a service, each one reads every entry. To share the work and track progress on the server, we need consumer groups.
3Consumer Groups and At-Least-Once Processing
A consumer group is a named cursor on the stream shared by several consumers. Redis hands each new entry to exactly one consumer in the group, and it remembers which entries were delivered but not yet confirmed. That list is the pending entries list (PEL). An entry leaves the PEL only when the consumer calls XACK.
7 messages in order. Play them, or step through them one at a time.
The entry survives the crash because it stays pending until someone acknowledges it.
The special id > means give me entries never delivered to anyone in this group. This is what makes it at-least-once: if worker-1 crashes after reading but before XACK, the entry is still in the PEL and can be delivered again. It also means an entry can be processed twice, once by the crashed worker (which may have done the work but not acknowledged it) and again after recovery. So the processing must be idempotent, for example by recording the order id that was billed.
When a consumer restarts with the same name, it should first read its own pending entries by passing id 0 instead of >, which returns its unacknowledged backlog, and only then switch to > for new entries. In Spring Data Redis, StreamMessageListenerContainer with a Consumer and manual acknowledge, or the receive(...) methods with autoAcknowledge off, maps directly onto these commands.
4Recovering Messages From Dead Consumers
If worker-1 never comes back, its pending entries would wait forever. XCLAIM and XAUTOCLAIM transfer ownership of entries that have been idle for longer than a minimum time. XAUTOCLAIM orders billing worker-2 60000 0 COUNT 50 says: give worker-2 up to 50 entries that have been pending for more than 60 seconds, scanning from the start of the PEL. Each claim increases the entry's delivery count, which XPENDING with a range shows.
Typically every consumer runs this claim step on a timer, so the group heals itself without an operator. The idle time must be longer than the slowest normal processing, otherwise a healthy but slow consumer has its work stolen and the entry is processed twice.
5Keeping the Stream Bounded
Because entries are never removed by reading or acknowledging, a stream grows until memory runs out. We cap it either at write time or separately.
The ~ makes trimming approximate, so Redis removes whole internal nodes at once, which is much cheaper than trimming to an exact count. But there is a catch: trimming does not care about the PEL. If a consumer group is far behind, trimming can delete entries it has not processed yet. So the cap must be sized from the write rate and the longest consumer lag we tolerate, and lag should be monitored with XINFO GROUPS.
6Follow-up questions
How do you handle a message that always fails (a poison message)?
Each claim raises the entry's delivery count. When the claim step sees an entry whose count is above a limit, say 5, it stops retrying: it copies the entry with XADD to a dead-letter stream such as orders.dlq, together with the error, and then XACKs the original so it leaves the PEL. An alert on the dead-letter stream lets someone inspect and replay it after a fix. Without this, one bad entry is retried forever and keeps a consumer busy.
Can pub/sub and Streams be combined?
Yes. A common pattern is to write the durable event to a stream and use pub/sub, or keyspace notifications, only as a hint that something changed. Consumers that miss the hint still catch up from the stream. But often a blocking XREADGROUP already gives low latency, so the extra channel is not needed.
Does a stream survive a Redis restart?
It survives exactly as well as any other key: with AOF or RDB persistence it is reloaded, without persistence it is lost. With asynchronous replication, entries written just before a failover can be missing on the new primary. Streams are persistent compared with pub/sub, but not as durable as a replicated disk log like Kafka.
7Conclusion
In this answer, we saw that pub/sub only reaches subscribers connected at that moment, which suits cache invalidation and live signals, while Streams keep an append-only log that consumers can resume from an id. Consumer groups add server-side tracking: XREADGROUP with > hands out new entries, XACK confirms them, the PEL and XPENDING show what is unconfirmed, and XAUTOCLAIM moves stuck entries to a live consumer. We also saw why processing must be idempotent and why every stream needs a MAXLEN or MINID cap.
Open with
Pub/sub and Streams solve different problems. With pub/sub, PUBLISH pushes the message only to clients subscribed at that moment, and the return value tells you how many got it.
Then, if they want more
If a subscriber is offline or disconnected for a slow buffer, the message is simply lost, so I use it for things like cache invalidation broadcasts or live notifications where a missed message does not matter.
A stream is a persistent append-only log: XADD appends an entry with an id, and entries stay after they are read, so a consumer can come back and continue from the last id it saw with XREAD.
For at-least-once processing I use a consumer group: XGROUP CREATE, then each worker calls XREADGROUP GROUP billing worker-1 COUNT 10 STREAMS orders with the greater-than id for new entries. Delivered entries sit in the pending entries list until the worker calls XACK, and XPENDING shows them. If a worker dies, another one takes its idle entries with XAUTOCLAIM or XCLAIM.
Because an entry can be delivered twice, the processing has to be idempotent, and poison messages go to a dead-letter stream after a few attempts. Finally I always cap the stream with XADD MAXLEN tilde or XTRIM, otherwise it grows without limit.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Redis documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Compare Redis pub/sub with Redis Streams. What happens to a message in each when a...": https://scrivio-interview-library.netlify.app/databases/redis/f24aabd184/