Redis XAUTOCLAIM in Java (Detailed Guide w/ Code Examples)

Use Case(s)

  • Stream Processing: When you have a distributed system where multiple consumers are consuming messages from the same stream, XAUTOCLAIM helps in ensuring that if a message isn't acknowledged by a consumer within a specific period of time, another consumer can claim it and process it.
  • Reliable Queue: XAUTOCLAIM can be used to implement a reliable queue system, where unprocessed messages are not lost but handed off to another worker for processing.

Code Examples

Using Jedis, a popular Java client for Redis:

import redis.clients.jedis.Jedis; import redis.clients.jedis.StreamEntryID; public class Main { public static void main(String[] args) { try (Jedis jedis = new Jedis("localhost", 6379)) { String key = "mystream"; String group = "mygroup"; StreamEntryID lastId = null; int idleTime = 10000; // 10 seconds int count = 10; // Consumer claims pending messages or waits for new ones List<Entry<String, List<StreamEntry>>> entries = jedis.xautoClaim(key, group, "consumer1", idleTime, lastId, count, false); for (Entry<String, List<StreamEntry>> entry : entries) { System.out.println("List ID: " + entry.getKey()); for (StreamEntry streamEntry : entry.getValue()) { System.out.println("Stream ID: " + streamEntry.getID()); System.out.println("Fields: " + streamEntry.getFields()); } } } } }

In this example, we're using XAUTOCLAIM to claim and process messages from a stream that are older than the specified idle time (10 seconds).

Best Practices

  • Handling Failures: Always design your system to handle failures. If a consumer dies before acknowledging a message, it will be stuck in a pending state until XAUTOCLAIM or XPENDING is used to reclaim it.

Common Mistakes

  • Ignoring Message ID: When you claim messages with XAUTOCLAIM, Redis returns messages along with their IDs. Be careful not to ignore these IDs because you'll need them to acknowledge the processing of each message.


  1. What's the difference between XREADGROUP and XAUTOCLAIM?

    • XREADGROUP reads new messages from a stream for a particular group. XAUTOCLAIM, on the other hand, is designed to reclaim messages that are pending (i.e., unacknowledged by other consumers).
  2. Can I use XAUTOCLAIM without creating a group first?

    • No, you can't. You must first create a consumer group using XGROUP CREATE before using XAUTOCLAIM.

Was this content helpful?

Start building today

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