Home > Net >  Understanding xreadgroup command
Understanding xreadgroup command

Time:11-10

I have written redis stream consumer in python and the minimal code looks as below:

key = 'order_completed'
group = 'inventory_group'

redis.xgroup_create(key, group)  #statement1

result = redis.xreadgroup(group, key, {key: '>'}, None) #statement2

I need help to understand the last two statements.

My limited understanding is that redis.xgroup_create(key, group) is creating a consumer group.

And, redis.xreadgroup(group, key, {key: '>'}, None) is used to read latest entry in the stream.

What is {key: '>'}?

I have already read xreadgroup but not able to understand much from it.

CodePudding user response:

The > special stream entry ID makes XREADGROUP to return only the stream entries which were never delivered to any consumer in the group - basically, it will return new entries only.

Apart from >, the command accepts any other regular stream entry ID: in that case, XREADGROUP returns the stream entries already delivered to the calling consumer, whose IDs are greater than the passed value.

From the command documentation:

The ID to specify in the STREAMS option when using XREADGROUP can be one of the following two:

  • The special > ID, which means that the consumer want to receive only messages that were never delivered to any other consumer. It just means, give me new messages.
  • Any other ID, that is, 0 or any other valid ID or incomplete ID (just the millisecond time part), will have the effect of returning entries that are pending for the consumer sending the command with IDs greater than the one provided. So basically if the ID is not >, then the command will just let the client access its pending entries: messages delivered to it, but not yet acknowledged. Note that in this case, both BLOCK and NOACK are ignored.
  • Related