Redis Streams provides a reliable way to build event-driven applications, background workers, and message-processing systems. Applications can publish events to a stream, while multiple consumers process those events independently using consumer groups.
However, a consumer may stop unexpectedly while processing a message. The application might crash, lose its network connection, restart, or experience a timeout. When this happens, a message can remain unprocessed even though Redis has already delivered it to a consumer.
Redis Streams provides mechanisms to track these messages and transfer them to healthy consumers. Commands such as XREADGROUP, XACK, XPENDING, XCLAIM, and XAUTOCLAIM help applications manage message delivery and recovery.
In this article, we will explore how Redis tracks unprocessed messages, how to identify failed consumers, and how to recover pending messages without losing control of the processing workflow.
Understanding Redis Streams and Consumer Groups
Redis Streams is a data structure designed for storing and processing sequences of events. Each entry has a unique ID and contains one or more field-value pairs.
For example, an order-processing application can publish events to a stream named orders:
XADD orders * orderId 1001 customerId 501 status Created
XADD orders * orderId 1002 customerId 502 status Created
XADD orders * orderId 1003 customerId 503 status Created
Redis generates an ID for each entry. The ID contains a timestamp component and a sequence number.
A consumer can read entries directly from a stream, but applications that need multiple workers sharing the processing workload should use consumer groups.
A consumer group allows multiple consumers to process different messages from the same stream. Redis tracks which consumer receives each message and whether that message has been acknowledged.
For example:
orders stream
|
v
order-processors group
|
+---- consumer-1
|
+---- consumer-2
|
+---- consumer-3
Each consumer processes its assigned messages. If a consumer fails before acknowledging a message, Redis keeps track of that message so another consumer can recover it.
Creating a Redis Consumer Group
Before reading messages through a consumer group, create the group using XGROUP CREATE.
XGROUP CREATE orders order-processors 0 MKSTREAM
The command creates a consumer group named order-processors for the orders stream.
The arguments have the following meanings:
- orders identifies the stream.
- order-processors identifies the consumer group.
- 0 starts reading from the beginning of the existing stream.
- MKSTREAM creates the stream if it does not already exist.
If the application should process only messages published after the group is created, use $ instead of 0.
XGROUP CREATE orders order-processors $ MKSTREAM
Choose the starting position carefully. Starting at 0 allows the group to process existing entries, while $ starts from new entries arriving after group creation.
Reading Messages With XREADGROUP
Consumers use XREADGROUP to receive messages from a consumer group.
For example, consumer-1 can read messages using:
XREADGROUP GROUP order-processors consumer-1 COUNT 5 STREAMS orders >
The > identifier tells Redis to return messages that have not previously been delivered to another consumer in this group.
When Redis delivers a message through XREADGROUP, it records the message in the group's Pending Entries List, commonly called the PEL.
The PEL contains messages that have been delivered but not yet acknowledged.
This behavior is important for recovery. Redis does not consider a message successfully processed simply because a consumer received it.
The consumer must explicitly acknowledge successful processing.
Understanding Message Acknowledgment With XACK
After successfully processing a message, the consumer should acknowledge it using XACK.
For example:
XACK orders order-processors 1733130000000-0
The command identifies the stream, consumer group, and message ID.
Once acknowledged, the message is removed from the group's pending entries list.
The stream entry itself is not deleted. XACK only removes its pending status for the specified consumer group.
This distinction matters when several consumer groups process the same stream. Acknowledging a message in one group does not acknowledge it in another group.
A typical processing sequence looks like this:
- The consumer reads a message using XREADGROUP.
- The application performs the required business operation.
- The application acknowledges the message using XACK.
- Redis removes the message from the pending entries list.
The acknowledgment should happen after successful processing, not immediately after receiving the message.
What Happens When a Consumer Fails?
Consider an order-processing application with two consumers.
Consumer-1 receives an order message and starts processing it. Before the processing finishes, the application crashes.
Because the consumer did not acknowledge the message, Redis keeps the message in the pending entries list.
The message is not automatically delivered to another consumer just because consumer-1 has stopped running.
This can cause processing delays if the application does not implement a recovery mechanism.
For example, the following command shows the pending messages in a consumer group:
XPENDING orders order-processors
The command returns information such as:
- Total number of pending messages.
- Lowest pending message ID.
- Highest pending message ID.
- Number of pending messages assigned to each consumer.
This information helps administrators identify consumers that have stopped processing messages successfully.
Inspecting Pending Messages With XPENDING
The summary form of XPENDING provides an overview of pending messages.
For more detailed information, specify a message ID range and a result count.
XPENDING orders order-processors - + 10
The command returns details for up to ten pending messages.
The extended response includes:
| Field | Description |
|---|
| Message ID | Unique identifier of the stream entry |
| Consumer | Consumer currently responsible for the message |
| Idle time | Time elapsed since the message was last delivered or claimed |
| Delivery count | Number of times the message has been delivered |
The idle time is particularly useful for recovery.
A message that has been pending for several minutes may belong to a consumer that has crashed or become unresponsive.
However, a long idle time does not always mean that a consumer has failed. The consumer might be processing a slow operation or waiting for an external dependency.
Recovery logic should therefore use an appropriate idle threshold based on the expected processing duration.
Recovering Messages With XCLAIM
Redis provides XCLAIM to transfer ownership of a pending message from one consumer to another.
For example:
XCLAIM orders order-processors consumer-2 60000 1733130000000-0
The arguments specify:
- orders: the stream name.
- order-processors: the consumer group.
- consumer-2: the new consumer.
- 60000: the minimum idle time in milliseconds.
- The final argument: the message ID to claim.
Redis transfers ownership only if the message has been idle for at least the specified duration.
After claiming the message, consumer-2 becomes responsible for processing it.
The new consumer can then process the message and acknowledge it:
XACK orders order-processors 1733130000000-0
XCLAIM is useful when the application already knows which message needs recovery.
For example, an administrator may inspect pending entries using XPENDING and manually claim selected messages.
For automated recovery across many pending messages, XAUTOCLAIM is generally more convenient.
Automatically Recovering Messages With XAUTOCLAIM
XAUTOCLAIM was introduced in Redis 6.2. It simplifies the process of finding idle pending messages and transferring them to another consumer.
Instead of manually identifying each message with XPENDING and claiming it with XCLAIM, the application can use one command.
XAUTOCLAIM orders order-processors consumer-2 60000 0-0 COUNT 10
This command asks Redis to:
- Examine pending messages in the orders stream.
- Look for messages that have been idle for at least 60 seconds.
- Transfer eligible messages to consumer-2.
- Return up to the configured number of entries it attempts to claim.
The command also returns a cursor that can be used to continue scanning the pending entries list.
For example, a response may contain a next cursor and a collection of claimed messages.
1) "1733130000000-0"
2) 1) 1) "1733129990000-0"
2) 1) "orderId"
2) "1001"
3) "status"
4) "Created"
The first value is the cursor for the next scan. The second value contains the claimed messages.
An application should continue scanning using the returned cursor until it reaches 0-0, which indicates that the scan has completed.
For example:
XAUTOCLAIM orders order-processors consumer-2 60000 0-0 COUNT 10
XAUTOCLAIM orders order-processors consumer-2 60000 1733130000000-0 COUNT 10
The second command uses the cursor returned by the first command. In an application, this cursor should be stored and passed to the next iteration rather than hardcoded.
XAUTOCLAIM is especially useful for background recovery workers that periodically inspect and recover messages left by failed consumers.
Implementing a Recovery Worker
A production application should separate normal message consumption from recovery responsibilities.
Normal consumers read new messages using XREADGROUP with >. A recovery worker periodically scans the pending entries list and claims messages that have exceeded the configured idle threshold.
A simplified recovery workflow looks like this:
Recovery Worker
|
v
Read Pending Messages
|
v
Find Messages Above Idle Threshold
|
v
Claim Messages
|
v
Process Messages
|
v
Successful?
/ \
Yes No
| |
XACK Retry or
Record Failure
The recovery worker should not claim every pending message immediately. A message may still be actively processed by its original consumer.
For example, if an order-processing operation normally takes up to 20 seconds, a recovery threshold of five seconds could cause another consumer to claim the message while the original consumer is still working.
This can result in duplicate processing.
Choose the idle threshold based on the expected processing time, timeout configuration, and failure-detection requirements.
Preventing Duplicate Processing
Redis Streams consumer groups provide at-least-once delivery behavior when messages are acknowledged after successful processing.
At-least-once delivery means a message can be delivered more than once if a consumer fails at the wrong time.
Consider this scenario:
- Consumer-1 receives an order message.
- Consumer-1 successfully updates the order in the database.
- Consumer-1 crashes before sending XACK.
- A recovery worker claims the pending message.
- Consumer-2 processes the same order again.
The database operation may execute twice unless the application protects against duplicate processing.
One common solution is idempotent message handling.
For example, an application can maintain a processed-message table containing unique message identifiers.
CREATE TABLE dbo.ProcessedMessages
(
MessageId VARCHAR(100) PRIMARY KEY,
ProcessedAt DATETIME2 NOT NULL
);
Before processing an event, the application checks whether the message ID has already been recorded.
The business update and insertion of the processed-message record should happen in the same database transaction.
If the message has already been processed, the consumer can avoid repeating the business operation and acknowledge the Redis message.
This pattern is especially important for payment processing, order fulfillment, inventory updates, and other operations where duplicate execution can cause financial or business problems.
Handling Repeated Processing Failures
Not every recovered message will succeed on the next attempt.
A message may repeatedly fail because of invalid data, unavailable services, database constraints, or application bugs.
Applications should avoid retrying the same message indefinitely without recording its failure history.
Redis provides delivery-count information through XPENDING. This information can help identify messages that have been delivered multiple times.
For example:
XPENDING orders order-processors - + 20
The application can inspect the delivery count and apply a retry policy.
A practical retry strategy may include:
- Retry temporary network failures.
- Use controlled delays between attempts.
- Limit repeated processing attempts.
- Record the failure reason.
- Move permanently failing messages to a dead-letter stream.
- Alert the operations team when the failure threshold is reached.
A dead-letter stream can be created using another Redis Stream.
For example:
XADD orders:dead-letter * originalMessageId 1733130000000-0 orderId 1001 error "Invalid order data"
In a real application, preserve the original message fields, failure information, retry count, and timestamps.
The original message should be acknowledged only after the dead-letter record has been successfully written. When both operations must be atomic, use an appropriate Redis transaction or Lua script, taking care to handle command results and failure conditions correctly.
Monitoring Consumer Health
Message recovery should be supported by monitoring.
Redis provides XINFO CONSUMERS to inspect consumer information within a group.
XINFO CONSUMERS orders order-processors
This command provides information such as consumer names, pending message counts, and idle information.
Administrators can also inspect the group:
XINFO GROUPS orders
Useful monitoring metrics include:
- Total pending messages.
- Number of messages waiting beyond the recovery threshold.
- Pending messages per consumer.
- Message delivery counts.
- Number of successfully acknowledged messages.
- Recovery attempts.
- Dead-letter stream size.
- Message processing duration.
Monitoring these values helps identify failed workers, slow consumers, and growing processing backlogs.
When using Redis 7.2 or later, note that XINFO CONSUMERS reports both idle and inactive information. The meaning of the idle field changed in Redis 7.2, so monitoring systems should interpret these fields according to the server version.
Common Mistakes to Avoid
Acknowledging Messages Before Processing
If a consumer acknowledges a message before completing the business operation, a failure can result in lost work.
Acknowledge only after successful processing.
Using an Idle Threshold That Is Too Small
A recovery worker may claim messages that are still being processed by healthy consumers.
Set the threshold based on actual processing duration and expected application delays.
Assuming XCLAIM Prevents Duplicate Processing
Claiming a message changes its ownership in the consumer group. It does not guarantee that the previous consumer has stopped executing its business operation.
Use idempotent processing to protect against duplicate execution.
Ignoring the XAUTOCLAIM Cursor
XAUTOCLAIM returns a cursor for continuing the scan. Repeatedly starting from 0-0 without maintaining progress can cause inefficient recovery scans.
Use the returned cursor to continue scanning.
Deleting Stream Entries Too Early
Removing stream entries does not replace proper acknowledgment. Manage stream retention separately and ensure that entries required by pending consumers are not removed prematurely.
Ignoring Poison Messages
A message that always fails can consume resources and delay normal processing.
Use retry limits, failure tracking, and a dead-letter strategy.
Best Practices for Production Redis Streams
- Use consumer groups when multiple workers should share a stream's workload.
- Assign unique consumer names to running consumer instances.
- Use XACK only after successful processing.
- Monitor pending entries with XPENDING.
- Use XAUTOCLAIM for automated recovery of sufficiently idle messages.
- Choose recovery thresholds based on real processing durations.
- Implement idempotent business operations.
- Track delivery counts and repeated failures.
- Use a dead-letter stream for messages that exceed the retry policy.
- Monitor consumer activity and pending message growth.
- Use appropriate stream retention policies.
- Test consumer crashes, network interruptions, and application restarts before production deployment.
Advantages and Disadvantages
Advantages
- Redis tracks messages that have been delivered but not acknowledged.
- Failed consumers do not automatically make pending messages disappear.
- XPENDING helps identify unprocessed messages.
- XCLAIM allows targeted message recovery.
- XAUTOCLAIM simplifies automated recovery.
- Consumer groups support distributed message processing.
- Recovery can be integrated into background workers.
Disadvantages
- Applications must implement recovery and retry policies.
- At-least-once delivery can result in duplicate processing.
- Incorrect idle thresholds can cause concurrent processing.
- Pending entries require monitoring and management.
- Dead-letter handling and idempotency require additional application logic.
- Recovery behavior depends on stream retention and consumer group configuration.
Conclusion
Redis Streams provides useful mechanisms for recovering messages when consumers fail during processing. Consumer groups maintain a pending entries list that tracks messages delivered but not yet acknowledged.
Commands such as XPENDING help identify unprocessed messages, while XCLAIM and XAUTOCLAIM allow healthy consumers to take ownership and continue processing them.
However, message recovery alone does not guarantee exactly-once business execution. Applications must account for duplicate delivery through idempotent processing, controlled retries, and dead-letter handling.
By combining consumer groups, acknowledgment, pending-message monitoring, and automated recovery, developers can build more resilient Redis-based messaging systems that continue processing work even when individual consumers fail.
Join the conversation! Your thoughts help the community grow.