Sub topic 10
Utilization and queueing: why latency rises near saturation
This chapter assumes you know Little's law, percentiles and the difference between latency and throughput from earlier chapters. It explains why a service that is fast at 50% load can be slow at 90%, and how to turn that into a utilization limit for capacity planning.
Utilization and service time
Utilization is the fraction of time a resource is busy. For one server it is the arrival rate times the average service time:
- utilization = arrival rate × service time
- Service time is how long one request needs the resource when nobody is ahead of it.
- Latency is service time plus time spent waiting in a queue.
A worker that handles a request in 10 ms can serve at most 100 requests per second. At 70 requests per second its utilization is 0.7. Utilization above 1.0 means requests arrive faster than they leave, so the queue grows without bound.
Why waiting grows non-linearly
Requests do not arrive evenly. Several can show up within the same few milliseconds, even when the average rate is low. The extra requests wait. At low utilization the worker is usually idle, so the queue drains before the next burst. Near saturation the worker has little idle time left, so each burst leaves a backlog that takes a long time to clear.
The simplest model is the M/M/1 queue. It assumes random arrivals, random service times and one server. Its mean time in the system is:
W = S / (1 - ρ)Here S is the service time and ρ is the utilization. The code below computes the formula for a 10 ms service time.
def mean_latency_ms(service_ms, rho):
return service_ms / (1 - rho)
def max_utilization(service_ms, target_ms):
# Highest utilization that keeps mean latency within the target
return 1 - service_ms / target_ms
for rho in (0.5, 0.7, 0.8, 0.9, 0.95, 0.99):
print(f"{rho:.0%} {mean_latency_ms(10, rho):.0f} ms")
print(max_utilization(10, 40)) # 0.75The loop prints 20, 33, 50, 100, 200 and 1000 ms. Going from 50% to 90% utilization multiplies latency by five. Going from 90% to 99% multiplies it by ten.
The tail grows faster than the mean
In M/M/1 the time in the system follows an exponential distribution, so the p99 is about ln(100) ≈ 4.6 times the mean. At 90% utilization the mean is 100 ms and the p99 is roughly 460 ms. Targets written as percentiles therefore need more headroom than targets written as averages.
Little's law gives the same picture in terms of queue length. The average number of requests in the system is ρ / (1 - ρ). That is 1 at 50% utilization, 9 at 90% and 99 at 99%. Each of those requests holds a connection, a thread and memory.
Turning the curve into a planning rule
Use the model backwards. Start from the latency target and compute the highest utilization you can allow:
- Take the service time from a measurement at low load, such as 10 ms.
- Take the latency target for the component, such as a 40 ms mean.
- Compute the maximum utilization: 1 - 10/40 = 0.75.
- Multiply by the capacity of one server (100 requests per second) to get 75 requests per second per server.
- Divide the peak load from your load estimate by that number to get the server count.
Many teams simply plan to run latency-sensitive services at 50% to 70% utilization at peak. This is the headroom from the load-estimation chapter, and now there is a reason for it. A steady 90% looks efficient on a cost chart, but it leaves no room for bursts, a failed instance or a slow dependency.
Common errors
- Reading CPU at 60% as "40% spare". Queueing delay depends on the busiest resource, which may be a connection pool, a disk or a single hot thread.
- Averaging utilization over a long window. A 5-minute average of 60% can hide many seconds at 100%.
- Sizing for average load. The peak sets the utilization that matters.
- Testing only at light load, which shows the flat part of the curve.
Exercise
A worker takes 20 ms to handle a request. You want a mean latency of at most 100 ms under the M/M/1 model. What is the highest utilization you can allow, and how many requests per second can one worker handle at that utilization?
Show answer
Maximum utilization = 1 - 20/100 = 0.8. One worker can serve 1000/20 = 50 requests per second at full utilization, so at 0.8 it handles 0.8 × 50 = 40 requests per second.