System Design Academy

Saving…

Lesson 6 of 9 in Module 40 of 54 lessons complete

Message Queues

Module 4, lesson 6Concept10 min

Definition

Message queue

In plain English
A message queue holds messages from producers until consumers are ready to process them.
Think of it like
It is like the ticket rail in a kitchen where orders wait for the next free cook.

Producer and consumer

In plain English
A producer puts messages onto the queue, and a consumer takes them off and does the work.
Think of it like
It is like waiters pinning tickets up and cooks taking them down.

Competing consumers

In plain English
Competing consumers are several consumers taking from one queue, each message going to just one of them.
Think of it like
Like several cooks working one ticket rail.

Why it exists

Nine hundred orders arrive in ten minutes. Kitchens cannot all finish at the same speed as incoming requests.

A consumer crashes while work is waiting. A durable queue keeps accepted messages until processing can resume.

How it works

Store messages durably and retain them long enough for recovery. A consumer acknowledgement confirms handling, but does not prevent duplicate effects.

Acknowledgement

In plain English
An acknowledgement is the consumer telling the queue a message is fully handled, so it can be deleted.
Think of it like
Like a cook spiking a finished ticket.

Dead-letter queue

In plain English
A dead-letter queue is a side queue for messages that keep failing, so they don't block the rest.
Think of it like
Like a "problem orders" tray the manager checks.

Producers send to a durable queue, which delivers work to two competing consumers and moves repeated failures to a dead-letter queue.

Workers share waiting jobs and separate repeated failures.

Producers send to a durable queue, which delivers work to two competing consumers and moves repeated failures to a dead-letter queue.

  1. Publish safely

    The producer waits for confirmation that the queue stored the message durably.

  2. Receive and process

    A consumer takes a message and completes its work.

  3. Acknowledge completion

    Acknowledge only after the required effect is saved.

  4. Retry unfinished work

    In this queue design, a message without acknowledgement becomes available after a visibility timeout.

  5. Isolate repeated failure

    After five failed attempts, move the message to a dead-letter queue for investigation.

  • A queue buffers bursts but cannot fix a permanently slower processing rate.
  • Add consumers only when the work can run independently.
  • A lost acknowledgement can cause completed work to be delivered again; prevent repeated effects.

Competing consumers share deliveries rather than all receiving copies. A retry can reach a different consumer after a crash.

Examples

Kitchen router

Friday 7 pm: 900 orders in 10 minutes. The queue holds them; kitchens pull at their own pace instead of the order service waiting on each kitchen.

Nine hundred orders in ten minutes average 90 orders per minute. Bursts within that window can still exceed kitchen capacity.

Check processing capacity as well as average arrivals.
Text
arrivals: 90 orders/minute on average
capacity: 100 orders/minute across kitchens
short bursts: wait in the queue
sustained arrivals above capacity: backlog grows

The insight

Queues turn spikes into steady work.

Trade-offs

Pros

  • Buffer short bursts.
  • Retry interrupted work.
  • Scale independent workers.

Cons

  • Duplicates remain possible.
  • Queue storage and operation cost money.
  • Following a job across machines takes effort.

Use when

  • Background work arrives in bursts.
  • Producers and consumers fail independently.

Avoid when

  • The caller needs an immediate result.
  • Unbounded waiting would violate a deadline.

Quick check

A consumer crashes while processing a message it hasn't acknowledged. What happens?
What is a dead-letter queue for?

Takeaways

  • Queues hold work between producers and consumers.
  • Acknowledge after completing work, and handle redelivery safely.
  • Dead-letter queues isolate jobs that need investigation.

Try the quick check above whenever you feel ready.

Previous: Synchronous vs Asynchronous Work