Skip to content

Lesson 8 of 9 · 28m

Streams and Consumer Groups for Workflows That Survive Failure

Design event feeds with XADD, consumer groups, pending entries, acknowledgements, trimming, and recovery loops that operators can actually run.

Redis Streams are what teams usually meant when they first tried to build a queue out of lists. A stream keeps ordered entries, supports multiple consumer groups, tracks pending deliveries, and lets consumers resume after failure. It is not Kafka, and pretending it is will hurt. It is an excellent Redis-native workflow primitive when the data volume, retention, and operational model fit.

Entries are ordered records, not mutable rows

XADD stream:app-events * type user.signup user_id u_934 tenant t_77
XADD stream:app-events 1780000100-0 type billing.invoice_paid invoice in_1881
XRANGE stream:app-events - + COUNT 5

IDs are millisecond-time plus sequence. Use * unless you have a strong reason to assign IDs. Entries are immutable once added. If state changes, append another event; do not conceptualize stream entries as rows awaiting update.

Consumer groups divide work and track delivery

XGROUP CREATE stream:app-events workers $ MKSTREAM
XREADGROUP GROUP workers worker-1 COUNT 10 BLOCK 5000 STREAMS stream:app-events >

The > means "give this consumer entries never delivered to the group." When the worker processes an entry successfully, acknowledge it:

XACK stream:app-events workers 1780000100-0

Acknowledgement removes the entry from the pending entries list for that consumer. It does not delete the entry from the stream. Other groups can still read it, and trimming policy controls retention.

Pending entries are the failure contract

Inspect work that was delivered but never acknowledged:

XPENDING stream:app-events workers
XPENDING stream:app-events workers - + 20
XAUTOCLAIM stream:app-events workers worker-2 60000 0 COUNT 10

A worker crash leaves entries pending. Recovery means another worker claims entries idle longer than your processing timeout, then either completes and acknowledges them or applies poison-message handling after repeated failure. If you do not inspect pending entries, a "reliable" stream silently stops making progress.

Trim intentionally or memory grows forever

XADD stream:app-events MAXLEN ~ 100000 * type metric name cpu value 0.82
XTRIM stream:app-events MAXLEN ~ 100000

Approximate trimming (~) is more efficient because Redis can drop whole internal nodes. Exact trimming is more precise and more expensive. Retention must match replay requirements. If downstream systems need seven-day replay but you trim at one hour, you have built data loss into the pipeline.

Know the boundaries

Streams do not provide cross-stream transactions, infinite retention, sophisticated partition rebalancing, or Kafka-style ecosystem semantics. They do provide low-latency ordered events inside Redis with strong operational simplicity. Use them for job dispatch, integration events, audit-like append feeds, and fanout where Redis memory and operations already make sense.

Field notes

  • XREAD reads streams without group state; XREADGROUP participates in delivery tracking. Mixing them accidentally creates confusion.
  • Blocking consumers need sane timeouts. A client that disappears while holding pending entries is normal and must be handled.
  • Consumer names should be stable enough to diagnose ownership but unique enough to avoid two live workers sharing identity.
  • Pending entries grow silently. Alert on group lag, stream length, memory, and old pending age together.

Was this lesson helpful?

Sign in to rate