Key takeaways
- ✓Utilization is how busy each worker is: arrival rate × work per request ÷ workers. 50 requests a second × 10 ms on one worker is 50% busy. It's the number to watch, because latency stays flat for a long time and then shoots up.
- ✓With random arrivals and random work, one worker's mean time in the system is one service time ÷ the spare share: 2× at 50% busy, 5× at 80%, 10× at 90%, 20× at 95%, 100× at 99%. Each time the spare share halves, the wait doubles. That bend is the knee.
- ✓Workers sharing one line bend later: four workers at 80% average 1.75 service times, against 5 for one worker. They still blow up near 100%. Four workers with a separate line each were 2.8× slower than one shared line (4.88 against 1.73, simulated).
- ✓Feed the model the work per request you measured, not the one you asked for. The endpoint asked for 10 ms and took about 11 ms at every load. At 86 requests a second the formula said 86% busy and 70 ms; the worker was really 95% busy, a third of the spare time, and averaged 204 ms.
- ✓A capacity plan: 600 requests a second ÷ 96 a worker (10.42 ms measured at today's peak) is 6.25 workers' worth of work. 7 workers is the fewest that meet a 100 ms p99 (the model said 77 ms; measured 80). 9 keeps 30% spare: 49 ms predicted, 49 measured.
- ✓Spare room is for the forecast being wrong. 9 workers hold the goal up to 812 requests a second, 35% over plan (measured 108 ms there, just over). 6 workers, 4% short of the work, waited 3.9 s at p99.
- ✓Past capacity, measure where users are. At 934 requests a second the service's own timer said 694 ms while clients waited 6.1 s at p99: most of the wait happened before the service saw the request.
Most services don't get slow a little at a time. They're fine, and then they aren't. This chapter is about why: a service with a fixed number of workers is a queue, and a queue's waiting time depends on how busy its workers are in a way that stays flat for a long time and then shoots up. One worker at 86 requests a second averaged 204 ms; at 91 a second, about 6% more traffic, it averaged 954 ms, almost five times slower. Then the chapter plans workers for twice today's traffic, and checks the plan with real runs.
The examples come from Lab 03 (Queues, Utilization and Capacity) of the system design labs, scripts 01_queue_sim.py (the queueing formulas, checked against a simulation), 02_real_vs_model.py (a real queue against the formulas) and 05_capacity_plan.py (a capacity plan, checked). The same lab's scripts 03_scaling.py and 04_bottleneck.py, on what happens when adding workers stops helping, are Chapter 5. The formulas are written from scratch in the lab's queueing.py; the simulation (queue_sim.py, built on the SimPy library) knows nothing about them, which is what makes each a check on the other. The real queue is an endpoint of the lab's small Python service, GET /work/sleep, running in Docker on one laptop (an Apple M4 Max). It has a settable number of workers; each request waits for a free one, then holds it for a random time, 10 ms on average, without using the CPU, and the service reports both times: how long the request waited and how long it worked. The laptop was busy with other work during the runs, so the real numbers are noisier than they would be on a quiet machine.
How busy is busy?
Think of a coffee shop. The baristas are workers, customers are requests, and making one coffee is the work one request needs. If one coffee takes a minute and 30 customers come in an hour, they bring 30 minutes of work: the barista is busy half the time.
The barista's hour: one block per coffee
30 customers × 1 min = 30 min of work an hour: 50% busy, 30 of 60 min spare. Time in the shop = 1 coffee-time ÷ (30/60) = 2.0 min.
Average time in the shop (minutes = coffee-times) against customers an hour
The formula is the long-run average for customers who arrive at random and orders that take a random time (one minute on average): the M/M/1 queue. If customers arrived exactly evenly and every coffee took exactly a minute, nobody would wait until the barista was 100% busy. Any single day varies around the average, more so near the top.
That share is utilization, written ρ (rho):
utilization = arrival rate × work per request ÷ workers
= 50 requests a second × 10 ms ÷ 1 worker = 500 ms of work a second = 50%
The work per request is usually called the service time: how long a worker spends on one request, not counting the wait for a worker. Time in the system is the two together, wait plus service, which is what the caller sees as latency. Above 100% utilization more work arrives than the workers can do, and the line grows for as long as that lasts.
Why a line forms long before 100%
50% busy sounds relaxed. But customers don't arrive evenly, and some orders take longer than others. Two customers walk in a few seconds apart and one waits; a slow order holds up everyone behind it. The line clears in the idle time between bursts, and the less idle time there is, the longer it takes to clear.
Below is one simulated 8-hour day at two counters, one barista each: the same random arrivals and random orders, one counter 50% busy and the other 90%. It's a simulation from the lab's own queue code (queue_sim.py), the one the video replays, not a measurement.
A simulation, not a measurement: the lab's own queue simulation (random arrivals, random coffee times, seed 10), one barista per counter, one coffee = 1 minute on average, an 8-hour day after 100 minutes of warm-up.
Same barista, same kind of orders; the 90% counter just gets more customers. One simulated day is luck as well as load: at 90% busy, other seeds gave a day's average anywhere from 4.3 to 24 coffee-times (this seed was picked as the closest to the formula on both counters). The chart samples the line at each whole minute, which is how the video reports a longest line of 7 and 20; between samples the 50% line briefly reached 8.
The classic model of this is the M/M/1 queue: requests arrive at random (a Poisson process: each moment is equally likely to bring one, independent of the last), each needs a random amount of work (exponential: mostly short, sometimes long, with the same spread as its mean), one worker, one first-come-first-served line. The two M's stand for those two kinds of randomness, the 1 for one worker. M/M/c is the same with c workers sharing one line. For M/M/1 the mean time in the system has a short formula:
mean time in the system = service time ÷ (1 − utilization) = one service time ÷ the spare share
| Utilization | Spare | Mean time in the system | p99 |
|---|---|---|---|
| 50% | 1 in 2 | 2.0× service time | 9.2× |
| 80% | 1 in 5 | 5.0× | 23.0× |
| 90% | 1 in 10 | 10.0× | 46.1× |
| 95% | 1 in 20 | 20.0× | 92.1× |
| 99% | 1 in 100 | 100× | 461× |
Each time the spare share halves, the wait doubles. Going from 50% to 80% busy multiplies the mean by 2.5; from 80% to 95%, by 4 more. Drawn as a chart, the line is almost flat and then turns steeply upward. That bend is called the knee, and it's why outages from load feel sudden: nothing broke, the queue just crossed into the steep part. p99, the time 99 requests in 100 finish within, is 4.6 × the mean for M/M/1 (ln 100 ≈ 4.6), so the slow requests climb the knee too, from 9 service times at 50% busy to 46 at 90%.
Workers sharing one line bend later. With four workers, a request only waits when all four are busy, and then the next free one takes it. At 80% busy, that happens to 60% of arrivals (the Erlang C formula gives the chance an arrival finds every worker busy), and the mean is 1.75 service times instead of 5. At 95% it's 5.5. Four workers still blow up at the end; they just get there later. Two more results from the same script:
- One shared line beats a line per worker. At 80% busy, the lab simulated four workers with one shared line (mean 1.73 service times, p99 6.8) and four workers each with its own line, requests assigned at random (mean 4.88, p99 21.3): 2.8× slower with the same workers and the same load, because a request can wait behind a busy worker while another sits idle. Running a Python web server as several processes that each accept their own connections (
uvicorn --workers 4) is closer to four lines than to one. - Variability costs. If every request took exactly the mean (simulated, called M/D/1), the wait in line was about half as long: 2.9 service times in the system at 80% busy instead of 5.0.
Utilization: how busy each worker is
One worker: 1 service time ÷ 0.10 spare = 10.0×. Every request takes exactly the mean (M/D/1, simulated): 5.4×.
Random arrivals, random (exponential) work per request, one first-come-first-served line. Each time the spare share halves, the one-worker wait doubles. Four workers sharing a line bend later and still blow up. Four workers with a line each behave like four one-worker queues: at 80% the lab simulated a mean of 4.88× for four separate lines against 1.73× for one shared line.
Checking the formulas before trusting them
Before using the formulas on a real service, the lab checked them against the simulation: 10 independent simulated runs of 50,000 requests each, for one and four workers at 50%, 80% and 95% busy, mean and p99. All 12 agreed: each 95% interval over the 10 runs contained the formula's value (one worker at 80%: formula 5.00, simulation 5.09, interval 4.94–5.25).
Near the knee, that interval also shows how much one run's luck matters. At 95% busy (formula: 20 service times), single runs averaged anywhere from 13.9 to 23.9. And an interval computed from one run's own 50,000 requests with a bootstrap (resampling those requests many times) is badly overconfident: over 20 runs, it contained the true 20.0 once. In a queue, requests aren't independent. One stuck behind a long line is followed by others stuck behind the same line, so 50,000 requests carry far less information than 50,000 independent samples would. Independent runs are the unit of evidence, not the requests inside one run.
A real worker
Does a real service follow the formula? Script 02 drives /work/sleep open loop (each request is sent on its own random schedule, whether or not earlier ones have come back, as real users do; Chapter 2 covers why that matters) for 60 seconds per point. It first measured the work with no line at all: 10.49 ms on average for the 10 ms asked. Then it picked arrival rates for 50%, 80%, 90% and 95% busy, and compared the real time in the system with the formula:
| Workers | Requests a second | Busy, measured | Real mean | M/M/c with 10 ms | Real p99 |
|---|---|---|---|---|---|
| 1 | 48 | 53% | 22.3 ms | 19.1 ms | 88.7 ms |
| 1 | 76 | 84% | 69.8 ms | 42.1 ms | 279 ms |
| 1 | 86 | 95% | 204 ms | 70.3 ms | 588 ms |
| 1 | 91 | 101% | 954 ms | 106 ms | 1,746 ms |
| 4 | 191 | 51% | 11.5 ms | 10.7 ms | 48.2 ms |
| 4 | 305 | 80% | 20.9 ms | 15.6 ms | 126 ms |
| 4 | 343 | 91% | 35.2 ms | 22.4 ms | 161 ms |
| 4 | 362 | 97% | 87.9 ms | 31.1 ms | 285 ms |
The real worker has a knee: 22, 70, 204, then 954 ms. At 91 requests a second as much work arrived as one worker could do, so the line never got a chance to clear; that 954 ms is one minute's average of a line still growing, not a steady state.
The formula missed
Before the runs, the formula fed the 10 ms the code asked for made a prediction. At 86 requests a second (85.8):
85.8 requests a second × 10 ms = 86% busy, 14% spare
10 ms ÷ 0.14 ≈ 70 ms
The real mean was 204 ms, almost three times more. In the real runs each request took about 11 ms of work, not the 10 asked for, even at the lightest traffic: 11.0–11.2 ms in every one-worker run, from 53% to 101% busy. (The four-worker runs measured 10.5–10.7 ms.) So 86 requests a second carried about 950 ms of work a second: 95% busy, not 86%. The plan assumed 14% spare time; there was really about 5%, a third as much, and so about three times the wait.
That's the main lesson of the knee: near it, a small error in the work per request becomes a big error in the wait. Here an 11% error in the work per request, at what looked like 86% busy, became a 3× error in the wait. So measure the work per request in the real service, and don't plug in the number you hoped for.
Fed each run's own measured work, the model does much better, and the lab checks it the way the previous section recommends: not against the formula's long-run average, but against 20 simulated 60-second runs that resample the run's own measured service times, which shows how far one run can drift by luck. At 86 requests a second, those runs averaged 123–392 ms; the real 204 sits inside. 14 of the 16 checks (mean and p99, eight runs) landed inside the simulated range. The two misses are the same run, four workers at 80%, where both the mean and p99 were high: the lab reads it as one disturbance on a busy machine, not a model failure, and one run can't tell which.
Requests a second (utilization planned from the idle 10.49 ms)
85.8 req/s × 10 ms = 86% busy on paper, 14% spare: 10 ms ÷ 0.14 = 70.3 ms. Each request really took 11.2 ms, so it was 95% busy, 5% spare.
Mean time in the system
real run inside the simulated rangereal 204 ms · model, 10 ms asked 70.3 ms · model, measured service 255 ms · 20 simulated runs 123 ms–392 ms
p99
real run inside the simulated rangereal 588 ms · model, 10 ms asked 324 ms · model, measured service 1.17 s · 20 simulated runs 448 ms–1.16 s
Log scale. The simulated runs resample the run's own measured work per request, so they show how much one 60-second run can vary by luck. 14 of the 16 checks land inside; the two misses are the same run (four workers, 80%), on a machine busy with other work (the lab reads it as one disturbance, not a model failure).
A capacity plan
Now the planning question. Today's peak is 300 requests a second on /work/sleep, and traffic is expected to double. How many workers for 600 a second, with the goal that 99 requests in 100 finish within 100 ms (a p99 of at most 100 ms)?
The inputs the plan needs: the arrival rate to plan for, the work per request measured at a realistic load (here, at today's peak: 10.42 ms), the latency goal, and a model (M/M/c, the formula for workers sharing one line, including its p99).
one worker does 1 ÷ 10.42 ms ≈ 96 requests a second
work at 2× peak 600 ÷ 96 = 6.25 workers' worth of work
So at least 7 workers, or the line never clears. Then the model gives:
| Workers | Busy | Predicted p99 | |
|---|---|---|---|
| Fewest that meet p99 ≤ 100 ms | 7 | 89% | 77 ms |
| With 30% spare room (at most 70% busy) | 9 | 69% | 49 ms |
Seven workers at 89% busy would be deep in the knee for one worker, but seven workers share one line, so a request rarely waits long. Spare room (often called headroom) is capacity you keep unused on purpose: here, utilization capped at 70%. With 9 workers the model says the goal holds up to 812 requests a second, 35% more than planned.
The plan, checked
A plan is a guess until it's tested. Script 05 ran each option for real at 600 requests a second for 45 seconds, plus the predicted edge and a run past it, and recorded p99 two ways: inside the service (wait + work, timed by the app) and at the client (what the load generator saw):
| Run | Busy, measured | Model p99 | p99 in the service | p99 at the client |
|---|---|---|---|---|
| 600/s, 6 workers (one fewer than the minimum) | 107% | ∞ (no steady state) | 1,013 ms | 3,917 ms |
| 600/s, 7 workers (the minimum) | 90% | 77 ms | 79 ms | 80 ms |
| 600/s, 9 workers (the plan) | 69% | 49 ms | 48 ms | 49 ms |
| 812/s, 9 workers (the predicted edge) | 93% | 100 ms | 107 ms | 108 ms |
| 934/s, 9 workers (15% past it) | 111% | ∞ | 694 ms | 6,072 ms |
- Fed the measured work, the model matched every stable run: 77 against 79–80 ms, 49 against 48–49, 100 against 107–108. The edge measured just over the goal, right where the formula put it.
- The minimum has nothing to spare. 6 workers can do 576 requests a second, 4% less than the 600 that arrive, and the slowest users waited almost four seconds. The plan's two extra workers cut p99 from 80 to 49 ms and, more importantly, absorb a forecast that's 35% too low.
- Past capacity, the service's own clock lies. At 934 a second the service measured a p99 of 694 ms while clients waited 6.1 s. Like a barista timing from the counter: most of the wait happened out the door, in connection pools and the server's socket backlog, before the service even saw the request. Measure latency where users are, too.
The service's p99 covers 694 ms of the 6.07 s its slowest 1% of users waited: about 5.38 s happened before it saw the request.
Two separately measured p99s, so the split is approximate: the slowest requests at the client aren't necessarily the slowest inside the service. Past capacity (107% and 111% busy) the line is still growing, so a longer run would give bigger numbers.
600 req/s ÷ 96 per worker (1 ÷ 10.42 ms, measured at today's peak) = 6.25 workers' worth of work. Model: fewest workers 7 (p99 77 ms); with 30% spare, 9 (p99 49 ms, holds to 812 req/s).
Run
9 workers, the plan.
The plan: about 69% busy. The model said 49 ms, and so did the clients.
Log scale. The model is M/M/c fed the work per request measured at today's peak. “In the app” is wait + work as the service timed it; “at the client” is what the load generator saw. The machine was busy with other work during these runs (load average 11–16).
Watch how busy, not how fast
Why set spare room on utilization rather than on today's p99? Because p99 is flat until close to the knee and then jumps. A healthy p99 today says almost nothing about how close you are to the steep part; utilization does. A service at 60% busy with a 20 ms mean that gets 40% more traffic lands at 84%: if it behaves like one M/M/1 worker, the mean goes from 2.5 to 6.25 service times, 20 ms to 50 ms, a 2.5× jump for 1.4× the traffic. Nothing about the 20 ms warned of it.
Caveats
- One laptop, busy with other work. Other work on the machine pushed its load average between 4 and 16 during these runs (each result file records it). The real numbers are this laptop's, from one run per point.
- This is the cleanest real queue there is.
/work/sleepholds a worker without using the CPU, so the work is close to exponential by design. Real requests often have heavier tails (a few very slow ones), which makes queues worse than M/M/c predicts; measure your own spread before trusting the model. - The service time differed between scripts. About 11 ms per request in script 02's one-worker runs, 10.4–10.7 ms in script 05's runs. Different runs on a busy machine; each model used the time measured in its own runs, which is why it matched.
- Over 100% there's no steady state. The 954 ms (one worker, 91 a second) and the 6.1 s (9 workers, 934 a second) are one run's averages of a growing line. A longer run would have given bigger numbers.
- Arrivals were random by construction. The load generator sent requests on a Poisson schedule. Real traffic can be burstier, which also costs waiting.
The checklist
- Work out utilization for each service: arrival rate × work per request ÷ workers. Watch it, not just today's latency.
- Measure the work per request in the real service, at the load you're planning for. Don't use the number the code asks for or the one you hope for.
- Know where the knee is: one worker's wait is one service time ÷ the spare share. More workers sharing one line bend later, and still bend.
- Prefer one shared line to a line per worker.
- Plan from a latency goal: fewest workers that meet it, then add spare room for forecasts that are wrong.
- Check the plan with real runs, including the predicted edge and one past it.
- Measure latency at the client as well as inside the service, especially near or past capacity.
- Judge a run near the knee against several runs, not against its own interval: one run's requests aren't independent.
Practice: plan the workers
Write the lab's queueing math: utilization, one worker's mean time in the system, Erlang C, the mean and p99 for workers sharing one line, the fewest workers that meet a p99 goal, and the highest rate that goal holds to. The tests use the recorded numbers: the knee (2, 5, 10, 20, 100 service times), the 70 ms the asked 10 ms predicted at 86 requests a second, 60% of arrivals waiting for four workers at 80%, and the plan: 6.25 workers' worth of work, 7 workers at 77 ms, 9 with spare room at 49 ms, holding to 812 requests a second.
Quiz
1.A service gets 50 requests a second, each needing 10 ms of one worker's time, and it has one worker. How busy is that worker?
2.One worker, random arrivals, random work per request. At 90% busy the mean time in the system is 10 service times. What is it at 95%?
3.The model, fed the 10 ms the endpoint asked for, predicted 70 ms at 86 requests a second. The real mean was 204 ms. What was wrong?
4.600 requests a second arrive, and one worker does 96 a second. Why are 6 workers not enough, even though 6.25 is close to 6?
5.7 workers met the 100 ms p99 goal (80 ms measured). Why did the plan pick 9?
6.At 934 requests a second on 9 workers, the service's own p99 was 694 ms. The load generator's p99 was 6.1 s. Which do you report?
Check your understanding
Try each question before opening its answer.
What is utilization, and why does latency grow so fast near 100%?
How busy each worker is: arrival rate × work per request ÷ workers. With random arrivals and random work, lines form whenever requests bunch up, and they clear in the spare time. One worker's mean time in the system is one service time ÷ the spare share: 2× at 50% busy, 5× at 80%, 20× at 95%. Halve the spare time and the wait doubles.
At 80% busy, roughly how long does a request take, for one worker and for four workers sharing one line?
One worker: 5 service times on average, 23 at p99. Four workers sharing a line: 1.75 on average, 6.9 at p99, because a request only waits when all four are busy (60% of arrivals at 80%, by Erlang C) and then the first free worker takes it. Four workers still blow up near 100%.
Why is one shared line for four workers faster than a line per worker?
With separate lines, a request can wait behind a busy worker while another one sits idle. One shared line never wastes a free worker. In the lab's simulation at 80% busy: a mean of 1.73 service times for one shared line, 4.88 for four separate lines, 2.8× slower with the same workers and load.
Your capacity model used 10 ms of work per request and is badly off near the knee. What do you check first?
The work per request the real service actually does, measured under real load. Here it was about 11 ms, not 10, so 86 requests a second was 95% busy, not 86%: a third of the spare time and about three times the wait (204 ms real against 70 predicted). Fed the measured work, 14 of 16 checks fell inside the range of simulated runs.
A simulation at 95% busy reports a mean of 23.3 with a tight interval computed from its own 50,000 requests. What's wrong?
The interval treats the requests as independent, and in a queue they aren't: one stuck behind a long line is followed by others stuck behind it. In the lab such intervals contained the true mean (20.0) in 1 run of 20, and single runs ranged 13.9–23.9. Use independent runs and an interval over their means.
Plan the workers for twice today's peak of 300 requests a second, with p99 ≤ 100 ms and 30% spare. What do you need, and how do you check it?
The rate (600 a second), the work per request measured at today's peak (10.42 ms, so 96 a second per worker: 6.25 workers' worth), the goal, and a model. M/M/c gives 7 workers as the fewest (77 ms predicted, 80 measured) and 9 with 30% spare (49 predicted, 49 measured). Then run it for real, including the predicted edge (812 a second: 108 ms measured) and one worker fewer than the minimum (6: 3.9 s).
Why keep spare room on utilization rather than on today's p99?
p99 stays flat until close to the knee and then jumps, so a healthy p99 says little about how close you are. Utilization tells you directly. The spare room absorbs a forecast that's wrong, a slow dependency or a lost server: with 9 workers the goal held to 35% more traffic than planned.
Past capacity, the service reported a p99 of 694 ms and the clients 6.1 s. Why the gap?
The service starts its clock when it accepts a request. Past capacity, most of the line forms before that, in connection pools and the server's socket backlog, where the service can't see it. Users feel the whole wait, so measure at the client too.
Next up: Chapter 5, scaling and bottlenecks: this chapter added workers and they helped. The next one finds where adding workers stops helping (Python threads, a hot database row, a slow pipeline stage) and how to find the bottleneck without guessing.