Redis Streams

Difficulty: Advanced

Question

What are Redis Streams, how do consumer groups work, and when would you pick Streams over Kafka?

Answer

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.

How entries and IDs work

  • 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.
  • IDs are always increasing, so you can range over a stream by time with XRANGE and XREVRANGE.
  • Unlike Pub/Sub, entries stay in the stream after being read. A consumer that was offline can come back and read from the last ID it saw.
  • The stream grows forever unless you trim it with 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.

How consumer groups distribute and track work

  1. Create a group once: XGROUP CREATE orders billing $ MKSTREAM. The $ means start from new messages only; 0 would replay history.
  2. Each worker calls 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".
  3. Every delivered entry goes into the group's Pending Entries List (PEL) under that consumer's name.
  4. After processing, the worker calls XACK orders billing <id>, which removes it from the PEL.
  5. If a worker crashes, its entries sit in the PEL. Another worker uses 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.

Redis Streams vs Kafka

AspectRedis StreamsKafka
StorageIn memory, persisted via RDB/AOFOn disk, sequential log segments
RetentionBounded by RAM, trim with MAXLEN/MINIDDays or weeks of data, bounded by disk
ScalingOne stream lives on one shardTopics split into partitions across brokers
OrderingTotal order within a streamOrder within a partition
OperationsAlready have Redis, almost zero setupBrokers, KRaft controllers, more tuning
EcosystemClient librariesKafka 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.

Where Streams fit in real systems

  • Background job pipelines, such as sending order confirmation SMS and email after checkout.
  • Activity feeds and audit logs that you keep for a short window.
  • Fan-out to several independent services: each service creates its own consumer group on the same stream and reads every entry.
  • IoT or telemetry buffers where you only care about recent data.

Common mistakes

  • Never trimming the stream, then running out of memory months later.
  • Forgetting to XACK, so the PEL grows without bound and XPENDING gets slow.
  • Not running a reclaim loop with XAUTOCLAIM, so messages owned by dead consumers are never processed.
  • Assuming exactly-once semantics. Streams give at-least-once; deduplicate using the entry ID or a business key.
Consumer group message lifecycle. An entry is added, delivered to one consumer, tracked as pending, then acknowledged.
  1. Producer → Redis Stream: XADD orders * amount 49900
  2. Worker A → Redis Stream: XREADGROUP ... STREAMS orders >
  3. Redis Stream → Worker A: Entry delivered, added to PEL
  4. Worker A → Redis Stream: XACK orders billing id
  5. Worker B → Redis Stream: XAUTOCLAIM idle entries from dead workers

Code examples

Producer and consumer group with redis-cli

# 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.

Worker loop in Node.js with ioredis

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.

Key points

  • Streams are an append-only log with time-based IDs; entries persist after being read, unlike Pub/Sub.
  • XREADGROUP with > fetches never-delivered entries; delivered entries live in the PEL until XACK.
  • Delivery is at-least-once, so handlers must be idempotent.
  • Trim with MAXLEN ~ or MINID or the stream will eat all your memory.
  • Use XAUTOCLAIM to recover entries from crashed consumers.
  • A single stream lives on one node; shard across several streams for more throughput.

Common follow-up questions

  • How would you handle a poison message that keeps failing and being reclaimed?
  • How is a Redis Stream different from Redis Pub/Sub?
  • How would you scale a stream beyond what one Redis node can handle?
  • What happens to pending entries if Redis restarts?
  • How do you guarantee exactly-once processing on top of Streams?

Interview tips

  • Walk through the PEL and XACK lifecycle; it shows you understand delivery guarantees, not just the commands.
  • Do not claim Streams replace Kafka; compare retention, partitioning and ecosystem honestly.
  • Mention a dead-letter strategy using the delivery count that XPENDING reports.

Concepts covered

Redis Streams, Consumer Groups, XADD and XREADGROUP, Pending Entries List, XACK, Stream Trimming

More Redis & Caching questions