Difficulty: Advanced
What are Redis Streams, how do consumer groups work, and when would you pick Streams over Kafka?
A Redis Stream is an append-only log data type, added in Redis 5.0, where every entry gets a unique, time-ordered ID and holds a small set of field-value pairs. Consumer groups let several workers share the work of reading a stream, with Redis tracking which entry was delivered to which consumer and which ones are still unacknowledged. It gives you durable, replayable messaging inside Redis, which makes it a good fit for moderate-scale event processing where running Kafka would be overkill.
XADD orders * amount 49900 user 42 appends an entry. The * asks Redis to generate an ID like 1718000000000-0, which is milliseconds since epoch plus a sequence number.XRANGE and XREVRANGE.XADD ... MAXLEN ~ 100000 or XTRIM. Since 6.2 you can also trim by ID with MINID, which is handy for keeping "the last 7 days". The ~ makes trimming approximate and much cheaper.XGROUP CREATE orders billing $ MKSTREAM. The $ means start from new messages only; 0 would replay history.XREADGROUP GROUP billing worker-1 COUNT 10 BLOCK 5000 STREAMS orders >. The special ID > means "give me entries never delivered to anyone in this group".XACK orders billing <id>, which removes it from the PEL.XPENDING to find them and XAUTOCLAIM (6.2+) or XCLAIM to take ownership of entries idle longer than some threshold.This gives at-least-once delivery. Your handler must be idempotent, because an entry can be processed twice if a worker finishes the work but dies before XACK.
| Aspect | Redis Streams | Kafka |
|---|---|---|
| Storage | In memory, persisted via RDB/AOF | On disk, sequential log segments |
| Retention | Bounded by RAM, trim with MAXLEN/MINID | Days or weeks of data, bounded by disk |
| Scaling | One stream lives on one shard | Topics split into partitions across brokers |
| Ordering | Total order within a stream | Order within a partition |
| Operations | Already have Redis, almost zero setup | Brokers, KRaft controllers, more tuning |
| Ecosystem | Client libraries | Kafka Connect, Streams API, schema registry |
The honest summary: Streams are simpler and have very low latency, but a single stream is bound by one node's memory and CPU. Kafka is built for high-throughput, long-retention, multi-team event pipelines.
XACK, so the PEL grows without bound and XPENDING gets slow.XAUTOCLAIM, so messages owned by dead consumers are never processed.# Create the group and the stream if missing
redis-cli XGROUP CREATE orders billing $ MKSTREAM
# Producer: append an order event, keep roughly the last 100k entries
redis-cli XADD orders MAXLEN '~' 100000 '*' orderId 9812 amount 49900
# Consumer: read up to 10 new entries, block up to 5s
redis-cli XREADGROUP GROUP billing worker-1 COUNT 10 BLOCK 5000 STREAMS orders '>'
# Acknowledge after successful processing
redis-cli XACK orders billing 1718000000000-0
# See what is still pending for the group
redis-cli XPENDING orders billing
# Reclaim entries idle for more than 60s into worker-2
redis-cli XAUTOCLAIM orders billing worker-2 60000 0-0 COUNT 10
This is the full at-least-once loop: add, read with a group, acknowledge, and reclaim work from crashed consumers.
import Redis from 'ioredis';
const redis = new Redis();
const STREAM = 'orders', GROUP = 'billing', ME = 'worker-1';
async function run() {
while (true) {
const res = await redis.xreadgroup(
'GROUP', GROUP, ME, 'COUNT', 10, 'BLOCK', 5000,
'STREAMS', STREAM, '>'
);
if (!res) continue; // timed out, loop again
for (const [, entries] of res) {
for (const [id, fields] of entries) {
await handleOrder(fields); // must be idempotent
await redis.xack(STREAM, GROUP, id);
}
}
}
}
async function handleOrder(fields) {
console.log('processing', fields);
}
run();
Each worker uses its own consumer name in the same group, so Redis spreads entries across workers and tracks unacknowledged ones per worker.
Redis Streams, Consumer Groups, XADD and XREADGROUP, Pending Entries List, XACK, Stream Trimming