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
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:
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:
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): 20Production 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.
Delivery is at least once. A consumer can process an entry, crash
before XACK, and have the entry redelivered to someone else. Make
handlers idempotent (for example, record processed IDs in a set, or
use the entry ID as a database unique key), or you'll double-process
after every crash.
Trimming
Streams live in memory and don't delete entries on their own, even after every group has acknowledged them. Cap them:
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
- Run
streams.pyand readXINFO GROUPS stream:ordersafterwards. - Create a second group
analyticson the same stream from0and read with it. It gets all 20 entries again. - 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 tostream:orders:dlq. XADD1,000 entries withMAXLEN ~ 100and checkXLEN. It's a bit over 100, because of the approximate trim.