bigonotes
System DesignMessaging & Coordination

Queues

How message queues buffer bursts and spread work, plus ordering, retries, dead letter queues, partitions and backpressure.

5 min readLast updated

A queue sits between the part of a system that creates work (the producer) and the part that does it (the consumers or workers). The producer drops a message on the queue and moves on. Workers pull messages off and process them at their own pace. Queues let you absorb traffic spikes without dropping requests, and let you scale producers and consumers separately.

How it works#

Producers add messages at one end and workers take them from the other. The diagram shows the full path, including the two failure exits covered later in this note.

Producers push messages onto a queue; a pool of workers pulls from it; messages that keep failing move to a dead letter queue; when the queue is full, producers get pushed back
  • A message can be anything: a short string, a JSON job description, an event.
  • The queue stores each message durably until a worker acknowledges that it has processed it.
  • Producers and consumers don't know about each other. You can restart, deploy or scale the workers with little impact on producers.

Smoothing load. Because the queue holds messages until a worker is free, it can absorb a burst. Say a spike of 1,000 requests arrives but your workers can process 200 per second.

Without a queue, 800 requests fail. With a queue, they wait a few seconds and then get processed. Nothing is dropped.

Two common uses#

Most queues in a design do one of two jobs.

  1. Buffer bursty traffic. A ride-sharing app sees huge spikes at rush hour or after a concert. Ride requests go on a queue, and the matching service works through them at a steady rate instead of falling over.
  2. Distribute work. A photo service puts each "apply this filter" job on a queue. A pool of workers pulls jobs, so the expensive image processing is spread evenly across machines.

What you need to know#

Once a queue is in your design, five concepts come up again and again.

Concept What it means
Ordering Many queues are roughly first in, first out. Kafka guarantees order only within a partition, so related messages must share a partition key. Some managed queues offer strict FIFO as an option with lower throughput.
Retries If a worker fails or doesn't acknowledge in time, the message is delivered again. You can usually configure the number of attempts and the delay between them. Because of retries, workers should be idempotent: processing a message twice must be safe.
Dead letter queue (DLQ) After too many failed attempts, a message moves to a separate queue. You can inspect it, fix the bug and replay it, instead of having it block or vanish.
Partitions A queue can be split across servers to handle more messages. Each partition is consumed by its own worker or set of workers. As with database sharding, you choose a partition key so related messages land together.
Backpressure A way to slow producers down when consumers can't keep up. See below.

Backpressure: the queue can hide a capacity problem#

The buffering that makes queues useful also makes it easy to overload a system without noticing.

Picture a kitchen that can cook 200 meals an hour while orders arrive at 300 an hour. If you keep accepting every order, the pile of pending orders grows forever.

Wait times climb until the whole thing collapses. The queue didn't fix the problem. It hid it.

Backpressure pushes back on the producer when you're overloaded:

  • When the queue is full, reject new requests with an error such as 429 Too Many Requests, or
  • Slow down how fast you accept new messages.

The producer now knows to back off, so the problem is visible and handled at the source.

Here's the same overload both ways, with workers that handle 200/s.

Without backpressure:

  1. The producer sends 300/s.
  2. The queue grows without limit.
  3. Latency explodes or the system crashes.

With backpressure:

  1. The producer sends 300/s.
  2. The queue fills up.
  3. 100/s are rejected.
  4. The producer slows down.
  5. The system is stable at 200/s.

A highway on-ramp light is a good mental model. When the highway is congested, the light stops cars from entering. Drivers wait a bit, but the highway keeps moving.

Example. A pipeline processes thousands of events per second and calls a slow downstream service (say, a machine learning model). If that service slows down and nothing pushes back, the queue balloons with work that can't be finished in time. A good design watches queue depth and either throttles ingestion or rejects upstream producers before the backlog becomes unrecoverable.

Trade-offs and pitfalls#

Backpressure handles overload, but queues bring a few other costs too.

  • Latency: a queue adds delay. If a request has a tight latency budget (for example, under 500 ms end to end), putting a queue in the synchronous path will almost certainly break it. Use queues for work the user doesn't wait on.
  • Duplicates: most queues deliver at least once. Design consumers so a repeated message doesn't charge a card twice.
  • Hidden backlog: monitor queue depth and message age, and alert when they grow.
  • Ordering vs throughput: strict global order limits you to one consumer. Ordering per key (per user, per order) scales much better.

Common technologies#

A few systems cover most use cases.

  • Apache Kafka: a distributed log often used as a high-throughput queue or event stream.
  • Amazon SQS: a fully managed queue on AWS, with standard and FIFO variants.
  • RabbitMQ and Google Cloud Pub/Sub are other common choices.

Key takeaways#

  • A queue decouples producers from consumers so each can scale and fail independently.
  • Queues absorb bursts: extra work waits instead of being dropped.
  • Use retries plus a dead letter queue for failures, and make consumers idempotent.
  • Partition keys give you scale while keeping related messages in order.
  • A queue can hide a capacity shortfall. Use backpressure and watch queue depth.
Revision notes · 2 min

Core idea#

A queue sits between producers that create work and consumers that do it. It absorbs traffic spikes without dropping requests and lets each side scale on its own.

Key points#

  • The queue stores each message durably until a worker acknowledges it.
  • Producers and consumers do not know about each other.
  • Two common uses: buffer bursty traffic and distribute expensive work across workers.
  • If 1,000 requests arrive and workers handle 200 per second, the rest wait instead of failing.

Concepts#

Concept What it means
Ordering Kafka orders only within a partition; related messages share a partition key
Retries Unacknowledged messages are redelivered; consumers must be idempotent
Dead letter queue Messages that fail too often move aside for inspection and replay
Partitions Split the queue across servers; choose a partition key
Backpressure Slow producers when consumers cannot keep up

Backpressure#

  • A queue can hide a capacity problem. If input exceeds processing, the backlog grows forever.
  • When the queue is full, reject new requests with 429 or slow intake.
  • Watch queue depth and message age. Throttle or reject before the backlog is unrecoverable.

Technologies#

  • Kafka: a distributed log for high-throughput queues and event streams.
  • Amazon SQS: managed, with standard and FIFO variants.
  • RabbitMQ and Google Cloud Pub/Sub are other choices.

Pitfalls#

  • A queue in the synchronous path breaks a tight latency budget. Use queues for work the user does not wait on.
  • Most queues deliver at least once. A repeated message must not charge a card twice.
  • Strict global order limits you to one consumer. Order per key scales better.

Say it in an interview#

  • "The queue absorbs the burst, and workers drain it at a steady rate."
  • "Retries plus a dead letter queue, with idempotent consumers."
  • "Backpressure returns 429 when the queue is full."

Found a mistake? Email ask@bigonotes.com