Redis Streams are an append-only log data structure introduced in Redis 5 — closer to a Kafka topic than a pub/sub channel. Entries are immutable, carry a server-assigned timestamp ID, persist with the rest of the database, and can be consumed by named groups that track per-consumer pending acknowledgments. This article covers the producer side (XADD), the two read modes (XREAD for tailing, XREADGROUP for at-least-once group consumption), and the operational levers that keep a stream from growing without bound.
How to verify
# Confirm the type and length of an existing stream
redis-cli TYPE events:orders
redis-cli XLEN events:orders
redis-cli XINFO STREAM events:orders
# Inspect group state
redis-cli XINFO GROUPS events:orders
redis-cli XINFO CONSUMERS events:orders billing
# Read the last N entries
redis-cli XREVRANGE events:orders + - COUNT 10
What’s happening
An entry in a stream has an ID of the form <ms-timestamp>-<sequence>. The default XADD events:orders * field value ... lets the server generate *, which becomes the current millisecond and a monotonically increasing sequence for entries in the same millisecond. IDs are always strictly monotonic; if a client clock jumps backwards, the server clamps to one greater than the latest ID. This makes a stream a totally ordered log.
There are two consumption modes. XREAD STREAMS events:orders $ is a non-blocking or blocking tail — it reads entries strictly newer than the cursor the caller passes in. It is good for fan-out broadcasting where every consumer wants every entry. XREADGROUP GROUP billing worker-1 STREAMS events:orders > is the at-least-once consumer-group mode — entries are distributed across consumers in the group, each entry is marked pending until XACK’d, and unacknowledged entries can be claimed by another consumer with XCLAIM after a timeout. This is the mode for work queues.
Streams persist with the database — they go to RDB snapshots and AOF, replicate to replicas, and survive restarts. Memory cost is bounded by entry count, so a stream needs a trimming strategy.
The procedure
-
Produce entries with XADD.
MAXLEN ~is a near-exact trim that caps memory.redis-cli XADD events:orders MAXLEN '~' 1000000 '*' \ order_id 1042 customer 17 amount 79.50The
~makes trimming O(1) by allowing slight overshoot at the macroblock boundary; without it the trim is strict but slower. -
Tail with XREAD (fan-out consumption). Block up to 5 seconds waiting for new entries newer than the last ID seen.
LAST="$" while true; do redis-cli XREAD BLOCK 5000 COUNT 100 STREAMS events:orders "$LAST" # parse the entries, update LAST to the last ID returned done -
Create a consumer group.
MKSTREAMcreates the stream if it does not exist;$starts the group at the current tail so historical entries are not redelivered.redis-cli XGROUP CREATE events:orders billing '$' MKSTREAM -
Consume with XREADGROUP. The
>cursor means “entries never delivered to any consumer in this group”.redis-cli XREADGROUP GROUP billing worker-1 \ BLOCK 5000 COUNT 10 STREAMS events:orders '>' -
Acknowledge after processing. Until you
XACK, the entry stays in the pending entries list (PEL) for that consumer.redis-cli XACK events:orders billing 1717326000123-0 -
Recover stuck entries with XAUTOCLAIM. Take ownership of entries that have been pending longer than 60 seconds — they belonged to a worker that crashed.
redis-cli XAUTOCLAIM events:orders billing worker-2 60000 0 -
Trim a stream by age.
MINIDkeeps entries newer than a given ID — useful when you key trimming on time rather than count.# Drop everything older than 1 hour ago in millisecond ID space CUTOFF=$(( ($(date +%s) - 3600) * 1000 )) redis-cli XTRIM events:orders MINID "$CUTOFF"
Common pitfalls
- A consumer that reads with
>and crashes beforeXACKleaves the entry in the PEL forever — without a periodicXAUTOCLAIMyou grow a backlog of stuck entries and lose work. XADDwithoutMAXLENorMINIDproduces unbounded growth. Set the trim every write or run a separate trimmer.- Reading with
XREADon a real-time tail and never persisting your cursor means a consumer restart skips entries between disconnect and reconnect. Persist the last ID in the same Redis or in the application’s database. - Streams are not partitioned in core Redis — a single stream is a single key on a single shard. For high throughput on a cluster, shard the stream key with hash tags:
events:{shard1},events:{shard2}. - Memory accounting for streams shows up under
INFO memoryasused_memory_overheadnot justused_memory_dataset. A long PEL with millions of unacknowledged entries can be the actual memory leak.
In the engagements we run, every Redis Streams workload comes with a trim policy (count or age), an XAUTOCLAIM cron that recovers stuck entries, and a Prometheus alert when the consumer group lag exceeds a threshold. We operate that under Managed Operations, and for clients comparing streams against pub/sub the choice usually comes down to durability and replay needs.