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 bench | What it means in a real system |
|---|---|
| Box at the left | The producer: the program that creates work, for example the website. The lamp on its front lights when it has to wait. |
| Numbered tile | One message: one job to do. The number shows the order it was made in. |
| Belt with 12 places | The queue. The front of the line is at the right, by the two posts. |
| Workers 1, 2, 3 | The consumers: programs that do the jobs. Each takes one message at a time. |
| Row of lamps on a worker | How far it is with its message. When all five are lit, it says “done” and the message fades. |
| Cover over a track | That worker is off. |
| Meters | How 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.