Redis XPENDING in Ruby (Detailed Guide w/ Code Examples)

Use Case(s)

XPENDING command in Redis is used when working with streams and consumer groups. It provides details of pending messages for a given stream, which are messages delivered but not yet acknowledged by consumers.

  1. Monitoring the status of a message queue.
  2. Debugging or tracking down unacknowledged messages.
  3. Implementing custom message reprocessing logic in case of consumer failures.

Code Examples

First of all, you need to have a redis-client for ruby installed. If not, install it using:

gem install redis

Here is an example of how we use XPENDING:

require 'redis' redis = "localhost", port: 6379) # Add a message to stream and also create a group. redis.xadd("mystream", {"field" => "message"}) redis.xgroup('CREATE', 'mystream', 'mygroup', '$', mkstream: true) # Read and acknowledge one message from the stream. id, message = redis.xreadgroup('GROUP', 'mygroup', 'consumer1', 'STREAMS', 'mystream', '>').first[1][0] # XPENDING without any additional arguments just returns the number of pending messages. puts redis.xpending('mystream', 'mygroup')

In the above example, the xpending command will return 0 as the message has been acknowledged. If we comment out the XACK line, it would return 1 signaling there's one unacknowledged message in the stream.

Best Practices

  1. Always acknowledge a message after processing. Not doing so will leave messages as pending indefinitely which may lead to memory issues.
  2. Use XPENDING judiciously as it may have performance implications if there are a large number of pending messages.

Common Mistakes

  1. Misunderstanding the usage of XPENDING and XCLAIM: XPENDING is for viewing pending messages, while XCLAIM is used to change the ownership of a message.
  2. Not periodically reviewing the XPENDING messages, leading to a pile-up of unacknowledged messages, which can potentially cause memory issues.


Q: What information does XPENDING provide?

A: XPENDING command gives you the number of messages yet to be acknowledged, and detailed information about every pending message id, including the owner, the last time it was delivered, how many times it was delivered.

Q: How do I acknowledge a message in a Redis stream?

A: You use the XACK command to acknowledge a message. The syntax is: XACK key group ID.

Was this content helpful?

Start building today

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.