The busiest day of the year
An online shop sends an email receipt for every order. On a normal day it gets 50 orders an hour. On its busiest sale day it gets 50 orders a minute.
The email service can send only 20 a minute. If the website waits for each email, customers see an error at checkout. The shop loses sales on its best day.
By the end of this lesson
Note
I can explain why a queue lets two parts work at their own speed.
Key words
| Word | What it means | Example |
|---|---|---|
| Queue | a waiting line of messages | |
| Message | one piece of work to be done | receipt for order 1042 |
| Producer | the part that adds messages | |
| Consumer | the part that takes messages and does the work | |
| Backlog | the messages still waiting | |
| Worker | one running copy of a consumer |
The cells students fill in are left blank in this preview.
A queue sits between two parts
A queue is a waiting line for work. The website is the producer. It puts a message in the queue, such as "send a receipt for order 1042". Then it carries on at once.
The email service is the consumer. It takes the oldest message, does the work, then takes the next. Neither part waits for the other.
Try it: a queue in Python
# A list used as a queue: add at the end, take from the front.
queue = []
# The website adds three messages and carries on at once.
queue.append("receipt for order 1042")
queue.append("receipt for order 1043")
queue.append("receipt for order 1044")
print("Waiting:", len(queue))
# The email worker takes the oldest message first.
message = queue.pop(0)
print("Sending", message)
print("Waiting:", len(queue))
# Add a receipt for order 1045, then take two more messages.
# Which message is left at the end?
The backlog grows, then shrinks
Each part works at its own speed. When messages arrive faster than the consumer works, the backlog grows. When the rush ends, the consumer catches up and the backlog shrinks.
Nothing is lost while it waits. A customer gets the receipt a few minutes late. That is far better than an error at checkout.
Worked example: four busy minutes
| Minute | Orders in | Emails sent | Backlog after |
|---|---|---|---|
| 1 | 50 | 20 | 30 |
| 2 | 50 | 20 | 60 |
| 3 | 10 | 20 | 50 |
| 4 | 0 | 20 | 30 |
The cells students fill in are left blank in this preview.
A common mistake
Warning
Many people think a queue makes the work faster. It does not: the email service still sends 20 a minute. A queue stops one part waiting for the other, and it soaks up a rush. If messages arrive faster all day, every day, the backlog never empties. Then you add workers.
More workers drain the backlog
A consumer can run as several copies, called workers. Each worker takes a different message. Two workers that each send 20 emails a minute send 40 together.
A queue also helps when a part fails. If the email service is down for ten minutes, the messages wait. When it comes back, it starts on the oldest one. No order is forgotten.
Worked example: clearing a backlog of 600
| Workers | Emails a minute | Minutes to clear 600 |
|---|---|---|
| 1 | 20 | 30 |
| 2 | 40 | 15 |
| 3 | 60 | 10 |
| 6 | 120 | 5 |
The cells students fill in are left blank in this preview.