0
0
Redisquery~5 mins

XREADGROUP for consumer groups in Redis

Choose your learning style9 modes available
Introduction
XREADGROUP helps multiple consumers share the work of reading messages from a Redis stream without missing or repeating messages.
When you want several workers to process messages from the same stream without overlap.
When you need to ensure each message is processed at least once by one consumer.
When you want to track which messages have been delivered but not yet acknowledged.
When you want to balance load among consumers reading from a stream.
When you want to recover unacknowledged messages for retry.
Syntax
Redis
XREADGROUP GROUP <groupname> <consumername> [COUNT <count>] [BLOCK <milliseconds>] STREAMS <key> <id>
Use '>' as the to read new messages never delivered to any consumer in the group.
COUNT limits how many messages to return; BLOCK waits for new messages if none are available.
Examples
Read new messages from 'mystream' for consumer 'Alice' in group 'mygroup'.
Redis
XREADGROUP GROUP mygroup Alice STREAMS mystream >
Read up to 5 new messages for consumer 'Bob' in group 'mygroup'.
Redis
XREADGROUP GROUP mygroup Bob COUNT 5 STREAMS mystream >
Wait up to 1000 milliseconds for new messages for 'Alice' before returning.
Redis
XREADGROUP GROUP mygroup Alice BLOCK 1000 STREAMS mystream >
Sample Program
Create a group 'mygroup' on 'mystream' starting at the beginning, add a message with field 'name' and value 'Alice', then read new messages for consumer 'Alice'.
Redis
XGROUP CREATE mystream mygroup 0
XADD mystream * name Alice
XREADGROUP GROUP mygroup Alice STREAMS mystream >
OutputSuccess
Important Notes
You must create the consumer group with XGROUP CREATE before using XREADGROUP.
Use '>' as the ID to get only new messages never delivered to any consumer in the group.
Remember to acknowledge messages with XACK after processing to avoid redelivery.
Summary
XREADGROUP lets multiple consumers share reading from a Redis stream safely.
Use consumer groups to balance work and track message processing.
Always create the group first and acknowledge messages after processing.