Aller au contenu principal

The Tail at Scale: Why p99 Becomes p50 When You Fan Out

· 10 minutes de lecture
Clustron Team
Ingénierie des systèmes distribués

Profile a single shard and the latency looks healthy. Say the median response is two milliseconds and the 99th percentile is twenty — one request in a hundred is slow, the rest are quick, and that is a perfectly ordinary, perfectly acceptable distribution. You would ship it without a second thought. The trouble starts when you stop sending one request at a time and start sending requests that fan out: a scatter-gather across partitions, a multi-key read whose keys live on different shards, a search that must consult every slot. A request like that cannot return until every branch has answered, and that single fact changes everything about the latency you actually experience.

Here is the arithmetic that catches people off guard. A fan-out request is as slow as its slowest branch, and the slowest of many samples comes from the tail of the distribution, not its center. If each branch is independently slow with probability one percent, the chance that at least one of a hundred branches is slow is one minus ninety-nine-hundredths to the hundredth power — about sixty-three percent. So almost two out of every three wide requests will contain at least one slow branch, and because you wait for all of them, that branch sets the clock. The twenty-millisecond tail you dismissed as a one-percent event is now the typical latency of your fan-out request. This is what Dean and Barroso named "the tail at scale."

The instinct when latency is bad is to make things faster on average — cache more, serialize less, tighten the hot path. For a fan-out request that instinct is almost entirely wasted effort, because the request does not care about your average; it cares about your tail, and the two can move independently. This post is about why that is, and what actually works instead.

The slowest branch sets the clock​

The core of it is that a scatter-gather returns the maximum of its branch latencies, not the mean. When you wait for all N responses, the one that arrives last is the one that matters, and every other branch's speed is irrelevant to the request's total time. Improving the median of each shard — making the common case faster — does nothing to that maximum if the tail is unchanged, because the maximum is drawn from the tail. A hundred shards that are each quick ninety-nine percent of the time will, in aggregate, hand almost every wide request at least one of their rare slow responses, and the request inherits it.

Fan-out: the request waits for the slowest shardS1S2S3S4S5S6request doneone slow shard gates all six — the request inherits the tailHedge the straggler to a replica, take the first to answerS1S2S3S4hedge →S5S6done earlierthe replica answers before the straggler would have — tail cut

Why a better average is the wrong fix​

Most performance work targets the mean, because the mean is what a throughput benchmark and a flame graph show you. But a fan-out of width N routinely samples your 99th percentile when N is around a hundred, and your 99.9th when N is around a thousand — so the number you must drive down is the high percentile, not the average. A change that halves your median and leaves the tail alone does nothing for wide requests, because the tail is still there to be sampled. Worse, some classic average-latency optimizations actively grow the tail: larger batches, deeper buffering, and more aggressive coalescing all improve throughput and mean latency while making the occasional slow request slower. On the dashboard the average improves; in production the fan-out requests get worse, and the two facts are the same change seen from different ends.

Hedge the straggler​

The key realization is that you do not need every shard to be fast — you need to not wait for the one that happens to be slow on this particular request. Slowness in a healthy system is usually transient and per-request: a garbage-collection pause on one node, a momentary queue, a context switch at the wrong instant. It is not that one shard is permanently slow; it is that some shard is slow each time, and rarely the same one twice in a row. That is exactly the condition a hedged request exploits. Send the request; if a branch has not answered within, say, your 95th-percentile latency, send a second copy to a replica and take whichever returns first. Because the slowness was transient, the replica almost always answers quickly, and the request escapes the straggler.

// Fire the primary. If it is slow, race a replica and take the winner.
primary = replicaA.ReadAsync(key)
if (await primary.CompletesWithin(p95))
return primary.Result // the common case: no hedge needed
hedge = replicaB.ReadAsync(key) // only for the slow tail (~a few %)
return await FirstToComplete(primary, hedge) // take the winner, cancel the loser

A tied request refines this: tell the two copies about each other so that the moment one begins executing, the other is cancelled, which avoids doing the work twice in the common case where neither has started. The cost of hedging is a few percent of extra traffic — you only ever hedge the slow tail — and the return is a dramatic cut in the high percentiles. It does require replicas to hedge against, which is exactly what replica reads already provide.

Shrink the fan-out, bound the tail​

Two structural levers finish the job. The first is to make N smaller, because the amplification is exponential in the width: halving the number of shards a request touches helps far more than halving each shard's latency. If a request fans out to a hundred shards only because its data is scattered, co-locating the keys that are read together — so the request touches ten shards instead of a hundred — drops the probability of hitting a straggler off a cliff. The cheapest tail is the branch you never had to wait on, and partition-aware routing that avoids needless cross-shard hops is the same lever.

The second is that the component tail you are hedging against must itself be bounded, or there is no percentile to hedge at. An unbounded queue lets a shard's latency climb without limit under load, and a component with no worst case defeats every technique above, because the "replica" you hedge to is just as likely to be stuck in the same unbounded queue. This is why admission control and bounded queues matter here: a fast, honest "busy" gives each component a worst case, and a worst case is precisely what makes the tail hedgeable. The tail-at-scale techniques all assume each component's slowness is transient and bounded — backpressure is the discipline that makes that assumption true.

Wrap-up​

Fan a request out across many components and wait for all of them, and you stop experiencing each component's typical latency and start experiencing its tail, because the slowest of N branches sets the clock and the slowest of many samples lives in the tail. At a hundred-way fan-out, a shard's 99th-percentile behavior becomes the request's median behavior — which is why pushing down the average accomplishes nothing and pushing down the high percentiles accomplishes everything. Hedge the stragglers against replicas so one transiently slow branch cannot hold the whole request hostage; shrink the fan-out by co-locating what is read together, since the amplification grows with width; and bound each component's worst case with admission control so there is a tail to hedge against in the first place. A wide request is only ever as fast as its slowest piece, so the entire craft is making sure no single piece can stay slow for long.