Absorbing a burst
After this: Work out the queue length tick by tick, and tell a burst a queue can absorb from too little capacity.
On this page · 8 sections · 3 checks
Jobs do not arrive at an even pace. A shop sends a newsletter at nine o'clock and a thousand people buy at once. A school opens its results page and everyone signs in. For a few minutes, jobs pour in much faster than usual, and then they slow down again. What does the queue do in that rush?
Taking jobs off at a fixed pace
Two workers share a queue, and each can do 10 jobs a tick. Together they take at most 20 jobs a tick. That is their : how many jobs they finish per tick.
Now jobs arrive in a , a short rush followed by a quiet stretch. Over eight ticks, the arrivals are 10, 10, 40, 40, 10, 0, 0, 0.
Each tick has the same two parts, in this order:
- The new jobs join the back of the queue.
- The workers take up to 20 jobs from the front.
The burst, tick by tick
| Tick | Arrive | In line | Taken | Waiting after |
|---|---|---|---|---|
| 1 | 10 | 10 | 10 | 0 |
| 2 | 10 | 10 | 10 | 0 |
| 3 | 40 | 40 | 20 | 20 |
| 4 | 40 | 60 | 20 | 40 |
| 5 | 10 | 50 | 20 | 30 |
| 6 | 0 | 30 | 20 | 10 |
| 7 | 0 | 10 | 10 | 0 |
| 8 | 0 | 0 | 0 | 0 |
Read the last column. In ticks 1 and 2 the workers keep up and nothing waits. In ticks 3 and 4, 40 arrive but only 20 leave, so the queue grows to 20 and then 40. That is its highest point, the peak. From tick 5 fewer arrive than leave, and the queue shrinks: 30, 10, and empty after tick 7.
All 110 jobs were done. Without a queue, the 20 jobs the workers could not take in tick 3 would have had nowhere to wait, and the system would have had to turn them away. With the queue, they waited a little. The longest any job waited was 2 ticks.
Watch it happen
The task asks you to say how many will wait before each step. Use the two parts of a tick: add the arrivals, then take 20.
- Queue: 8 ticks of arrivalsRun it and step through. Jobs arrive 10, 10, 40, 40, 10 and then none, and the two workers take 20 a tick between them. Before each step, say how many will be left waiting, then check the queue card.
Press Run it, then step through and watch the requests move.
The same rule in code
The table follows a rule short enough to write down. Here it is as a program: add the arrivals, take at most 20, print what is left.
arrivals = [10, 10, 40, 40, 10, 0, 0, 0]per_tick = 20waiting = 0for tick, new in enumerate(arrivals, start=1): waiting += new taken = min(waiting, per_tick) waiting -= taken print(tick, waiting)1 0 2 0 3 20 4 40 5 30 6 10 7 0 8 0
public class Main { public static void main(String[] args) { int[] arrivals = {10, 10, 40, 40, 10, 0, 0, 0}; int perTick = 20; int waiting = 0; for (int i = 0; i < arrivals.length; i++) { waiting += arrivals[i]; int taken = Math.min(waiting, perTick); waiting -= taken; System.out.println((i + 1) + " " + waiting); } }}1 0 2 0 3 20 4 40 5 30 6 10 7 0 8 0
waiting is the length of the queue. Each time round the loop, the new jobs are added, taken is 20 or whatever is in line if that is less, and the rest stay for the next tick.
Python runs in the browser when you press Run on the block below. Java does not run here: if you are learning Java, read the Java version and paste it into a Java tool on your computer when you want to run it.
Change the arrivals to make the burst longer, or change per_tickperTick to 30, and run it again.
When a queue cannot help
A burst ends. Now look at a rush that does not end. Jobs arrive at 25 a tick, every tick, and the two workers still take 20.
- After tick 1: 25 arrive, 20 leave, 5 wait.
- After tick 2: 10 wait.
- After tick 3: 15 wait.
The queue grows by 5 every tick and never shrinks. After 60 ticks, 300 jobs are waiting, and a new job waits 15 ticks before a worker reaches it.
Press Run it, then step through and watch the requests move.
A bigger queue does not fix this; it only holds a longer line. The fix is to take jobs off faster than they arrive: add a third worker, and 30 leave every tick.
The rule
- A queue absorbs a burst: when work arrives faster than the workers can take it for a short time, the extra waits in line instead of being turned away.
- Over a longer stretch, the workers must take at least as many jobs as arrive. If they cannot, the queue grows without end, and the only fix is more throughput, such as more workers.
Check yourself
In the burst, 20 jobs are waiting after tick 3. In tick 4, 40 new jobs arrive and the two workers take 20. How many are waiting after tick 4?
The last jobs of the burst arrive in tick 5, and 30 are waiting after it. From tick 6 nothing new arrives, and the workers take up to 20 a tick. After which tick is the queue empty again?
All afternoon, 25 jobs arrive every tick. Two workers take 10 each, 20 a tick in all, and the queue keeps growing. What fixes it?
Where this goes next
- Work out the queue length tick by tick: add the arrivals, then take up to the workers' throughput.
- Read a burst's table: for arrivals 10, 10, 40, 40, 10 against 20 a tick, the queue peaks at 40, clears after tick 7, and all 110 jobs are done.
- Say that a queue absorbs a short burst, so extra jobs wait instead of being turned away.
- Say that when jobs arrive faster than the workers take them all the time, the queue grows without end (300 behind after 60 ticks at 25 in, 20 out), and only more throughput fixes it.