ealeng

Episode 04 of 55 · Queues and capacity

Why busy is slow: queues, utilization and capacity

One worker at 86 requests a second averaged 204 ms; 6% more traffic took it to 954 ms. Why a busy service gets slow all at once (utilization and the knee), why a queueing formula fed the work you asked for missed by 3×, and a capacity plan for twice today's peak, checked with real runs: 9 workers, a p99 of 49 ms, and room for 35% more traffic than planned.

Lab 03 · Queues, Utilization and Capacity17 min read
Video transcript9:24

0:00Cold open

0:01

I tested my service with one worker, so it handles one request at a time. At eighty-six requests a second, the average took two hundred milliseconds.

0:12

Then I sent five more requests a second. About six percent more traffic.

0:18

The average jumped to almost a full second. Nearly five times slower.

0:25

And next month, my traffic doubles. How many workers will I need?

0:31Intro

0:32

Hands-on system design, part four.

0:36

Today: why a busy service gets slow all at once, and how to plan for it.

0:42Meet Lucía

0:43

I’m Lucía, a platform engineer.

0:46

My service has a set number of workers. Each request needs one worker for about ten milliseconds. Some take less, some take more.

0:56

When every worker is busy, a new request waits in line.

1:01

Think of a coffee shop. Workers are baristas. Requests are customers. And ten milliseconds of work is making one coffee.

1:1101 · What you need

1:13

To run it yourself: Python with a few packages, and Docker for the service.

1:19

The tests check the queueing formulas against a simulation of a queue. They need no services.

1:26

No cloud, and no API keys.

1:3102 · The work, in code

1:32

Here’s one request’s work, in code.

1:36

First, wait for a free worker. That waiting is the line.

1:41

Then hold that worker for a random time: ten milliseconds on average.

1:47

And report both: how long it waited, and how long it worked.

1:5303 · How busy is busy?

1:55

First: how busy is a barista? Say one coffee takes one minute.

2:00

Thirty customers an hour bring thirty minutes of work.

2:05

So the barista is busy thirty minutes out of sixty. Half the time.

2:11

That share is called utilization. Here, fifty percent.

2:16

Same for my service. Fifty requests a second, ten milliseconds each, is half a second of work every second. Fifty percent busy.

2:2704 · Why a line forms

2:28

Half busy sounds relaxed. But customers don’t arrive evenly, and some orders take longer than others.

2:37

Here’s one simulated day at half busy. The height is how many people are waiting.

2:43

A short line forms now and then, and it clears quickly. Half the time is spare, so the barista catches up.

2:51

Now the same shop, ninety percent busy.

2:55

The line builds up, and it rarely clears. There’s only one spare minute in ten to catch up.

3:0405 · The knee

3:05

So how long does a customer spend in the shop? It depends on the spare time.

3:11

Half busy: one minute in two is spare. A customer spends about two coffee-times in the shop: about one waiting, and one for their own coffee.

3:21

Eighty percent busy: one minute in five is spare. Five coffee-times.

3:28

Ninety percent: ten. Ninety-five: twenty. The rule: one coffee-time, divided by the spare share. Each time the spare time halves, the wait doubles.

3:39

On a chart, it’s almost flat, and then it shoots up. That bend is called the knee.

3:46

Four baristas sharing one line hit the knee later. But they still hit it.

3:5306 · A real worker

3:54

Does a real service do this? Here’s mine, with one worker, at four traffic levels. Now one coffee-time is ten milliseconds.

4:03

Forty-eight requests a second: twenty-two milliseconds on average.

4:09

Seventy-six a second: seventy milliseconds.

4:14

Eighty-six: two hundred and four milliseconds. About twenty coffee-times. Notice it sits at ninety-five percent busy. Hold that thought. And ninety-one: almost a full second.

4:27

At ninety-one a second, as much work arrived as one worker could do. So the line never got a chance to clear.

4:3607 · The formula missed

4:38

Before that run, I predicted it with the knee rule. Eighty-six requests a second, ten milliseconds each, is eighty-six percent busy. Fourteen percent spare. Ten milliseconds divided by fourteen percent: about seventy milliseconds.

4:54

What do you think the real average was?

5:00

Two hundred and four. Almost three times more.

5:0508 · Why it missed

5:07

In the real runs, each coffee took about eleven milliseconds, not the ten I asked for. Even at the lightest traffic.

5:16

Eighty-six requests times eleven milliseconds is about nine hundred fifty milliseconds of work every second. Ninety-five percent busy, not eighty-six. The plan assumed fourteen percent spare. Really, only five.

5:32

A third of the spare time. So about three times the wait.

5:37

Near the knee, a small mistake in the work per request becomes a big mistake in the wait. So measure it in the real service. Don’t plug in the number you hoped for.

5:5009 · The work to plan for

5:51

Now, next month. Today’s peak is three hundred requests a second. Double it: six hundred.

5:58

I measured the work at today’s peak: ten point four milliseconds per request. A second is a thousand milliseconds, so one worker handles about ninety-six a second.

6:11

Six hundred divided by ninety-six is six point three. A bit more than six workers’ worth of work.

6:2010 · How many workers?

6:21

So far we looked at the average. For a plan, I care about the slow ones. My goal: ninety-nine requests in a hundred finish within a hundred milliseconds. That’s a p99 of a hundred milliseconds.

6:36

Remember, baristas sharing one line hit the knee later. The formula for that is longer, so the code works it out.

6:45

The fewest workers that meet the goal: seven. They’d be about ninety percent busy. But seven baristas share one line, so the predicted p99 is seventy-seven milliseconds. Under the goal.

6:59

With thirty percent spare room, so at most seventy percent busy: nine workers. Predicted p99: forty-nine.

7:0911 · The plan, checked

7:11

A plan is a guess until you test it. So I ran each option for real, at six hundred requests a second.

7:19

First, six workers. We need six point three, so that’s four percent too few. What do you think the slowest users waited?

7:30

Almost four seconds. The line never cleared.

7:34

Seven workers: eighty milliseconds. The formula said seventy-seven.

7:41

Nine workers: forty-nine. The formula said forty-nine.

7:4712 · Why spare room

7:49

Seven workers meet the goal. So why pay for nine?

7:54

Because traffic forecasts are often wrong. With nine, the formula says the goal holds up to eight hundred and twelve requests a second. That’s thirty-five percent more than planned.

8:06

I tested eight hundred and twelve too: a p99 of a hundred and eight milliseconds. Just over the goal, right where the formula put the edge.

8:17

At nine hundred and thirty-four a second, fifteen percent past the edge, the slowest users waited six seconds.

8:26

And the service’s own timer said only seven tenths of a second. Like a barista timing from the counter: most of the wait happened out the door, before the service even saw the request. So measure where users are, too.

8:42Back to Lucía

8:44

So, three rules.

8:46

Watch how busy your workers are, not today’s wait times. The wait stays flat, and then it shoots up.

8:54

Measure the work per request in the real service. Near the knee, small errors become big ones.

9:02

And leave spare room, then check the plan with a real run.

9:07

Next time: what happens when adding workers stops helping.

9:12

For today: busy is the number to watch.Lucía: Watch how busy, not how fast.

9:17Summary

9:18

How busy, the knee, measured work, and a plan you check.

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.

Try it · An hour at the counterOne barista · one coffee = 1 minute of work · the M/M/1 formula

The barista's hour: one block per coffee

0 min15 min30 min45 min60 min
Making coffee: 30 min Spare: 30 min
Busy
50%
Spare share
1 in 2
Time in the shop
2.0 min
Of that, waiting
1.0 min

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

01020304050600/h10/h20/h30/h40/h50/h60/h50% → 280% → 590% → 1095% → 20

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.

Try it · A day at the coffee shopA simulation, not a measurement · Lab 03 queue_sim.py

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.

10:22 · minute 142 of 480
50% busy259 customers in the day
busy8:0010:0012:0014:0016:00line length (20 at the top rule)
In line now
0
Longest yet
3
Served
71
Day's average time in the shop: 2.00 coffee-times (formula: 1 ÷ 0.5 = 2). 50% of customers found the barista busy.
90% busy435 customers in the day
busy8:0010:0012:0014:0016:00line length (20 at the top rule)
In line now
20
Longest yet
20
Served
150
Day's average time in the shop: 9.12 coffee-times (formula: 1 ÷ 0.1 = 10). 95% of customers found the barista busy.
Waiting Just arrived Being served (ring: how far along the coffee is)

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.
Try it · The kneeLab 03 · 01_queue_sim.py · times in units of one service time

Utilization: how busy each worker is

0×5×10×15×20×25×0%20%40%60%80%100%↑
One worker (M/M/1) Four workers sharing one line (M/M/4)↑ off the chart
Spare time
10%
One worker
10.0×
Four, one line
3.0×
One worker, 10 ms work
100 ms

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.

Try it · A real queue against the modelLab 03 · 02_real_vs_model.py · GET /work/sleep, 10 ms asked, 60 s per run

Requests a second (utilization planned from the idle 10.49 ms)

Measured busy
95%
Work per request
11.2 ms
Real mean
204 ms
Model, 10 ms asked
70.3 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 range
5 ms10 ms30 ms100 ms300 ms1 s3 s

real 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 range
5 ms10 ms30 ms100 ms300 ms1 s3 s

real 588 ms · model, 10 ms asked 324 ms · model, measured service 1.17 s · 20 simulated runs 448 ms–1.16 s

Real run (wait + work, timed in the app) Model fed the asked 10 ms Model fed the run's measured work 20 simulated runs of the same length

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.
Try it · Door → line → counterLab 03 · 05_capacity_plan.py · p99 at the client vs. in the service, 45 s per run
doorservice's clock starts0 msdone: 6.07 s
Out the door: connection pools, socket backlog (the service can't see it) At the counter: line for a worker + the work (the service's own timer)
p99 at the client
6.07 s
p99 in the service
694 ms
Before its clock
89%

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.

Try it · The capacity plan, checkedLab 03 · 05_capacity_plan.py · 600 req/s planned, goal p99 ≤ 100 ms, 45 s per run

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.

Measured busy
69%
Model p99
49 ms
p99 in the app
48.0 ms
p99 at the client
49.0 ms
Model49.0 ms
In the app48.0 ms
At the client49.0 ms
20 msgoal 100 ms1 s8 s

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/sleep holds 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

  1. Work out utilization for each service: arrival rate × work per request ÷ workers. Watch it, not just today's latency.
  2. 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.
  3. 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.
  4. Prefer one shared line to a line per worker.
  5. Plan from a latency goal: fewest workers that meet it, then add spare room for forecasts that are wrong.
  6. Check the plan with real runs, including the predicted edge and one past it.
  7. Measure latency at the client as well as inside the service, especially near or past capacity.
  8. 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.

Try it · Practice in PythonRuns in your browser · ⌘/Ctrl + Enter to run
queue_practice.py89 lines

Quiz

Try it · Check your understanding0 of 6 answered
  1. 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. 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. 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. 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. 5.7 workers met the 100 ms p99 goal (80 ms measured). Why did the plan pick 9?

  6. 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.

Learning something useful?

New labs, articles, and videos on evaluating and testing AI systems come out regularly. Follow along so you don't miss the next one.