XREADGROUP
Syntax
XREADGROUP GROUP group consumer [COUNT count] [BLOCK milliseconds] [CLAIM min-idle-time] [NOACK] STREAMS key [key ...] id [id ...]
Client
- Python (redis-py)
- Node.js (node-redis)
- Java-Sync (Jedis)
- Lettuce-Sync
- Java-Async (Lettuce)
- Java-Reactive (Lettuce)
- Go (go-redis)
- C#-Sync (NRedisStack)
- C#-Async (NRedisStack)
- PHP (Predis)
- Rust-Sync (redis-rs)
- Rust-Async (redis-rs)
Example Code
xreadgroup(
groupname: str,
consumername: str,
streams: Dict[KeyT, StreamIdT],
count: Optional[int] = None,
block: Optional[int] = None,
noack: bool = False,
claim_min_idle_time: Optional[int] = None
) → ResponseT
Notes
Available since: Redis Open Source 5.0.0
Time complexity: For each stream mentioned: O(M) with M being the number of elements returned.
ACL categories: @write, @stream, @slow, @blocking.
Usage Example
Normally you use the command like that in order to get new messages and process them. In pseudo-code:
WHILE true
entries = XREADGROUP GROUP $GroupName $ConsumerName BLOCK 2000 COUNT 10 STREAMS mystream >
if entries == nil
puts "Timeout... try again"
CONTINUE
end
FOREACH entries AS stream_entries
FOREACH stream_entries as message
process_message(message.id,message.fields)
# ACK the message as processed
XACK mystream $GroupName message.id
END
END
END
In this way the example consumer code will fetch only new messages, process them, and acknowledge them via XACK. However, the example code above is not complete, because it does not handle recovering after a crash.
Differences between XREAD and XREADGROUP
- The difference is that
XREADGROUPsupports consumer groups. - Without consumer groups, all clients get all the entries arriving in a stream.
- With consumer groups, it's possible to create groups of clients consuming different parts of the messages arriving in a stream.
Ordering Guarantees
Within each stream, entries are reported in the same order they were added by XADD (older first). When not blocked, across streams, entries are reported in the order the streams were specified.