Sub topic 9
Fan-out and tail latency amplification
This chapter assumes you know what percentiles are, how a request moves through services and databases, and how to read short Python scripts. It builds on the latency budget chapter: a budget that looks fine on paper can fail once a request calls many backends at once.
What fan-out is
Fan-out means one incoming request triggers several backend calls. A search page might query 20 index shards. A product page might call pricing, inventory, reviews and recommendations. If the response needs all of those answers, the request finishes only when the slowest call finishes.
That rule is the source of tail amplification. Each backend is usually fast. Occasionally it is slow. The more calls you wait for, the more likely it is that at least one of them is slow.
The arithmetic
Suppose each backend meets its p99 target. Then 99% of its calls are fast and 1% are slow. If a request waits for n independent calls, the chance that all of them are fast is 0.99 raised to the power n. The chance that at least one is slow is the remainder.
# 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 calls, about 63% of user requests hit at least one backend's slow 1%. A backend's p99 has become close to the typical experience of the whole request.
What this means for targets
The calculation assumes independent calls. Real systems often have correlated slowness, such as a shared database, a garbage collection pause or a network problem. Correlation can make the result better or worse than the formula suggests, so treat the formula as a planning estimate and confirm it by measurement.
Three consequences follow for target setting:
- Backend targets must be tighter than the end-to-end target suggests. If the user-facing p99 must hold with 100 parallel calls, each backend needs a percentile well beyond p99, such as p99.9 or p99.99, to be in its target range.
- The mean and even the median of a backend say almost nothing about the user's experience. Measure the high percentiles of every dependency that sits in a fan-out.
- Per-component p99 values do not add up to the end-to-end p99. Your latency budget is a planning sum. Keep headroom in it, as in the budget below.
# 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 70 ms of headroom is not waste. It absorbs the amplification that the sum ignores.
Common ways to reduce amplification
- Reduce the fan-out. Fewer calls per request means a lower chance of hitting a slow one. Cache results, merge calls or precompute data.
- Set per-call timeouts. A timeout caps how long the slowest call can hold the request. Choose it from the budget, not from a default.
- Hedge requests. Send a duplicate call to another replica if the first has not answered after a short delay, usually near the backend's p95. Use whichever answer arrives first. This cuts the tail but adds load, so cap the share of hedged calls.
- Return partial results. If a non-essential call is late, answer without it. A page without recommendations is usually better than a slow page.
- Isolate slow components. Use separate pools or limits so one slow dependency does not consume all workers.
Common errors
- Reading a backend's p99 as the end-to-end p99 for a fan-out request.
- Checking only averages for dependencies in the critical path.
- Treating the independence assumption as fact when calls share a resource.
- Setting timeouts longer than the latency budget, so the budget cannot hold when a call is slow.
Exercise
A page request calls 50 backends in parallel, and each backend meets its p99 target. Using 1 - 0.99^50, about what share of page requests will see at least one slow backend call? Name two ways to lower that share.
Show answer
1 - 0.99^50 is about 0.395, so roughly 40% of requests. Ways to lower it include reducing the number of calls (caching or merging), tightening each backend's tail target (for example to p99.9), hedging slow calls, or returning partial results when a non-essential call is late.