TAIL LATENCY
Your p99 is someone's every request
Each backend is slow on 1% of calls. A request fans out to 20 backends and waits for all of them. Roughly what fraction of requests is slow?
A backend that is slow 1% of the time looks healthy on its own. Its p99 sits right at the edge of the slow path. Now put it behind a fan-out. The parent sends one request to N backends and waits for all of them, so the parent is slow if any one call is slow. That happens with probability 1 - 0.99N: 18% at N = 20, 63% at N = 100.
You cannot make every backend perfect. You can stop waiting for the slow one. A hedged request sends a backup to another replica once the first call is late and takes whichever reply arrives first.
// wait for every shard
const parts = await Promise.all(shards.map((s) => s.query(q)));
// latency = the slowest of N callsasync function hedged(q, replicas, delayMs) {
let done = false;
const first = replicas[0].query(q).finally(() => { done = true; });
// send the backup only if the primary is still silent after delayMs
const backup = sleep(delayMs).then(() => (done ? first : replicas[1].query(q)));
return Promise.any([first, backup]); // first answer wins
}
const parts = await Promise.all(shards.map((s) => hedged(q, s.replicas, P95_MS))); Watch the parent’s p99 and the probability box. WAIT FOR ALL measures 18% slow parents against a formula of 18.2%, and the p99 lands in the slow path. HEDGED REQUESTS runs the same backends, with the same 1% slow calls, and holds p99 at 30 ms for about 1% extra load. The load is that low because fast calls here never pass 15 ms, so the hedge fires only for the slow 1%. With real latencies, where fast and slow overlap, a hedge at the p95 costs about 5%.
Every backend meets a 50 ms p99 on its own: only 1% of its calls are slow. Start at FANOUT 1 and raise it. Find the smallest fan-out at which the parent's p99 breaks the 50 ms SLO.
GOAL: p99 > 50 ms at this fan-out, ≤ 50 ms at every smaller one
Write hedge(request, backends, delay). backends[i].call(request) sends the request to replica i and returns how many ms its reply takes. Return the ms at which the caller has its first reply. Send to backends[0]; only if that reply takes longer than delay, send one backup to backends[1] at time delay. Backups are extra load: do not send one when the primary answers in time.
Search and feed services that scatter to shards, microservice pages that call a dozen services, distributed databases reading from replicas. Bigtable, Cassandra speculative retry, and gRPC hedging policies all send backup requests. The usual culprits behind the slow 1% are GC pauses, compaction, a noisy neighbour, or a cold cache.
“Every service meets its p99, but the page is slow. Why?” Compute 1 - 0.99^N for the fan-out, explain why the parent sees the tail, and propose hedged or tied requests after the p95. Mention the cost (a few percent extra load) and that backups must go to a different replica and be safe to repeat.
USED IN
Chapters that build on this lesson.
- Histograms, percentiles and why you cannot average themObservability
- Inside a switch: routing, timeouts, PIN translation and stand-inPayments
- Machine-learning fraud scoring at authorisation: features, latency budgets and false declinesPayments
- Performance and load testingQA and Quality Engineering
- Performance and load tests as part of the suiteSoftware Testing and Quality
- Capacity and costSRE, Platform and DevOps