Competing Consumers
Let several identical workers read from the same queue, each message going to exactly one of them, so processing scales out simply by adding workers.
Description
Many workers listen on one queue. The broker hands each message to one worker at a time; the others get different messages. When a worker finishes, it acknowledges the message and gets the next one. If it crashes mid-job, the message becomes visible again and another worker picks it up.
Throughput scales with the number of workers, and many platforms scale workers automatically on queue depth. It turns a pile of jobs (resize images, send emails, generate invoices) into parallel work without any coordination code.
System design
Competing Consumers system design diagram
- Upload service (service).
- Resize jobs (queue).
- Worker 1 (function).
- Worker 2 (function).
- Worker 3 (function).
- Thumbnails (object storage).
- Step 1: Upload service → Resize jobs: one job per image.
- Step 2: Resize jobs → Worker 1: job A.
- Step 3: Resize jobs → Worker 2: job B.
- Step 4: Resize jobs → Worker 3: job C.
- Step 5: Worker 2 → Thumbnails: save, then ack.
- Every uploaded image becomes one resize job on the queue.
- Worker 1 receives job A. While it holds the job, no other worker sees it.
- Worker 2 gets job B at the same time.
- Worker 3 gets job C. When the queue grows, more workers are started automatically.
- When a worker finishes, it saves the thumbnail and acknowledges the job. If it crashes first, the job reappears on the queue for another worker.
- AWS
- Amazon SQS with Lambda event source mappings or ECS services scaling on queue depth.
- Azure
- Service Bus queues with Azure Functions or KEDA-scaled Container Apps.
- Google Cloud
- Pub/Sub pull subscriptions or Cloud Tasks with Cloud Run.
When to use it
Use it when
- You have many independent jobs that can be processed in any order.
- Workload varies, and you want to add or remove workers to match it.
- Jobs must not be lost if a worker crashes.
Avoid it when
- Messages must be processed strictly in order. Use ordered partitions or keys, accepting less parallelism.
- Jobs depend on each other's results. You need a workflow, not a pool.
- Each message must reach every consumer. That's Publisher–Subscriber.
Pitfalls
- Duplicate processing: a slow worker's message times out and goes to another worker. Make handlers idempotent and set visibility timeouts longer than the slowest job.
- Poison messages retried forever. Use a maximum receive count and a dead-letter queue.
- Workers overwhelming a shared downstream (a database) when they scale out. Cap concurrency to what the downstream can take.
Alternatives
Publisher–Subscriber
Choose it when every subscriber needs its own copy of each message.
Queue-Based Load Leveling
Choose it when the main goal is smoothing spikes for one steady consumer.
Batch jobs
Choose it when work arrives in big scheduled chunks, such as a nightly import; a batch framework may be simpler.