The symptom and three explanations#
A fictional ledger-api service runs 16 request workers per instance. At 09:40 UTC on a Tuesday its p99 latency went from 60 ms to about 75 ms over ten minutes and stayed there, while p50 sat at 20 ms throughout. No deploy happened in the hour. Three explanations fit the graph:
- Queueing. Traffic grew, the workers are busier, and requests that arrive when every worker is busy wait for one. Most requests still find a free worker, so the median barely moves.
- A slow subset. About 2% of requests, the
/exportroute, got four times slower, for example because one tenant's exports grew. The other 98% are unchanged, so p50 cannot see it. - A mix shift. Nothing got slower, but
/exportwent from 2% to 5% of requests, and those were always the slowest.
Google's SRE book calls latency increases "often a leading indicator of saturation" and suggests watching p99 over a short window for that reason. The same p99 also moves for the other two explanations, which is why it cannot choose between them. Monitoring distributed systems
What each explanation predicts#
| Observation | Queueing | Slow subset | Mix shift |
|---|---|---|---|
| Total p99 | rises | rises | rises |
| Total p50 | flat or up slightly | flat | flat |
p99 of /item, the fast route | rises | flat | flat |
p99 of /export | up slightly | rises a lot | flat |
Share of requests that are /export | flat | flat | rises |
| Time waiting for a worker, or in-flight requests against worker count | rises | flat | flat or up slightly |
The /item row separates queueing from the other two, because waiting for a worker does not care which route a request is for. The /export row and the share row then split the slow subset from the mix shift. The first two rows are the graph everyone was looking at.
The lab: four worlds through one queue#
The script simulates 200,000 requests through 16 workers, first come first served, with random arrivals. An /item request takes about 20 ms of worker time and an /export request three times that. The baseline runs the workers at about 55% busy. The queueing world raises the arrival rate until they are about 90% busy. The slow-subset world makes /export twelve times slower than /item instead of three. The mix world sends 5% of requests to /export instead of 2%. It needs Python 3.8 or later and nothing else.
import heapq, random, statistics, sys
WORKERS = 16
def simulate(world, n=200_000, seed=11):
rng = random.Random(seed)
rate = 0.66 if world == "queueing" else 0.40 # arrivals per ms
export_share = 0.05 if world == "mix" else 0.02
free = [0.0] * WORKERS # when each worker is next free
t, busy, rows = 0.0, 0.0, []
for _ in range(n):
t += rng.expovariate(rate)
route = "/export" if rng.random() < export_share else "/item"
work = rng.lognormvariate(3.0, 0.3) # about 20 ms for /item
if route == "/export":
work *= 12 if world == "slow-subset" else 3
start = max(t, heapq.heappop(free)) # first come, first served
heapq.heappush(free, start + work)
busy += work
rows.append((route, start - t, start - t + work))
return rows, busy / (t * WORKERS)
def p(xs, q):
return statistics.quantiles(xs, n=100)[q - 1]
if len(sys.argv) < 2 or sys.argv[1] not in ("baseline", "queueing", "slow-subset", "mix"):
sys.exit("usage: python3 tail.py baseline|queueing|slow-subset|mix")
world = sys.argv[1]
rows, util = simulate(world)
total = [r[2] for r in rows]
item = [r[2] for r in rows if r[0] == "/item"]
export = [r[2] for r in rows if r[0] == "/export"]
wait = [r[1] for r in rows]
print(f"{world:11} util {util:3.0%} | all p50 {p(total, 50):2.0f} p99 {p(total, 99):3.0f} | "
f"/item p99 {p(item, 99):3.0f} | /export p99 {p(export, 99):3.0f} share {len(export) / len(rows):3.0%} | "
f"wait p99 {p(wait, 99):2.0f}")
Save it as tail.py. Before you run it, write down, for each world, whether /item p99 and wait p99 will move. Then run:
for w in baseline queueing slow-subset mix; do python3 tail.py "$w"; done
Python 3.11 printed (all times in ms):
baseline util 55% | all p50 20 p99 59 | /item p99 40 | /export p99 122 share 2% | wait p99 2
queueing util 90% | all p50 24 p99 72 | /item p99 58 | /export p99 129 share 2% | wait p99 33
slow-subset util 64% | all p50 20 p99 235 | /item p99 41 | /export p99 487 share 2% | wait p99 6
mix util 58% | all p50 21 p99 77 | /item p99 41 | /export p99 121 share 5% | wait p99 3
Total p99 rose in every world and p50 moved by at most 4 ms. Only the queueing world moved /item p99 and the wait time. The slow-subset world moved /export p99 alone, and the mix world moved the share alone.
The prediction fails if /item p99 rises in the slow-subset or mix world as much as it does in the queueing world, or stays flat in the queueing world. Change seed=11 to another value to repeat it; with seed=3 the same columns moved. Raise the queueing rate from 0.66 to 0.70 and the median starts to move as well (about 27 ms here), which is the point where queueing stops hiding from p50.
The slow-subset world also shows where the separation weakens. Slow /export requests hold workers longer, so utilisation rose from 55% to 64% and the wait p99 from 2 to 6 ms. A slow subset that is large enough pushes the whole service into queueing, and then both explanations are true at once.
Read it on a real system#
The observations need a latency histogram labelled by route, a request count by route, and either the time requests wait before a worker picks them up or the number of requests in flight. With Prometheus histograms, the p99 of each route is one query. histogram_quantile estimates from bucket counts, so read a change in the estimate, not its exact value. histogram_quantile
histogram_quantile(0.99,
sum by (le, http_route) (rate(http_server_request_duration_seconds_bucket{service="ledger-api"}[5m])))
The share of each route:
sum by (http_route) (rate(http_server_request_duration_seconds_count{service="ledger-api"}[5m]))
/ scalar(sum(rate(http_server_request_duration_seconds_count{service="ledger-api"}[5m])))
OpenTelemetry's HTTP conventions define http.route as a low-cardinality route template, and an opt-in http.server.active_requests counter of requests in flight. Divide in-flight requests by the worker count per instance to get how full the workers are; a value near 1 means new requests wait. OpenTelemetry HTTP metrics The exported metric names depend on the exporter and library, so use your own.
A slow subset can hide inside one route. Split the slow route again by tenant or by request size (the same conventions define http.server.request.body.size) before naming a cause. One customer says it is slow
Read the per-route p99 per instance before averaging anything. Percentiles from different hosts cannot be averaged into a fleet percentile, and a single hot instance can look like fleet-wide queueing. Averaging percentiles across hosts
Limits#
Queueing that happens before the server's timer starts does not show in server-side latency. A request that waits in a load balancer, in the kernel's accept queue or in a client's connection pool is invisible to a histogram recorded by the application. The fast route's p99 then stays flat while users wait. Compare the client's or load balancer's latency with the server's for the same minutes.
A shared dependency that got slower for one route's queries looks like a slow subset; one that got slower for everyone looks like queueing on the fast route, because every request spends longer in it. Read the dependency's own time per call before calling it queueing.
The three explanations can combine, as the slow-subset run showed. When the fast route's p99 and one route's p99 both rose, find which resource ran short with the method in Find the saturated resource under load. For the general method of choosing the observation, see Choose the observation where explanations disagree.
Questions#
Why does queueing raise p99 before p50?
With many workers, most requests arrive when at least one is free and wait for nothing. Only the requests that arrive while every worker is busy wait, and at moderate utilisation those are a small fraction, so they show up in the tail first. In the lab at 90% busy the median rose 4 ms while the p99 wait reached 33 ms.
How do I tell a slow subset from a change in traffic mix?
Compare the slow route's own p99 and its share of requests over the same minutes. A slow subset raises the route's p99 with its share flat; a mix shift raises the share with its p99 flat.
Can p99 rise from queueing when CPU looks low?
Yes, if the workers are the scarce resource. A worker waiting on a database or another service holds its slot without using CPU, so a pool of busy workers can make requests queue at low CPU. Read worker occupancy, not CPU, for that case.