Part 2 of 8 · 2 min read
What a queue puts between two programs
A queue lets one program hand work to another without waiting for it.
Think of a restaurant kitchen. A waiter does not stand at the stove until the dish is cooked. The waiter clips an order slip to a rail and goes back to the tables. The cook takes slips from the rail when ready. The rail is the queue.
In software, a direct call works differently. Service A calls service B and waits for the answer. If B is slow, A is slow. If B is down, A fails. The two are tied together in time.
A message queue breaks that tie. The System Design Interview book describes it as a durable component, kept in memory, that supports asynchronous communication. Programs that create messages are called producers (or publishers). Programs that perform the work described in the messages are called consumers (or subscribers). The producer drops a message in the queue and moves on. The consumer picks it up later.
Message queue
A message queue is a buffer that sits between programs. One side adds messages, and the other side takes them out and acts on them, usually at a later time.
Example: a web server adds the message "resize photo 8841" to a queue and replies to the user at once. A worker takes the message a few seconds later and does the resize.
The same book gives a photo example. Web servers publish photo processing jobs to the queue. Workers pick up the jobs and do the slow cropping and blurring in the background. The producers and the workers can be scaled on their own. When the queue grows long, more workers are added. When it stays empty, some workers can be removed.
Asynchronous
Asynchronous means the sender does not wait for the receiver to finish. The sender only waits until the message has been safely accepted.
Example: the web server waits only until the queue confirms "message stored". It does not wait for the photo to be resized.
The Designing Data-Intensive Applications (DDIA) book adds a consequence. Delivery to a consumer happens at some undetermined later time. Often that is a fraction of a second, but it can be much longer if a backlog has built up.
A queue decouples two programs in three ways:
| Kind of decoupling | What it means | Example |
|---|---|---|
| In time | The consumer need not be running when the message is sent | Workers are down for a release, and orders still queue up |
| In speed | Each side works at its own pace | A burst of uploads is worked off slowly |
| In failure | A crash on one side does not stop the other | The email provider is down, and the web site keeps taking orders |
The Understanding Distributed Systems book (UDS) says the same: a message channel is a temporary buffer for the receiver, and sending a message does not need the receiving service to be online.
Check yourself:
- Name the two roles that a queue connects.
- In the kitchen example, what does the rail do that a direct hand-over would not?