Learn Labs
Messaging

Streams

Append-only logs, consumer groups, the pending list, and recovery

A stream is an append-only log of entries, each a small set of field-value pairs with a unique, time-ordered ID. Unlike Pub/Sub, entries are stored, so consumers can read history, resume after a crash, and acknowledge work. With consumer groups, a stream is a reliable work queue, a small Kafka inside Redis.

Adding and reading

XADD stream:orders * order_id 7000 amount 10* = auto ID, e.g. 1791790000000-0XADD stream:orders MAXLEN ~ 100000 * order_id 7001 amount 11append and trim to ~100k entriesXLEN stream:ordersXRANGE stream:orders - + COUNT 10oldest firstXREVRANGE stream:orders + - COUNT 10newest firstXREAD COUNT 10 STREAMS stream:orders 0everything after ID 0XREAD BLOCK 5000 STREAMS stream:orders $wait up to 5s for NEW entries only

Entry IDs are <milliseconds>-<sequence>: monotonically increasing, so you can range-query by time (XRANGE stream:orders 1791790000000 +).

Plain XREAD is like Kafka without consumer groups: every reader sees everything and tracks its own position.

Consumer groups

A consumer group shares a stream's entries among several consumers, so each entry goes to one consumer in the group, and tracks what each one has acknowledged:

XGROUP CREATE stream:orders billing 0 MKSTREAMgroup starts at the beginningXREADGROUP GROUP billing worker-a COUNT 8 STREAMS stream:orders >> = never-delivered entriesXACK stream:orders billing 1791790000000-0done with this oneXPENDING stream:orders billingdelivered but not acked, per consumerXAUTOCLAIM stream:orders billing worker-a 60000 0-0take over entries idle > 60sXINFO GROUPS stream:orderslag, last delivered ID, pending count

Different groups on the same stream are independent: billing and analytics each get every entry, and within each group the entries are split across that group's consumers. That's the same model as Kafka consumer groups.

The pending entries list

When a consumer reads an entry with XREADGROUP, it moves into the group's Pending Entries List (PEL) until the consumer XACKs it. If the consumer crashes first, the entry stays pending and can be claimed by another consumer. labs/streams.py walks through exactly that:

no XACKXAUTOCLAIMstream:orders20 entriesgroup: billingXREADGROUP >worker-bread 8, crashedPEL8 pending for bworker-aacked its 8worker-a claims the 8 idle entries, processes and acks them: pending drops to 0
docker compose -f compose_prod.yaml run --rm labs python streams.py

worker-a processed 8, worker-b read 8 and crashed
pending in PEL: 8  by consumer: [{'name': 'worker-b', 'pending': 8}]
worker-a claimed and acked 8 of worker-b's messages
worker-a processed the remaining 4
pending now: 0
stream length (messages are kept until trimmed): 20

Production consumers run a periodic XAUTOCLAIM for entries idle longer than a timeout. XPENDING also reports a delivery count per entry. An entry that keeps failing and being reclaimed is a poison message. After N deliveries, move it to a dead-letter stream and XACK it.

Trimming

Streams live in memory and don't delete entries on their own, even after every group has acknowledged them. Cap them:

XADD stream:orders MAXLEN ~ 100000 * ...trim on every append (~ = approximate, much cheaper)XTRIM stream:orders MINID ~ 1791700000000drop entries older than a timestampXINFO STREAM stream:orderslength, first/last entry, radix tree nodes

The ~ lets Redis trim whole internal nodes at a time instead of exactly to the number, which is far cheaper. Trimming ignores consumer groups: an entry trimmed before a slow group read it is gone for that group. Size MAXLEN with your slowest consumer's worst lag in mind.

Internals

A stream is a radix tree of listpacks: entries are grouped into compact blocks keyed by ID prefix, so appending is O(1) and range lookups by ID are O(log N). Field names are de-duplicated against the first entry in each block, which keeps streams with a fixed schema small.

Lab

  1. Run streams.py and read XINFO GROUPS stream:orders afterwards.
  2. Create a second group analytics on the same stream from 0 and read with it. It gets all 20 entries again.
  3. Add a dead-letter step: re-run the crash scenario three times without acking, check the delivery count in XPENDING stream:orders billing - + 10, and move entries delivered more than twice to stream:orders:dlq.
  4. XADD 1,000 entries with MAXLEN ~ 100 and check XLEN. It's a bit over 100, because of the approximate trim.

On this page