Redis Stream Message Claiming#
When working with Redis Stream Consumer Groups, there may be situations where messages remain in a pending state because a consumer failed to process them. FastStream provides a mechanism to automatically claim these pending messages using Redis's XAUTOCLAIM command through the min_idle_time parameter.
What is Message Claiming?#
In Redis Streams, when a consumer reads a message from a consumer group but fails to acknowledge it (due to a crash, network issue, or processing error), the message remains in the Pending Entries List (PEL) of that consumer group. These unacknowledged messages are associated with the original consumer and have an "idle time" - the duration since they were last delivered.
Message claiming allows another consumer to take ownership of these pending messages that have been idle for too long, ensuring that messages don't get stuck and workload can be redistributed among healthy consumers.
Using min_idle_time for Automatic Claiming#
FastStream's StreamSub provides a min_idle_time parameter that enables automatic claiming of pending messages via Redis's XAUTOCLAIM command. When set, the consumer will automatically scan for and claim messages that have been pending for at least the specified duration (in milliseconds).
Basic Example#
Here's a simple example that demonstrates automatic message claiming:
How It Works#
When min_idle_time is set:
- Circular Scanning: Instead of using
XREADGROUPto read new messages, the consumer usesXAUTOCLAIMto scan the Pending Entries List - Idle Time Check: Only messages that have been pending for at least
min_idle_timemilliseconds are claimed - Ownership Transfer: Claimed messages are automatically transferred from the failing consumer to the claiming consumer
- Continuous Processing: The scanning process is circular - after reaching the end of the PEL, it starts over from the beginning
Practical Use Case#
Consider a scenario where you have multiple workers processing orders:
Combining with Manual Acknowledgment#
You can combine min_idle_time with manual acknowledgment policies for fine-grained control:
Configuration Guidelines#
Choosing min_idle_time#
The appropriate min_idle_time value depends on your use case:
- Short duration (1-5 seconds): For fast-processing tasks where quick failure recovery is needed
- Medium duration (10-60 seconds): For most general-purpose applications with moderate processing times
- Long duration (5-30 minutes): For long-running tasks where you want to ensure a consumer has truly failed
Warning
Setting min_idle_time too low may cause messages to be unnecessarily transferred between healthy consumers. Set it based on your typical message processing time plus a safety buffer.
Deployment Patterns#
Pattern 1: Dedicated Claiming Worker#
Deploy a separate worker specifically for claiming abandoned messages:
# Main workers (fast path)
@broker.subscriber(
stream=StreamSub("tasks", group="workers", consumer="main-1")
)
async def main_worker(task): ...
# Claiming worker (recovery path)
@broker.subscriber(
stream=StreamSub("tasks", group="workers", consumer="claimer", min_idle_time=15000)
)
async def claiming_worker(task): ...
Pattern 2: All Workers Can Claim#
All workers can claim messages from each other:
# Each worker can both process new messages and claim abandoned ones
@broker.subscriber(
stream=StreamSub(
"tasks",
group="workers",
consumer=f"worker-{instance_id}",
min_idle_time=10000,
)
)
async def worker(task): ...
Technical Details#
- Start ID: FastStream automatically manages the
start_idparameter forXAUTOCLAIM, enabling circular scanning through the Pending Entries List - Empty Results: When no pending messages meet the idle time criteria, the consumer will continue polling
- ACK Handling: Claimed messages must still be acknowledged using
msg.ack()to be removed from the PEL
References#
For more information about Redis Streams message claiming: