redisredis-streams

Redis streams: groups/consumers naming and cleanup


I have a rather primitive use-case for streams: multiple producers and 1 consumer that periodically process messages in batches. Sometimes there will be more than 1 consumer and this is why I want to switch from "traditional" unreliable queue (rpush/lrange/ltrim) to streams.

There're actually 2 related questions:

  1. When using Redis consumer groups I have to specify group name and consumer name. Is this good idea to use some hard-coded string as group's name and some random string as consumer name? Consumer is just 1 process and its name will be different on each start in this case. On some rare occasions there will be 2 or more consumers at a time.

  2. Consumer group has to be created via xgroup create before I can xreadgroup from it. Each time a new consumer starts (with a random name) it gets added to a list of consumers, but it remains there even after the process died. My understanding that in this case I need to re-assign all pending messages from the dead consumers to the active one, and then delete old consumers. Is this correct?


Solution

    1. Yep, that's the right approach.
    2. Again correct. The basic logic here is to call XPENDING followed by XCLAIM (and XGROUP DELCONSUMER finally). Note that the upcoming Redis 6.2 (currently RC2) makes this easier with the new XAUTOCLAIM command and the addition of XGROUP CREATECONSUMER.