Skip to the page

Redis in depth · mid

Redis pub/sub or Streams

Pub/sub is fire-and-forget. Streams keep a log with ids and consumer groups. Choose by what a missed message costs.

The notes

What you must know

  • Pub/sub is at-most-once and stores nothing. A subscriber that is disconnected misses every message published meanwhile. A slow subscriber's messages pile up in an output buffer in Redis until a limit disconnects it. Unlimited buffers can fill memory.
  • In Cluster, a plain PUBLISH is sent to every node. Sharded pub/sub (SPUBLISH and SSUBSCRIBE) keeps a channel on the shard that owns its slot.
  • Streams: an append-only log with ids. XADD, trimming with MAXLEN or MINID, and readers resume from the last id (XREAD BLOCK). A stream with no trimming grows until memory is full.
  • Consumer groups (XREADGROUP, XACK, the pending list, XAUTOCLAIM) share work. They are not for fan-out where every server needs every message.
  • For live UI updates where the app refetches the current state on reconnect, pub/sub is simple and enough, because the order API is the source of truth. If a missed update matters (notification workers, audit), use Streams or a real broker.
  • Each WebSocket server should subscribe only to the channels of its connected users, not to one firehose.

The red pen

Common traps: mistake → fix

  • Mistake: Pub/sub keeps messages until they are read

    Fix: it keeps nothing.

  • Mistake: A consumer group is fan-out

    Fix: it shares messages among consumers.

  • Mistake: Streams need no trimming

    Fix: they grow without limit.

Easy to hard

Questions interviewers ask, easy to hard

  1. Pub/sub or Streams for live order updates?
  2. What happens when a WebSocket server restarts, in each?
  3. What is a consumer group in Streams?

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview