System design lab · Episode 4

How a message queue keeps work safe

A message queue lets one part of a system hand work to another part without waiting for it. The work waits in a line until a worker is free. Here the line is a belt: a producer puts numbered messages on it, and workers take them from the front. Follow the lessons under the picture.

  1. Episode 1Load balancer
  2. Episode 2Cache
  3. Episode 3Rate limiter
  4. Episode 4Message queue
  5. Episode 5Database index
  6. Episode 6Replication and consensus
  7. Episode 7Circuit breaker
  8. Episode 8Backpressure
  9. Episode 9Retries and jitter
  10. Episode 10Consistent hashing
  11. Episode 11Partitioned log
  12. Episode 12Write-ahead log
  13. Episode 13Quorums
  14. Episode 14Unique IDs
  15. Episode 15The last ticket
  16. Episode 16Saga
  17. Episode 17Cache problems
  18. Episode 18Bloom filter
123
A message a worker is working on nowThe belt: the queue, with 12 placesA cover over a track: that worker is off

Controls

Workers
Traffic
Time

Want the details?

What is a message queue?

When you sign up on a website, it may send you an email, make a small picture of your photo, and add you to a newsletter. If the website did all of that before answering, you would wait. Instead, it writes each job as a short message and puts it in a queue. It answers you at once. Other programs, the workers, take the messages from the queue and do the jobs a moment later.

The queue is a line: the first message in is the first one out. It keeps the messages safe until a worker has finished them.

How to read the bench
Part on the benchWhat it means in a real system
Box at the leftThe producer: the program that creates work, for example the website. The lamp on its front lights when it has to wait.
Numbered tileOne message: one job to do. The number shows the order it was made in.
Belt with 12 placesThe queue. The front of the line is at the right, by the two posts.
Workers 1, 2, 3The consumers: programs that do the jobs. Each takes one message at a time.
Row of lamps on a workerHow far it is with its message. When all five are lit, it says “done” and the message fades.
Cover over a trackThat worker is off.
MetersHow many messages wait now, how long the last message waited before a worker took it, and the length of the line over the last 30 seconds.
Why use a queue?
  • Nobody waits for the slow part. The producer puts the message down and goes on. The user gets a fast answer.
  • Rushes are smoothed out. When many messages come at once, the queue holds them, and the workers finish them at their own pace.
  • More workers, more speed. If the line keeps growing, you start more workers. They all take from the same queue, and each message still goes to only one of them.
  • Work is not lost. If a worker crashes, its unfinished message goes back to the queue, and another worker does it.
The numbers
  • A worker needs 1.9 seconds for a message, so one worker finishes about 0.5 per second, two about 1.05, three about 1.6.
  • Quiet: one message every 2.5 seconds. One worker is enough.
  • Busy: one message every second. One worker falls behind and the belt is full after about 25 seconds. Two workers just keep up. Three empty a full belt in about 20 seconds.
  • Rush: 8 messages within 2 seconds, every 20 seconds. One worker clears a rush in about 15 seconds, two in about 8.
In real systems
  • Common tools. RabbitMQ and Amazon SQS are message queues. Many web apps use background job tools such as Sidekiq or Celery, which keep their queues in Redis.
  • Saying “done”. In a real queue this is called an acknowledgement, or ack. Until a worker acks a message, the queue keeps it. If the worker crashes or takes too long, the queue gives the message to another worker.
  • Sometimes twice. If a worker finishes a job but crashes just before it says “done”, the job will run again. So jobs should be safe to repeat: sending the same email twice is bad, so the worker first checks if it was already sent. This is called at-least-once delivery.
  • When the queue is full. A real queue can hold millions of messages, but not without limit. When it is full, the producer must slow down or wait. This is called back-pressure.
  • Messages that always fail. A message that makes every worker fail is moved aside to a dead-letter queue, so it does not block the line forever.
  • Logs. Kafka works a little differently. It keeps every message in a long log and does not remove it. Each reader keeps its own bookmark, so many different readers can read the same messages.
Words to know
Message
A short note that describes one job to do.
Producer
The program that puts messages in the queue.
Consumer, or worker
A program that takes messages from the queue and does the jobs.
Acknowledgement (ack)
The worker's “done”. After it, the queue deletes the message.
Backlog
The messages that are waiting in the queue.
Back-pressure
Making the producer slow down when the queue is full.
FIFO
First in, first out: messages leave in the order they arrived.