Redis Streams Consumer Groups as a Single-Instance Task Queue
Run an at-least-once task queue with Redis Streams consumer groups on one instance—acknowledge work, reclaim crashed messages, and watch trimming risks.
19 Oct 2025, 04:02 UTC

The problem: a crashed worker should not lose a task
You have a web application that enqueues jobs—send an email, generate a report, process an upload—and a small pool of workers that pull jobs from Redis. If you use a plain list with LPOP, a worker that crashes after popping but before finishing deletes the job silently. Adding a dedicated broker such as RabbitMQ or Kafka may be overkill when Redis is already in your stack.
Redis Streams, available since Redis 5.0, provide an append-only log plus consumer groups. A consumer group tracks which messages have been delivered but not yet acknowledged, giving you at-least-once delivery on a single Redis instance without running a separate queueing service.
How a consumer group works as a queue
A stream is an append-only log. Producers add entries with XADD. A consumer group is a named set of consumers that share one stream. The group remembers the last ID it delivered and keeps a Pending Entries List (PEL): entries delivered to a consumer but not yet acknowledged.
The typical flow is:
- A producer appends a job with XADD.
- A consumer calls XREADGROUP to claim new messages.
- The consumer processes the job.
- The consumer calls XACK to remove the entry from the PEL.
- If the consumer crashes before XACK, another consumer can claim the entry with XCLAIM after an idle period.
Because XCLAIM can deliver the same entry to a different consumer, your handler must be idempotent or deduplicate by stream entry ID.
Worked example: add, claim, acknowledge, and reclaim a task
Run these commands against a single Redis instance, version 5.0 or later, using redis-cli or a Redis client in your application. If your instance uses ACLs, the connecting user needs permissions for XADD, XREADGROUP, XACK, and XPENDING. Replace the placeholders with your own stream, group, and consumer names.
# Append a job to the stream
XADD taskstream * type=email to=[contact removed] template=welcome
# Create the consumer group, starting at the beginning of the stream
XGROUP CREATE taskstream taskgroup 0 MKSTREAM
# Consumer A reads one new message; blocks up to two seconds if the stream is empty
XREADGROUP GROUP taskgroup consumerA COUNT 1 BLOCK 2000 STREAMS taskstream >
# List unacknowledged entries for this group
XPENDING taskstream taskgroup
# After consumer A finishes the job, acknowledge it
XACK taskstream taskgroup
# If consumer A crashed before acknowledging, consumer B can claim it after 60 seconds idle
XCLAIM taskstream taskgroup consumerB 60000
Expected checks: XREADGROUP returns the stream entry with its ID and fields. XPENDING lists the entry under consumerA until XACK runs. After XACK, XPENDING no longer lists that entry. If you run XCLAIM after the idle timeout, the entry appears under consumerB instead.
Relevant risks: XCLAIM does not know whether consumerA is truly dead or merely slow. If consumerA is still processing, both consumers may handle the same entry. XTRIM MAXLEN can evict entries that are still in a PEL, which causes silent data loss. Check pending counts before trimming, or avoid trimming streams that hold unacknowledged work.
Trade-offs and limits to verify
- Duplicates are expected. At-least-once delivery means a task can run more than once. Make handlers idempotent or record processed entry IDs.
- Aggressive trimming can delete unacknowledged work. XTRIM MAXLEN removes old entries even if a consumer group still references them in its PEL.
- This is single-instance behavior. In Redis Cluster, all entries for a stream key hash to one slot, so one stream and its consumer groups do not spread across nodes.
- Durability depends on your topology. Stream entries and consumer-group state follow Redis persistence and replication settings. Confirm failover behavior with INFO replication and CLUSTER INFO before routing production traffic.
- Blocking reads hold connections. XREADGROUP with BLOCK occupies a connection until a message arrives or the timeout expires. Size your connection pool accordingly.
Verify before you rely on it
In a staging instance running the same Redis version as production, add a message, read it with one consumer, stop that consumer before acknowledging, wait past your claim idle time, and run XCLAIM from another consumer. Confirm the entry moves to the new consumer's PEL. Then run XPENDING and XACK in your normal flow to confirm acknowledged entries disappear. These checks validate acknowledgment and reclaim behavior for your specific version and configuration.
Closing
Redis Streams consumer groups are a practical choice when you already operate Redis and need a straightforward, reliable queue on a single instance. Treat every delivery as potentially repeated, guard against aggressive trimming, and verify acknowledgment and failover behavior before sending production tasks through it.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.