Measure p95 / p99, not the average
Average latency looks fine while a slice of users wait seconds; a page that fans out to 100 services is as slow as the slowest one (tail latency amplification).
Say it like this
I design and alert on p99, not the mean, because a feed that fans out to a hundred shards is as slow as its slowest one on almost every request.
Basic solution
Track percentiles per endpoint and per dependency. Cut the tail with hedged requests (send a second copy after p95 and take the first reply), tighter timeouts, and fewer fan-out calls.
Hedging a slow shard
The feed service calls 30 shards. One is in a GC pause. After the p95 delay a hedge goes to the replica of that shard, which answers first; the original is cancelled.
How it works
- Every request records its duration into a histogram, one per endpoint and one per downstream call. A histogram counts requests into latency buckets, which lets you compute percentiles later without storing every individual sample.
- Dashboards show p50, p95 and p99, and alerts fire on p99 against the SLO. The mean is not used because a few very slow requests barely move it, while they are exactly what users complain about.
- For a request that fans out to N backends, the request is only done when the slowest backend replies. If each backend is slow with probability p, the chance that at least one is slow is 1 minus (1 minus p) to the power N. With a 1% slow rate and 100 backends that is 63% of requests. This is tail amplification: a rare slowness per call becomes a common slowness per request.
- To cut the tail, give each call a deadline close to that backend's p99, and send a hedged request: if the first copy has not replied by the p95 time, send the same request to another replica, take whichever answers first, and cancel the other.
- Reduce N itself: batch several calls into one, keep all of a user's data on one shard so a request touches one node, and cache the assembled result so most requests make no calls at all.
Where tails come from
- Garbage collection pauses, queueing behind one slow request, cache misses, disk stalls, a cold connection that needs a handshake, a retry after a timeout.
Worked example
News feed read
- Hydrating one feed page fetches 20 post bodies and 20 author profiles from caches sharded over 50 nodes, so a single page touches about 30 shards.
- Each shard has p50 = 0.5 ms and p99 = 20 ms (a garbage collection pause). Per request, the chance at least one of the 30 shards is slow is 1 minus 0.99 to the power 30, about 26%. So although every shard answers in 0.5 ms most of the time, one feed read in four takes 20 ms, and the feed p75 is already 20 ms.
- Hedging: send a second read to the replica of any shard that has not replied after 2 ms (about the shard p95). The slow shard now only hurts when its primary and its replica are both slow at the same instant: 0.01 x 0.01 = 0.0001 per shard, about 0.3% per request. The feed p99 drops from about 25 ms to about 3 ms.
- Extra load from hedging: only calls that are still waiting after 2 ms get hedged, about 5% of them, so backend load rises 5%, not 100%.
- Ride matching: the ETA call to a road graph service has a p99 of 300 ms. Matching sets a 150 ms deadline and falls back to straight-line distance if it is missed, so the match decision p99 stays under 200 ms instead of inheriting the ETA service's tail.
Variants and when each wins
Hedged requests
- If a call has not replied by its p95 time, send a duplicate to another replica and take the first reply. It is the best general tool, and it needs the call to be a read that is safe to run twice, plus spare capacity to absorb the duplicates.
Tied requests
- Send the request to two replicas at once, and whichever starts work first tells the other to drop it. It gives a lower tail than hedging because there is no waiting period, but it needs that cancel message, so it is used inside storage systems rather than between services.
Deadlines with fallbacks
- Stop waiting at a fixed time budget and return a degraded answer: a cached value, an approximation, or a partial result. It wins when hedging cannot help, because it bounds the tail no matter what the dependency does.
Fewer, bigger calls
- Keep a user's data on one shard so a request touches one node, batch the calls per shard, and cache the assembled page. It wins by removing the amplification rather than treating it.
Recommendation: measure percentiles per dependency first, then reduce fan-out, then hedge the calls that still dominate p99.
Failure modes
Averages hide the problem
- You notice dashboards are green while users complain about slow pages. It happens because the mean latency is fine and the 99th percentile is seconds, and nobody is looking at the 99th percentile. Fix it with histogram metrics, p99 on dashboards and alerts, and a latency SLO stated as a percentile.
Hedging makes an overload worse
- You notice a slow backend gets slower once hedging turns on. It happens because the backend is slow due to overload, and each hedge adds another request to it. Fix it by capping hedges to a few percent of requests and switching hedging off when the backend's error rate or latency budget is exhausted.
Retries at every layer
- You notice tail latency multiplies and backends see three to four times the normal load during incidents. It happens because the client, the gateway and the service each retry the same failed call. Fix it by retrying at one layer only, with a budget on how many retries are allowed.
Deadline too long
- You notice threads sit for seconds waiting on a dependency that will never answer in time. It happens because the per-call deadline was set far above the dependency's p99. Fix it with deadlines close to the dependency p99, propagated downstream so every call in the chain stops early too.
Garbage collection and compaction pauses
- You notice periodic p99 spikes that come from specific nodes. It happens because those nodes pause for garbage collection or compaction and every request in flight on them waits. Fix it by tuning the runtime, keeping caches off the garbage-collected heap, staggering compaction across nodes, and letting hedging route around a paused node.
Interviewer follow-ups
Answer out loud before you open each one.
Why does fan-out amplify tail latency?
Because the request finishes when the slowest sub-call finishes. If each of N calls is slow with probability p, the request is slow with probability 1 − (1 − p)^N, which grows fast with N: a 1% tail per call becomes a 26% tail at 30 calls and 63% at 100.
When is a hedged request a bad idea?
When the call is not idempotent, when the backend is slow because it is overloaded rather than because one replica is unlucky, or when you have no spare capacity. Cap hedges at a few percent of traffic and turn them off under backend pressure.
How do you compute p99 across many servers?
Each server exports a histogram with fixed buckets; the aggregator sums bucket counts across servers and interpolates the percentile. You cannot average per-server p99 values; that number is meaningless.
What latency target would you set and for which percentile?
Set the SLO at the percentile users feel: for an interactive endpoint something like p99 under 300 ms over a rolling window. Internal dependencies get tighter budgets so the end-to-end p99 still fits.
Connections
The same parts on other clouds
The diagrams above name AWS services as one concrete example. Every part has a close equivalent elsewhere. In an interview, name the role first, such as “a managed Redis cache”, then the product you would pick.
| AWS | Used here as | Google Cloud | Microsoft Azure | Open source and others |
|---|---|---|---|---|
| Amazon EC2 Auto Scaling | Feed service | Compute Engine managed instance groups | Virtual Machine Scale Sets | Kubernetes Deployment with a HorizontalPodAutoscaler, Nomad |