Zum Hauptinhalt springen

Racing the Tail: Hedged Requests and When to Send a Second Copy

· 10 Minuten Lesezeit
Clustron Team
Engineering für verteilte Systeme

Here is a latency fact that does not go away no matter how fast you make the common case. Every replica in a cluster occasionally stalls — a garbage-collection pause, a compaction, a page fault, a few hundred microseconds sharing a core with a noisy neighbor. For any single request the odds of hitting one of these are small, so the median is beautiful. But the stalls are frequent enough to show up at the ninety-ninth percentile, and the ninety-ninth percentile is often the number in the SLA.

It gets sharper the moment one user action needs more than one request. If a page assembles from twenty reads and each read independently has a 1% chance of landing on a stalled replica, the chance that at least one of the twenty is slow is not 1% — it is about 18%. The slowest of the twenty sets the response time, so the tail you occasionally saw on one request becomes the tail you routinely see on the whole page. You cannot fix this by making the fast path faster; the fast path was never the problem. The problem is the straggler, and the straggler is somewhere different every time.

Hedging attacks the straggler directly, and it does so without knowing in advance which replica will be slow. The idea: send the request to one replica, and if it has not responded by the time it is already running late — say, by its own p95 — send a second copy of the request to a different replica, then take whichever response arrives first and ignore the other. You are not retrying a failure; the first request has not failed, it is merely slow, and you are refusing to wait on it when a fresh copy against a different replica will very likely beat it.

Why a second copy collapses the tail​

The power of hedging comes from independence. A single request's latency has some probability of being slow — call it the p95 threshold, so 5% of requests cross it. When a request does cross it and you fire a second copy, that second copy is a fresh draw against a different replica, with its own independent chance of being slow. For the hedged request to still be slow, both copies have to land on a slow path — and if the two draws were independent, the combined probability of a long wait drops toward the product of the two, not their sum.

That is the whole trick: the tail of "the faster of two independent copies" is dramatically thinner than the tail of one. In the measurements Dean and Barroso reported in The Tail at Scale, hedging a read after the 95th percentile cut the 99.9th-percentile latency by roughly a factor of two while adding only a few percent to the total request count — because, by construction, you only ever send the second copy for the ~5% of requests that were already running late.

One copy: you wait on whichever replica you happened to pickreplica Aslow ✕client waits the full stall — this is your p99Hedged: at the p95 threshold, send a second copy and take the first replyreplica Acancelledreplica BB returns first ✓p95 threshold: fire the hedge heret0latency = threshold + B

Pay for it once: tied requests​

The naive version has a cost worth naming: for the hedged fraction of requests, two replicas do the work and one result is thrown away. At a 5% hedge rate that is only a few percent of wasted work, but it is not free, and under some workloads the duplicated work is exactly what you cannot spare.

Tied requests shrink the waste to almost nothing. Instead of the second copy racing blindly, the two copies know about each other: each carries the identity of its sibling, and the first replica to actually begin executing the request sends a cancellation to the other before it starts. The loser drops the request from its queue rather than running it to completion. Because the expensive part is the execution, not the enqueue, cancelling before execution means the duplicate costs a near-empty queue slot instead of a full operation. You still get the tail-collapsing effect of two independent draws, but you pay for roughly one unit of work instead of two.

// Hedge a read: fire the primary, and a second copy only if it runs late.
var primary = SendTo(replicaA, req);
var hedged = Task.Delay(p95Threshold) // only the slow ~5% ever reach here
.ContinueWith(_ => SendTo(replicaB, req));

var winner = await Task.WhenAny(primary, hedged.Unwrap());
Cancel(theOther(winner)); // tied: loser drops it before executing

// This is only safe because the operation is safe to run twice.
// A read is naturally idempotent. A write needs an idempotency key so the
// loser — if it ran anyway — is deduplicated at the resource, not double-applied.

That last comment is the hard constraint. Hedging sends the same operation to two places, so the operation must be safe to run twice. Reads are naturally safe — running a GET on two replicas and keeping one answer changes nothing. Writes are not, unless you have already made them idempotent: a hedged INCR with no deduplication double-counts exactly the way a retried one does, so a hedged write needs an idempotency key and dedup at the resource, or it needs to stay un-hedged. In practice hedging earns its keep on the read path, where partition-aware routing already knows which replicas hold a copy and the second copy is just a different replica of the same partition.

The one place it backfires: overload​

Hedging has a failure mode that is the mirror image of its benefit. It works because the second copy is cheap relative to the spare capacity in the system — a few percent more load buys a lot of tail reduction. But when the whole system is saturated, latencies rise everywhere, which means more requests cross the hedge threshold, which means more second copies, which adds load to a system that is already underwater. That is a classic metastable feedback loop: the mechanism meant to shave the tail becomes the engine that sustains an outage.

The defense is to make hedging a budget, not a reflex. Cap the extra load it may add — a common rule is to allow hedged copies for at most a small fraction (say 1–2%) of total requests, tracked in a rolling counter, and to simply stop hedging once that budget is spent. Pair it with the same backpressure that protects the rest of the system: a hedge that would have to queue behind real work is a hedge not worth sending. Hedging should spend slack when there is slack and vanish when there is not.

Wrap-up​

Tail latency is set by stragglers, and stragglers move — no single replica is reliably the slow one, so you cannot route around them in advance. Hedging exploits exactly that: send a second copy of an already-late request to a different replica and take the first reply, and because the two copies are independent draws the combined tail collapses, usually for only a few percent of added load aimed only at the requests that were running late. Tied requests cut the duplicated work to near-zero by letting the first replica to start cancel its sibling, and the whole technique rests on the operation being safe to run twice — free for reads, requiring an idempotency key for writes. The single rule to respect is that hedging spends spare capacity, so it must back off under overload or it will feed the very congestion it was meant to hide. Used that way, it is one of the cheapest ways to pull in a p99 — and a measurement that still kept sampling through the stalls is the only way you will see that it worked.