Producers, consumers and the queue
After this: Follow jobs from producer to queue to workers, first in, first out, and say when a job is acknowledged.
On this page · 7 sections · 3 checks
In the last lesson the app server put one job on a queue, and a worker sent the email later. A real website has thousands of sign-ups a day, so the queue holds many jobs at once. Who takes which job, in what order, and how does the queue know a job is finished?
Two sides of the queue
A queue has two sides, and different programs stand on each side.
- The program that puts jobs on the queue is a . In the sign-up, the app server is the producer.
- The program that takes jobs off the queue and does them is a . The workers from the last lesson are consumers.
The two sides do not wait for each other. The producer adds a job and moves on at once. A worker takes the next job whenever it is free. The queue sits between them and holds the jobs that are waiting.
First in, first out
The queue hands out jobs in the order they arrived: the oldest job first. This is the same queue you met in the Stacks and queues tutorial: add at the back, take from the front, first in, first out. Here the queue is a separate system that many programs share, but the rule is the same.
Here is the smallest example. Five jobs, j1 to j5, are on the queue. Two workers each take one job per tick, where a tick is one step of time. No new jobs arrive.
- First tick: worker 1 takes j1 and worker 2 takes j2. Three jobs wait: j3, j4, j5.
- Second tick: the workers take j3 and j4. One job waits: j5.
- Third tick: worker 1 takes j5. Worker 2 has nothing to do. The queue is empty.
The queue went 3, 1, 0, and all 5 jobs were done. Each job went to exactly one worker, and the oldest went first. Two workers sharing the queue take twice as many jobs per tick as one worker would.
Watch it happen
The card counts the first tick as tick 0. Run it and step through, and read the number waiting on the queue card after each tick.
- Queue: 1 tick of arrivalsRun it and step through. Five jobs are on the queue before the first tick. Each tick, each worker takes one job from the front. Watch the number waiting on the queue card after every tick.
Press Run it, then step through and watch the requests move.
Saying "done"
There is one more part of the rule. When a worker takes a job, the queue does not delete it yet. It hides the job from the other workers and waits. When the worker has finished the work, it tells the queue so. That message is called an , and sending it is called acknowledging the job. Only then does the queue delete the job for good.
Why wait? Because a worker can crash in the middle of a job. Follow one job:
- A worker takes the job "send welcome email to Ana". The queue hides it from the other workers.
- The worker crashes before it sends the email. It never acknowledges the job.
- No acknowledgement comes, so the queue shows the job again.
- Another worker takes it, sends the email and acknowledges it. The queue deletes the job.
Ana still gets her email. A job leaves the queue only when someone says it is finished, so a crash does not lose it.
Queues notice a missing acknowledgement in one of two ways. Some give each job a time limit, often called a : if no acknowledgement arrives by then, the job is shown again. Others notice that the worker's connection has dropped, and hand its jobs out again at once. With a time limit, a worker that is only slow, not crashed, can see its job handed to a second worker while it is still working on it.
The rule
- Producers put jobs at the back of the queue and move on.
- Consumers (workers) take jobs from the front, oldest first, and each job goes to one worker.
- A worker acknowledges a job after the work is done, and only then is the job deleted. A job that is never acknowledged is handed out again.
Check yourself
A producer puts three jobs on an empty queue, in this order: j1, then j2, then j3. A worker becomes free. Which job does it take?
Five jobs, j1 to j5, are on the queue in that order. Two workers each take one job per tick, and no new jobs arrive. What is still on the queue after two ticks?
A worker takes the job "send welcome email to Ana" and crashes before it finishes. What happens to the job?
Where this goes next
- Name the two sides of a queue: producers put jobs on it, consumers (workers) take jobs off it, and neither waits for the other.
- Follow jobs through a queue first in, first out, with each job going to one worker: five jobs and two workers leave 3, 1, 0 waiting.
- Say what an acknowledgement is, and why a worker sends it only after the work is done.
- Say what happens to a job whose worker crashes: it was never acknowledged, so the queue hands it out again.