Module 2 · Backend architectureLesson 17 of 887 minIntermediate

Message queues

A queue lets one part of a system hand off work and move on, while workers finish it at their own pace.

Try it

Raise traffic until the queue grows. Then add workers until it drains. Crash a worker and see the others carry on.

Some work doesn't need to happen while the user waits: sending an email, resizing a photo, charging a card after an order is placed. A message queue lets you hand that work off.

The pieces

  • Producer: puts a message on the queue, like "resize photo 123".
  • Queue: stores messages safely, in order, until someone takes them.
  • Consumer (worker): takes a message, does the work, then acknowledges it so the queue deletes it.

If a worker crashes before acknowledging, the message becomes visible again and another worker picks it up. Nothing is lost, but a message might be processed twice, which is why consumers should be idempotent (you'll learn that in module 5).

Why queues help

  • Absorb bursts: a spike fills the queue instead of crushing the database.
  • Decouple: the producer doesn't need the worker to be running right now.
  • Scale separately: add workers when the queue grows, remove them when it's empty.
  • Faster responses: the user gets "order received" immediately.

The one rule

A queue smooths out bursts, not a permanent overload. If messages arrive faster than workers can finish them on average, the queue grows until something breaks. Monitor queue length and age of the oldest message, and scale workers on them.

Popular tools

  • RabbitMQ: classic message broker with flexible routing.
  • Amazon SQS: fully managed, simple queues.
  • Kafka: a durable log that many consumers can read at their own pace. Great for event streams and replaying history.

Dead letter queues

Some messages fail every time (bad data, a bug). After a few tries they're moved to a dead letter queue so they stop blocking others, and an engineer can inspect them later.

Quick check

Orders arrive at 100 per second during a 1-minute sale. Workers can handle 40 per second. With a queue, what happens?