TExcelTAD
Home › Redis › Redis Stream Consumer Groups

Redis Stream Consumer Groups

With plain XREAD, every reader sees every entry. That is right for broadcasting, but for a work queue you want the opposite: a pool of workers that share the entries, each entry processed by exactly one worker, and nothing lost if a worker crashes. Consumer groups provide exactly that on top of a stream.

The model

Term Meaning
Group a named cursor on a stream, shared by its consumers (emails, billing, ...)
Consumer one worker inside a group, identified by a name you choose (worker-1)
Last delivered ID how far the group has handed out entries
Pending entries list (PEL) entries delivered to a consumer but not yet acknowledged

A stream can have many groups, and each group sees the whole stream independently. Inside a group, entries are spread over the consumers.

Creating a group and reading

XGROUP CREATE stream group start-id creates a group. Start at 0 to process the existing history, or at $ to get only new entries. MKSTREAM creates the stream if it does not exist.

XREADGROUP GROUP group consumer COUNT n STREAMS stream > reads entries for a consumer. The special ID > means "entries never delivered to anyone in this group".

redis · try it yourself
XADD jobs 1-1 task email to ada@example.com
XADD jobs 2-1 task email to linus@example.com
XADD jobs 3-1 task resize image cat.png
XGROUP CREATE jobs workers 0
XREADGROUP GROUP workers w1 COUNT 1 STREAMS jobs >
XREADGROUP GROUP workers w2 COUNT 1 STREAMS jobs >
XREADGROUP GROUP workers w1 COUNT 5 STREAMS jobs >
XREADGROUP GROUP workers w2 COUNT 5 STREAMS jobs >
redis> XADD jobs 1-1 task email to ada@example.com
"1-1"
redis> XADD jobs 2-1 task email to linus@example.com
"2-1"
redis> XADD jobs 3-1 task resize image cat.png
"3-1"
redis> XGROUP CREATE jobs workers 0
OK
redis> XREADGROUP GROUP workers w1 COUNT 1 STREAMS jobs >
1) 1) "jobs"
   2) 1) 1) "1-1"
         2) 1) "task"
            2) "email"
            3) "to"
            4) "ada@example.com"
redis> XREADGROUP GROUP workers w2 COUNT 1 STREAMS jobs >
1) 1) "jobs"
   2) 1) 1) "2-1"
         2) 1) "task"
            2) "email"
            3) "to"
            4) "linus@example.com"
redis> XREADGROUP GROUP workers w1 COUNT 5 STREAMS jobs >
1) 1) "jobs"
   2) 1) 1) "3-1"
         2) 1) "task"
            2) "resize"
            3) "image"
            4) "cat.png"
redis> XREADGROUP GROUP workers w2 COUNT 5 STREAMS jobs >
(nil)

Worker w1 got entry 1-1, w2 got 2-1, then w1 asked for up to five and received the only remaining one. When nothing is left, the reply is (nil).

Acknowledging with XACK

A delivered entry stays in the group's pending list until the consumer confirms it with XACK. XPENDING stream group summarises the pending list: the count, the smallest and largest pending IDs, and how many each consumer holds.

redis · try it yourself
XADD jobs 1-1 task a
XADD jobs 2-1 task b
XADD jobs 3-1 task c
XGROUP CREATE jobs workers 0
XREADGROUP GROUP workers w1 COUNT 2 STREAMS jobs >
XREADGROUP GROUP workers w2 COUNT 1 STREAMS jobs >
XPENDING jobs workers
XACK jobs workers 1-1 3-1
XPENDING jobs workers
redis> XADD jobs 1-1 task a
"1-1"
redis> XADD jobs 2-1 task b
"2-1"
redis> XADD jobs 3-1 task c
"3-1"
redis> XGROUP CREATE jobs workers 0
OK
redis> XREADGROUP GROUP workers w1 COUNT 2 STREAMS jobs >
1) 1) "jobs"
   2) 1) 1) "1-1"
         2) 1) "task"
            2) "a"
      2) 1) "2-1"
         2) 1) "task"
            2) "b"
redis> XREADGROUP GROUP workers w2 COUNT 1 STREAMS jobs >
1) 1) "jobs"
   2) 1) 1) "3-1"
         2) 1) "task"
            2) "c"
redis> XPENDING jobs workers
1) (integer) 3
2) "1-1"
3) "3-1"
4) 1) 1) "w1"
      2) "2"
   2) 1) "w2"
      2) "1"
redis> XACK jobs workers 1-1 3-1
(integer) 2
redis> XPENDING jobs workers
1) (integer) 1
2) "2-1"
3) "2-1"
4) 1) 1) "w1"
      2) "1"

After the acknowledgements only 2-1, held by w1, is still pending. In a worker the rule is simple: process the job, then XACK it. If the worker dies before acknowledging, the entry is not lost.

Recovering after a crash

When a consumer restarts, it first reads its own pending entries by passing 0 instead of >. That returns what it received earlier but never acknowledged:

redis · try it yourself
XADD jobs 1-1 task a
XADD jobs 2-1 task b
XGROUP CREATE jobs workers 0
XREADGROUP GROUP workers w1 COUNT 2 STREAMS jobs >
XACK jobs workers 1-1
XREADGROUP GROUP workers w1 STREAMS jobs 0
XREADGROUP GROUP workers w2 STREAMS jobs 0
redis> XADD jobs 1-1 task a
"1-1"
redis> XADD jobs 2-1 task b
"2-1"
redis> XGROUP CREATE jobs workers 0
OK
redis> XREADGROUP GROUP workers w1 COUNT 2 STREAMS jobs >
1) 1) "jobs"
   2) 1) 1) "1-1"
         2) 1) "task"
            2) "a"
      2) 1) "2-1"
         2) 1) "task"
            2) "b"
redis> XACK jobs workers 1-1
(integer) 1
redis> XREADGROUP GROUP workers w1 STREAMS jobs 0
1) 1) "jobs"
   2) 1) 1) "2-1"
         2) 1) "task"
            2) "b"
redis> XREADGROUP GROUP workers w2 STREAMS jobs 0
1) 1) "jobs"
   2) (empty array)

w1 gets 2-1 back; w2 has nothing pending, so its list is empty.

If a consumer never comes back, another one can take over its old entries with XCLAIM (by ID) or XAUTOCLAIM (scan for entries idle longer than a minimum time). The examples use a minimum idle time of 0 to keep them repeatable; in real code you would use something like 60000 (one minute).

redis · try it yourself
XADD jobs 1-1 task a
XADD jobs 2-1 task b
XGROUP CREATE jobs workers 0
XREADGROUP GROUP workers dead-worker COUNT 2 STREAMS jobs >
XCLAIM jobs workers w2 0 1-1
XAUTOCLAIM jobs workers w3 0 0-0 COUNT 10
XPENDING jobs workers
redis> XADD jobs 1-1 task a
"1-1"
redis> XADD jobs 2-1 task b
"2-1"
redis> XGROUP CREATE jobs workers 0
OK
redis> XREADGROUP GROUP workers dead-worker COUNT 2 STREAMS jobs >
1) 1) "jobs"
   2) 1) 1) "1-1"
         2) 1) "task"
            2) "a"
      2) 1) "2-1"
         2) 1) "task"
            2) "b"
redis> XCLAIM jobs workers w2 0 1-1
1) 1) "1-1"
   2) 1) "task"
      2) "a"
redis> XAUTOCLAIM jobs workers w3 0 0-0 COUNT 10
1) "0-0"
2) 1) 1) "1-1"
      2) 1) "task"
         2) "a"
   2) 1) "2-1"
      2) 1) "task"
         2) "b"
3) (empty array)
redis> XPENDING jobs workers
1) (integer) 2
2) "1-1"
3) "2-1"
4) 1) 1) "w3"
      2) "2"

XAUTOCLAIM returns a cursor for the next call (0-0 means the scan is complete), the claimed entries, and a list of IDs that were deleted from the stream in the meantime. Because the minimum idle time is 0, w3 claimed both entries, including the one w2 had just taken over; with a real threshold, only entries that had been idle long enough would move.

Several groups on one stream

Each group keeps its own position, so one stream can feed several independent services. XINFO GROUPS shows every group with its consumers, pending count, last delivered ID and lag (entries not yet delivered):

redis · try it yourself
XADD signups 1-1 user ada
XADD signups 2-1 user linus
XGROUP CREATE signups welcome-email 0
XGROUP CREATE signups analytics 0
XREADGROUP GROUP welcome-email m1 COUNT 2 STREAMS signups >
XREADGROUP GROUP analytics a1 COUNT 1 STREAMS signups >
XINFO GROUPS signups
redis> XADD signups 1-1 user ada
"1-1"
redis> XADD signups 2-1 user linus
"2-1"
redis> XGROUP CREATE signups welcome-email 0
OK
redis> XGROUP CREATE signups analytics 0
OK
redis> XREADGROUP GROUP welcome-email m1 COUNT 2 STREAMS signups >
1) 1) "signups"
   2) 1) 1) "1-1"
         2) 1) "user"
            2) "ada"
      2) 1) "2-1"
         2) 1) "user"
            2) "linus"
redis> XREADGROUP GROUP analytics a1 COUNT 1 STREAMS signups >
1) 1) "signups"
   2) 1) 1) "1-1"
         2) 1) "user"
            2) "ada"
redis> XINFO GROUPS signups
1)  1) "name"
    2) "analytics"
    3) "consumers"
    4) (integer) 1
    5) "pending"
    6) (integer) 1
    7) "last-delivered-id"
    8) "1-1"
    9) "entries-read"
   10) (integer) 1
   11) "lag"
   12) (integer) 1
2)  1) "name"
    2) "welcome-email"
    3) "consumers"
    4) (integer) 1
    5) "pending"
    6) (integer) 2
    7) "last-delivered-id"
    8) "2-1"
    9) "entries-read"
   10) (integer) 2
   11) "lag"
   12) (integer) 0

The welcome-email group has received both signups while analytics is one entry behind.

A worker loop

Putting it together, a worker in Python with redis-py looks like this:

python
import redis

r = redis.Redis(decode_responses=True)
GROUP, ME = "workers", "worker-1"

# First recover our own unacknowledged entries, then switch to new ones.
last_id = "0"
while True:
    reply = r.xreadgroup(GROUP, ME, {"jobs": last_id}, count=10, block=5000)
    entries = reply[0][1] if reply else []
    if last_id == "0" and not entries:
        last_id = ">"                 # backlog done: read new entries from now on
        continue
    for entry_id, fields in entries:
        handle(fields)                # your job logic
        r.xack("jobs", GROUP, entry_id)

Tip: Entries that keep failing would be retried forever. Use the delivery counter (shown by the extended form of XPENDING) to move entries that failed too often to a separate "dead letter" stream.

Test yourself

In XREADGROUP ... STREAMS jobs >, what does > mean?

  1. Entries with a greater score
  2. Entries never delivered to any consumer of this group
  3. All entries from the beginning
  4. Only pending entries

What happens to a delivered entry that is never acknowledged?

  1. It is deleted after 60 seconds
  2. It stays in the group's pending entries list
  3. It goes back to the start of the stream
  4. It is delivered to all consumers

Two groups read the same stream. How many times is each entry delivered?

  1. Once in total
  2. Once per group
  3. Once per consumer
  4. Never