Sub topic 6
Splitting a latency budget across the request path
This chapter assumes you know the request path of a typical backend (client, load balancer, service, database) and can read short Python. You should also know what a percentile is, such as p99, from the earlier chapter on percentiles.
What a latency budget is
A user-facing target applies to the whole request, for example "finish within 300 ms at p99". The request passes through several components, and each one adds time. A latency budget divides the end-to-end target into a share for each component. Each team then has a number it can measure and defend.
Without a budget, every team assumes the others are fast. Without one, a slow database query and a slow gateway can each look acceptable alone, and the sum still misses the target.
Map the request path first
List every hop the request makes on the critical path. A hop is on the critical path if the response waits for it. Work done after the response is sent, such as writing an analytics event, does not count.
Allocate shares and keep headroom
Start from the end-to-end target. Give each component a share based on measured or expected cost, and keep the unallocated remainder as headroom. Headroom covers variance, retries and things you forgot.
# Latency budget for a request that must finish within 300 ms at p99
budget_ms = 300
allocation_ms = {
"edge_and_network": 20,
"api_gateway": 10,
"application_service": 80,
"database": 120,
}
used = sum(allocation_ms.values())
headroom = budget_ms - used
print(used, headroom) # 230 70
assert headroom >= 0, "allocation exceeds the end-to-end budget"The allocations sum to 230 ms, which leaves 70 ms of headroom. The assert turns an over-allocated budget into a failure you see right away, instead of a surprise in production.
A common way to choose the shares:
- Measure each component today, at the percentile you target.
- Give the largest, least flexible component (often the database) the largest share.
- Give each component slightly more than its measured value, so normal variation does not break it.
- Keep 15% to 25% of the total unallocated.
Component p99 values do not add up exactly
The planning sum above is a simplification. The p99 of a sum of latencies is not the sum of the p99 values. Slow requests rarely hit the slow tail of every component at once, so the sum of p99 values usually overstates the end-to-end p99. In practice the budget is a planning tool, and you confirm the real end-to-end percentile by measuring it.
Parallel calls need tighter targets
If a request waits for several backend calls in parallel, the slowest call decides the latency. The more calls you wait for, the more likely one of them lands in its slow tail.
# Chance that at least one of n parallel backend calls is slower than its p99
p_fast = 0.99
for n in (1, 10, 100):
p_slow_somewhere = 1 - p_fast ** n
print(n, round(p_slow_somewhere, 3))
# 1 0.01
# 10 0.096
# 100 0.634With 100 parallel calls, about 63% of requests hit at least one call slower than its p99. If you want the end-to-end p99 to hold, each backend needs a target at a higher percentile, such as p99.9, or a smaller budget share. The later chapter on fan-out covers this in detail.
Common errors
| Mistake | Effect | Fix |
|---|---|---|
| Allocating 100% of the budget | Any variance breaks the target | Reserve headroom |
| Budgeting averages | The tail misses the target | Budget at the target percentile |
| Counting off-path work | Components get budgets they do not need | Include only the critical path |
| Never revisiting the split | Shares drift from reality | Compare budget with traces regularly |
| Ignoring retries and timeouts | A retry doubles a hop's time | Set timeouts below the share, and count retries |
Timeouts follow the budget
A component's share should also set its timeout. If the database share is 120 ms, a client timeout of 5 seconds means one stuck query can consume the entire request budget many times over. Set timeouts close to the share, plus a small margin. Then a slow dependency fails fast, and the caller can retry or return a fallback inside the remaining budget.
Exercise
A request has a 400 ms p99 target. The allocations are network 30 ms, gateway 20 ms, service 100 ms and database 200 ms. How much headroom remains, and is the allocation acceptable?
Show answer
The allocations sum to 350 ms, so 50 ms (12.5%) remains. It is valid, because it does not exceed 400 ms, but the headroom is thin. A rule of thumb is 15% to 25%, so reduce a share or ask whether the target can grow.
Exercise
Why is the sum of per-component p99 values not the end-to-end p99, and what should you do about it?
Show answer
Slow tails in different components rarely coincide on the same request, so the sum usually overstates the end-to-end p99. Use the sum as a planning figure and measure the real end-to-end percentile with tracing or load tests.