0
0
Redisquery~5 mins

Consumer groups concept in Redis

Choose your learning style9 modes available
Introduction
Consumer groups help multiple clients share the work of reading messages from a stream without missing any messages or reading the same message twice.
When you want several workers to process messages from a stream in parallel.
When you need to make sure each message is processed only once by one worker.
When you want to balance the load of message processing across multiple clients.
When you want to track which messages have been processed and which are still pending.
When you want to build a reliable message queue system using Redis streams.
Syntax
Redis
XGROUP CREATE <stream> <groupname> <id or $>
XREADGROUP GROUP <groupname> <consumername> COUNT <count> STREAMS <stream> >
XACK <stream> <groupname> <id> [<id> ...]
XGROUP CREATE creates a new consumer group for a stream.
XREADGROUP reads messages for a specific consumer in the group, marking them as pending until acknowledged.
Examples
Create a consumer group named 'mygroup' on stream 'mystream' starting from new messages.
Redis
XGROUP CREATE mystream mygroup $
Consumer 'consumer1' reads up to 2 new messages from 'mystream' in group 'mygroup'.
Redis
XREADGROUP GROUP mygroup consumer1 COUNT 2 STREAMS mystream >
Acknowledge that message with ID '1526985058136-0' was processed by the group.
Redis
XACK mystream mygroup 1526985058136-0
Sample Program
This example creates a group, adds two messages, reads them as consumer1, then acknowledges them.
Redis
XADD mystream * name Alice
XADD mystream * name Bob
XGROUP CREATE mystream mygroup $ 
XREADGROUP GROUP mygroup consumer1 COUNT 2 STREAMS mystream >
XACK mystream mygroup 1-0 2-0
OutputSuccess
Important Notes
Consumer groups allow multiple consumers to share the workload without duplicating message processing.
Messages remain pending until acknowledged with XACK, so you can track unprocessed messages.
Use '>' in XREADGROUP to get new messages that were never delivered to any consumer.
Summary
Consumer groups let many clients read from the same stream safely and efficiently.
They help avoid duplicate processing by tracking message delivery and acknowledgment.
You create groups, read messages as consumers, and acknowledge messages when done.