Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Queues let each part work at its pace

After this lesson: I can explain why a queue lets two parts work at their own speed.

45 min lessonNo commands

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

WordWhat it meansExample
Queuea waiting line of messages
Messageone piece of work to be donereceipt for order 1042
Producerthe part that adds messages
Consumerthe part that takes messages and does the work
Backlogthe messages still waiting
Workerone 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

Code: 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

MinuteOrders inEmails sentBacklog after
1502030
2502060
3102050
402030

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

WorkersEmails a minuteMinutes to clear 600
12030
24015
36010
61205

The cells students fill in are left blank in this preview.

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.