Consumer Groups
What is a consumer group
Section titled “What is a consumer group”A consumer group lets multiple workers (consumers) read from the same stream, with each entry delivered to only ONE consumer in the group. Redis tracks which entries have been delivered but not yet acknowledged. This enables at-least-once processing.
Creating a group — XGROUP CREATE
Section titled “Creating a group — XGROUP CREATE”127.0.0.1:6379> XADD jobs * type "email" to "[email protected]""1700000000000-0"127.0.0.1:6379> XADD jobs * type "sms" to "+6681234567""1700000000001-0"127.0.0.1:6379> XGROUP CREATE jobs workers $ MKSTREAMOK$ means “start from the latest entry” — only new messages added after the group is created will be delivered. Use 0 to read from the beginning of the stream. MKSTREAM creates the stream automatically if it does not exist yet.
XREADGROUP — reading as a consumer
Section titled “XREADGROUP — reading as a consumer”127.0.0.1:6379> XADD jobs * type "email" to "[email protected]""1700000000002-0"127.0.0.1:6379> XREADGROUP GROUP workers consumer-1 COUNT 1 STREAMS jobs >1) 1) "jobs" 2) 1) 1) "1700000000002-0" 2) 1) "type" 2) "email" 3) "to" 4) "[email protected]"The special ID > means “give me new undelivered entries”. Once delivered, the entry is placed in the Pending Entries List (PEL) for consumer-1 until it is explicitly acknowledged.
XACK — acknowledging entries
Section titled “XACK — acknowledging entries”127.0.0.1:6379> XACK jobs workers 1700000000002-0(integer) 1Once acknowledged, the entry is removed from the PEL. Only call XACK after your worker has successfully processed the message — acknowledging too early risks data loss if the worker crashes mid-processing.
XPENDING — inspecting unacknowledged entries
Section titled “XPENDING — inspecting unacknowledged entries”127.0.0.1:6379> XPENDING jobs workers - + 101) 1) "1700000000002-0" 2) "consumer-1" 3) (integer) 4321 4) (integer) 1Each row shows: the entry ID, the consumer holding it, how long it has been idle in milliseconds, and how many times it has been delivered. This is your primary tool for detecting stuck or slow consumers.
XADD jobs * type "email" to "[email protected]"
XADD jobs * type "sms" to "+6681234567"
XGROUP CREATE jobs workers 0 MKSTREAM
XREADGROUP GROUP workers consumer-1 COUNT 10 STREAMS jobs >
XACK jobs workers 1700000000000-0
XPENDING jobs workers - + 10