Pub/Sub and StreamsLesson 4.3
Redis Stream consumer groups: reliable message processing with XACK
XGROUP CREATE, XREADGROUP, XACK, pending entries list, consumer group semantics, at-least-once delivery, XCLAIM for stuck messages
Consumer groups for reliable processing
Consumer groups let multiple workers share stream processing, with each message delivered to exactly one worker. Unacknowledged messages stay in a pending entries list (PEL) so they can be reclaimed if a worker fails.
Setup and consuming
# Create a group starting from the beginning of the stream
XGROUP CREATE events workers $ MKSTREAM
# Worker reads up to 10 undelivered messages
XREADGROUP GROUP workers consumer1 COUNT 10 STREAMS events >
# '>' means: give me messages not yet delivered to any consumerAcknowledging
# After processing, acknowledge the entry ID
XACK events workers 1700000000000-0Recovering stuck messages
# List messages pending longer than 60 seconds
XPENDING events workers - + 10
# Claim them for another consumer
XCLAIM events workers consumer2 60000 1700000000000-0Without XACK, the message stays in the PEL. XCLAIM transfers ownership to a healthy consumer, ensuring at-least-once delivery. Design your consumers to be idempotent — the same message may be processed more than once if a worker crashes after processing but before ACKing.
