0
0
RedisConceptBeginner · 3 min read

What is Consumer Group in Redis Streams: Explanation and Example

A consumer group in Redis Streams is a way to allow multiple clients to share the workload of processing messages from a stream. It lets consumers read different parts of the stream independently while tracking which messages have been processed, enabling scalable and reliable message consumption.
⚙️

How It Works

Imagine a group of friends sharing a box of letters to read. Instead of everyone reading the same letter, they divide the letters so each friend reads different ones without overlap. In Redis Streams, a consumer group works similarly by letting multiple consumers read from the same stream but each consumer gets its own set of messages to process.

When a message is added to the stream, Redis keeps track of which consumer in the group has read it. This way, no message is lost or processed twice unless explicitly needed. If a consumer crashes, other consumers can claim its pending messages, ensuring reliable processing.

💻

Example

This example shows how to create a consumer group, add messages to a stream, and read messages from the group.

redis
XGROUP CREATE mystream mygroup $ MKSTREAM
XADD mystream * temperature 22.5
XADD mystream * temperature 23.0
XREADGROUP GROUP mygroup consumer1 COUNT 2 STREAMS mystream >
Output
1) 1) "mystream" 2) 1) 1) "1609459200000-0" 2) 1) "temperature" 2) "22.5" 2) 1) "1609459200001-0" 2) 1) "temperature" 2) "23.0"
🎯

When to Use

Use consumer groups when you want to process messages from a Redis stream with multiple workers or clients in parallel. This is helpful for scaling tasks like event processing, logging, or real-time data pipelines.

For example, a web application might use consumer groups to distribute user activity logs to several processors that analyze data simultaneously without missing any events.

Key Points

  • Consumer groups allow multiple clients to share message processing from a single Redis stream.
  • Redis tracks which messages each consumer has read to avoid duplication.
  • Unacknowledged messages can be claimed by other consumers to ensure reliability.
  • Useful for scaling and parallelizing message processing tasks.

Key Takeaways

Consumer groups enable multiple clients to read from the same Redis stream without overlapping work.
Redis tracks message delivery per consumer to ensure no message is lost or processed twice.
They are ideal for scaling message processing across many workers.
Unprocessed messages can be reassigned to other consumers for fault tolerance.