Sub topic 10
Utilization and queueing: why latency rises near saturation
This chapter assumes you know the earlier ideas in this tutorial: latency, throughput, concurrency, percentiles, and Little's law (in-flight requests = arrival rate × time in system). You should also be able to read short Python programs.
Earlier chapters treated latency as a number you budget. This chapter explains why that number is not fixed. A component's latency depends on how busy it is, and the dependence is steeply nonlinear. A service that answers in 10 ms at 50% load can take several times longer at 90% load, even though the code and hardware have not changed.
Utilization and queues
Utilization is the fraction of time a resource is busy. For a single server it is:
utilization (rho) = arrival rate (lambda) / service rate (mu)The service rate is the maximum throughput of one server. If a request takes 10 ms of service time, the server can finish 100 requests per second, so mu = 100. At 50 requests per second, rho = 0.5.
A request that arrives while the server is busy has to wait. That waiting is queueing delay. Response time is the sum of two parts:
response time = queueing delay + service timeService time is how long the work itself takes. Queueing delay is how long the request waits for its turn. At low utilization the second part is near zero. Near saturation it dominates.
Requests do not arrive on a perfect schedule. They arrive in random clumps. Even at 50% average utilization, several requests sometimes arrive within a few milliseconds, and the later ones wait. Waits happen at every utilization above zero. As utilization rises, the server has less idle time in which to clear the backlog.
The M/M/1 model
The simplest queueing model is called M/M/1. It has these properties:
- Arrivals are random and independent, at an average rate lambda (the first "M").
- Service times are random, with an exponential distribution and a mean S = 1/mu (the second "M").
- There is one server and an unlimited queue (the "1").
Real systems do not match these assumptions exactly. The model is still useful because it gives closed-form results that show the right shape of the curve. The main results are:
- Mean response time:
W = S / (1 - rho) - Mean number in the system (Little's law applied to W):
L = rho / (1 - rho) - Response-time percentile p:
S / (1 - rho) * ln(1 / (1 - p))
In M/M/1 the response time itself is exponentially distributed, which makes the percentile formula simple. For p99, ln(100) is about 4.605. The p99 is therefore about 4.6 times the mean.
The key term is 1 / (1 - rho). It is a multiplier on the service time, and it grows without bound as rho approaches 1.
| Utilization | Multiplier 1/(1-rho) | Mean response (S = 10 ms) | p99 response (S = 10 ms) |
|---|---|---|---|
| 0.50 | 2 | 20 ms | 92 ms |
| 0.70 | 3.3 | 33 ms | 154 ms |
| 0.80 | 5 | 50 ms | 230 ms |
| 0.90 | 10 | 100 ms | 461 ms |
| 0.95 | 20 | 200 ms | 921 ms |
| 0.99 | 100 | 1000 ms | 4605 ms |
Going from 80% to 90% utilization looks like a small change in load. It doubles the mean latency. Going from 90% to 99% multiplies it by ten.
Computing and simulating the curve
The first program evaluates the formulas. The second simulates an M/M/1 queue with the Lindley recursion. For each request, the wait is the previous request's wait plus its service time, minus the gap before the next arrival, with a floor of zero.
import math
service_ms = 10.0
k99 = math.log(1 / (1 - 0.99)) # about 4.605
for rho in (0.5, 0.7, 0.8, 0.9, 0.95, 0.99):
mean_ms = service_ms / (1 - rho)
p99_ms = mean_ms * k99
print(f"rho={rho:.2f} mean={mean_ms:7.1f} ms p99={p99_ms:7.1f} ms")import random
def simulate(rho, service_ms=10.0, n=200_000, seed=1):
rng = random.Random(seed)
mean_gap_ms = service_ms / rho
wait = 0.0
responses = []
for _ in range(n):
service = rng.expovariate(1 / service_ms)
gap = rng.expovariate(1 / mean_gap_ms)
responses.append(wait + service)
wait = max(0.0, wait + service - gap)
return responses
for rho in (0.5, 0.8, 0.9):
r = sorted(simulate(rho))
mean = sum(r) / len(r)
p99 = r[int(0.99 * len(r)) - 1]
print(f"rho={rho:.2f} mean={mean:6.1f} ms p99={p99:6.1f} ms")The simulated means and p99 values land close to the table: about 20 ms and 92 ms at rho = 0.5, about 50 ms and 230 ms at 0.8, and about 100 ms and 460 ms at 0.9. Sampling noise grows with utilization. A queue near saturation takes a long time to settle, so results at 0.95 or above vary a lot between seeds and run lengths. This variance matters when you load test, as described below.
What the model leaves out
The multiplier is a first approximation. Two refinements change how you apply it.
Variability
Kingman's approximation estimates the mean queueing delay of a single server with general arrival and service patterns:
queueing delay ≈ (rho / (1 - rho)) * ((ca^2 + cs^2) / 2) * SHere ca and cs are the coefficients of variation (standard deviation divided by mean) of the inter-arrival times and service times. For M/M/1 both are 1, so the middle factor is 1. The implications:
- If service times are very uniform (cs near 0), queues are shorter than M/M/1 predicts.
- If service times are heavy-tailed (cs of 3 or more), queues are much longer. A few slow requests, such as a large report query or a garbage collection pause, block everything behind them.
- Bursty arrivals (ca above 1) raise delay the same way. Batch jobs, cache expirations, and retry storms create bursts.
The 1/(1-rho) shape stays the same. Variability scales the curve up or down.
Multiple servers
Most services run several workers behind a load balancer. With c servers sharing one queue, the utilization is rho = lambda / (c * mu), and queueing delay is far smaller at the same rho than with c separate single-server queues. This is the pooling effect. The M/M/c model (Erlang C) gives exact numbers. The practical consequences are:
- A pool of 32 workers can run safely at a higher utilization than a pool of 2.
- Splitting traffic across many independent small servers, with no shared queue, wastes this effect. A request can wait behind a slow one while another server sits idle.
- The cliff is still there. It starts later and is sharper once reached.
Finding the utilization ceiling
You can invert the percentile formula to find the highest utilization that still meets a p99 target. For a server with service time S, a percentile p, and a latency target T:
rho_max = 1 - S * ln(1 / (1 - p)) / TThen use rho_max to size capacity. This example assumes a 20 ms service time, a p99 target of 200 ms, and a peak of 1000 requests per second:
import math
service_ms = 20.0
target_ms = 200.0
percentile = 0.99
peak_rps = 1000
k = math.log(1 / (1 - percentile))
max_rho = 1 - k * service_ms / target_ms
service_rate = 1000 / service_ms # requests per second per server
safe_rps_per_server = max_rho * service_rate
servers = math.ceil(peak_rps / safe_rps_per_server)
print(round(max_rho, 3), round(safe_rps_per_server, 1), servers)
# 0.539 27.0 38Each server can run at most 54% busy, which is 27 requests per second out of a raw 50. The fleet needs 38 servers, not the 20 that naive division (1000 / 50) suggests. A p99 target can nearly double the machine count compared with sizing for raw throughput. The result is conservative if your service times are less variable than exponential. Measure your real service-time distribution before settling on it.
Choose the target latency first and derive the utilization ceiling from it. Teams often do the opposite: they pick a CPU number such as "scale at 70%" and never check what latency that implies.
Where the queues are
"Utilization" applies to every resource a request needs, and each has its own queue. Common ones:
- CPU run queue: threads waiting for a core.
- Worker thread or process pool: requests waiting for a free worker.
- Connection pool: requests waiting for a database connection.
- Kernel accept backlog and load balancer queues: requests waiting before your code sees them.
- Disk and network: I/O operations waiting on IOPS or bandwidth limits.
The bottleneck is the resource with the highest utilization relative to its capacity. It sets the throughput ceiling, and its queue contributes most of the delay. Utilization on the other resources can look healthy while one saturated database connection pool stalls every request.
Some queues are hidden. A request waiting in a connection pool shows up as latency in the caller but as nothing at all in the database's own metrics. When latency rises and the downstream service looks fine, check how long requests wait for a connection, a thread, or a lock before they are sent.
Little's law links this back to concurrency. At rho = 0.9 an M/M/1 server holds about 9 requests on average (0.9 / 0.1), and at 0.99 it holds about 99. Rising in-flight counts with flat throughput are the signature of a queue building up.
Failure modes near saturation
Queueing creates several failure patterns beyond slow responses.
- Unbounded queues turn overload into timeouts. If arrivals exceed capacity (rho above 1), the queue grows without limit. Latency rises until clients time out, and the server then works on requests whose callers have already given up. Throughput of useful work collapses.
- Retries add load at the worst time. A client that retries on timeout raises the arrival rate on a service that is already saturated. A short slowdown can become a sustained overload that persists after the original cause is gone.
- Percentiles diverge from means. The p99 is about 4.6 times the mean in M/M/1, and the gap widens with variability. A dashboard showing mean latency can look acceptable while the tail breaches its target.
- Autoscaling lags. New capacity takes time to start. Scaling on a utilization threshold close to the cliff leaves no room for the delay. Scale at the ceiling you derived, not at the point where latency is already rising.
The common defenses are bounded queues, load shedding (rejecting excess requests early with a fast error), timeouts shorter than client timeouts, and retry limits with backoff. The next chapter covers these as trade-offs between latency and throughput.
Using this in a design
- Take the service time from measurement, not from a guess. Use the median or mean time spent actually working, excluding queue wait.
- Choose the percentile and latency target for the component. Use the budget from the earlier chapter.
- Compute
rho_maxfor that component, adjusting upward for multiple workers and downward for high variability. - Size capacity so peak load stays below
rho_max. Include headroom for failures: if one of N servers fails, the remaining N-1 must still stay below the ceiling. - Set alerts on utilization and on queue depth, not only on latency. Rising queue depth warns you before the latency target is breached.
- Bound every queue, and decide what happens when the bound is hit.
Exercises
Exercise
A service has a mean service time of 5 ms. Using the M/M/1 model, what is the highest utilization that keeps p99 response time at or below 100 ms? (Use ln(100) ≈ 4.605.)
Show answer
rho_max = 1 - S ln(100) / T = 1 - 5 4.605 / 100 = 1 - 0.230 = 0.77. The server can run at about 77% utilization. At 0.77 the mean response time is 5 / 0.23 ≈ 21.7 ms, and p99 is 21.7 * 4.605 ≈ 100 ms.
Exercise
A team raises a server's utilization from 80% to 90% to save hardware. By what factor does the mean response time change in the M/M/1 model, and why is the p99 effect worse in absolute terms?
Show answer
The multiplier 1/(1-rho) goes from 5 to 10, so mean response time doubles. The p99 is about 4.6 times the mean, so it also doubles, but from a larger base. With a 10 ms service time, p99 rises from about 230 ms to about 461 ms, an increase of about 230 ms. That increase is larger than the whole mean response time at 80%.
Exercise
Latency on an API rises sharply, but CPU on the API servers is at 40% and the database reports low load. Name one place a hidden queue could be, and one metric that would confirm it.
Show answer
A likely place is the database connection pool. If all connections are in use, requests wait inside the API process before reaching the database, so the database looks idle. Confirm it with the pool's wait time or pending-request count, or with the number of in-use connections sitting at the pool maximum. A full worker thread pool, with its queue depth, is another valid answer.