Skip to content
System Design Lab
Microservice Patterns

Competing Consumers

Let several identical workers pull from the same queue so throughput scales with the number of workers — and understand what you give up: ordering, and exactly-once processing.

Beginner 4 min read· Updated 2026-11-18

What you will learn

  • Scale message processing horizontally with workers that share one queue.
  • Explain why parallel workers break per-key ordering.
  • Understand visibility timeouts, redelivery and prefetch.
  • Configure competing consumers with Spring AMQP and keep ordering where it matters.

Before you start

Overview

A bank opens more counters when the line gets long. Customers take a ticket and go to whichever cashier is free. Nobody assigns customers to cashiers in advance, and a slow cashier simply serves fewer people.

With competing consumers, many instances of the same worker read from one queue. The broker hands each message to exactly one available worker. Add workers to process more messages per second; if a worker dies, its unacknowledged message is redelivered to another.

Run it

Scenario A cannot keep up; B scales to four workers. Look at Finished out of order (same key) in B — it is the price of parallelism. In C one worker gets stuck: its message reappears after the visibility timeout and someone else completes it.

Loading simulation…

Ordering: the main trade-off

Two messages for the same order — "created" then "cancelled" — can be picked up by two workers at the same time. If "cancelled" finishes first, the final state is wrong. Options, from simplest:

  • Make ordering irrelevant: idempotent, commutative updates or version checks (UPDATE … WHERE version < ?).
  • Partition by key: route all messages for a key to the same worker (Kafka partitions, RabbitMQ consistent-hash exchange, SQS FIFO message groups). Parallel across keys, ordered within a key.
  • Single active consumer for queues that must be strictly ordered — which gives up the scaling.

Implementation with Spring AMQP

ThumbnailWorker.java
@Component
class ThumbnailWorker {

    private final ThumbnailService thumbnails;

    ThumbnailWorker(ThumbnailService thumbnails) {
        this.thumbnails = thumbnails;
    }

    // 4 consumers per instance; run N instances for N × 4 competing consumers.
    @RabbitListener(queues = "thumbnail-jobs", concurrency = "4")
    public void handle(ThumbnailJob job) {
        thumbnails.render(job.imageId(), job.size());   // idempotent: redelivery is normal
    }
}

// application.yml
// spring.rabbitmq.listener.simple.prefetch: 1        # fair dispatch: a slow job does not hoard messages
// spring.rabbitmq.listener.simple.acknowledge-mode: auto

Advanced Prefetch is the number of unacknowledged messages a consumer may hold. A high prefetch improves throughput for tiny, uniform jobs but lets one slow worker hoard messages others could process; prefetch 1 gives the fairest dispatch for long or variable jobs. In SQS the equivalent knobs are batch size and the visibility timeout, which must exceed the worst-case processing time.

Trade-offs

BenefitsCosts
ThroughputScales roughly linearly with workers.Until a shared downstream (database, API) becomes the bottleneck.
ResilienceA dead worker's messages are redelivered to others.Redelivery means duplicates: handlers must be idempotent.
OrderingFine for independent jobs.Per-key order is lost unless you partition by key.

Production considerations

  • Autoscale workers on queue depth or the age of the oldest message, with a maximum that downstream systems can handle.
  • Set the visibility timeout (or consumer timeout) above the p99 processing time, and heartbeat/extend it for long jobs.
  • Use a Dead Letter Queue so a poison message does not bounce between workers forever.

Further reading

Related topics