Cloud & Infrastructure · Paper 03 / 05

The Tail at Scale

Jeffrey Dean, Luiz André Barroso

2013 · Communications of the ACM · ~11 min read

Read the original paper

This article shows why a request that waits for a hundred servers is usually slow even when each server rarely is, and describes hedged requests, tied requests and other ways to tolerate it. Read it before you design any fan-out or set a percentile target.

A team runs the leaf servers for a search index. Each leaf answers in a few milliseconds most of the time and in about a second one time in a hundred. The leaves are healthy: nothing is crashing, no machine is overloaded, and the slow one is a different machine each time. Yet the search page is slow for most users. Adding servers does not help, and there is no bug to fix.

Jeff Dean and Luiz André Barroso’s 2013 article explains why this happens and what Google did about it. Its main point is that in a large system, variability in response time is a property of the whole system, like failure, and has to be engineered around in the same way. After this page you should be able to compute how a single server’s tail becomes a request’s tail, explain what hedged and tied requests do to that number and what they cost, and choose among the other techniques the article describes.

We will use one setup throughout: a root server that fans each user request out to 100 leaf servers and waits for all of them. Each leaf is slow, meaning about one second, with probability 1 per cent.

The arithmetic of waiting for everyone

Predict first

Each of 100 leaves is slow 1 per cent of the time, independently. The root waits for all of them. What fraction of user requests are slow?

Show the worked answer

A request is fast only if every leaf is fast. The chance of that is 0.99 multiplied by itself 100 times, which is about 0.366. So about 63 per cent of user requests wait for a slow leaf. A rare event at one server becomes the usual case for the request.

The general rule is that if each server is slow with probability p and a request needs n of them, the request is slow with probability 1 − (1 − p)n. The article’s own example is the one above: with a one-second 99th percentile at each server, 63 per cent of 100-way fan-out requests take more than a second.

Now change the variables. Make each server much better: slow only one time in 10,000. Make the fan-out larger: 2,000 servers. The chance that a request is fast is 0.9999 to the 2,000th power, which is about e−0.2, or 0.82. Almost one request in five is still slow. Improving each server by a factor of a hundred did not make the problem go away, because the fan-out grew too. That is the article’s point in one calculation.

one server · 1% of calls slow1 request in 100 waitsfan out to 100 servers · each 1% slow63 requests in 100 wait for someone1 − 0.99¹⁰⁰ = 0.63 · the slow one is a different server each time, so there is nothing to fixthe paper’s own arithmetic
A 1 per cent chance of slowness at one server becomes a 63 per cent chance for a request that has to wait for a hundred servers.
Try itHow rare slowness becomes the common case

Set how often a single server is slow and how many servers one user request fans out to. The chart shows the chance that the request waits for a slow one.

0%20%40%60%80%100%1e01e11e21e3servers touched by one request (log)% of requests slow
plain fan-outhedged after the 95th percentile
chance a request is slow63.4%1 − (1 − 0.010)^100
with hedging1.00%both replicas must be slow
extra load from hedging~5%backups are sent only for the slowest 5%

At 100 servers, a one-in-a-hundred hiccup becomes the typical experience: most requests wait for someone's garbage collector, disk scrub or background compaction. The paper's answer is not to eliminate variability, which is impossible, but to stop one slow server from deciding the outcome.

The article also gives a measurement from a real Google service in which a root server distributes a request through intermediate servers to many leaves. The 99th-percentile latency for one leaf request was 10 ms. The 99th-percentile latency for all leaf requests of a user request to finish was 140 ms. And the 99th-percentile latency for 95 per cent of them to finish was 70 ms. Waiting for the last 5 per cent of leaves doubled the tail. Keep that number; it comes back when we discuss returning partial results.

Where the slow one per cent comes from

The article lists the causes, and what matters is what they have in common. Machines are shared, so another process can take the CPU, the memory bandwidth or the network for a while. Background daemons wake up. Storage systems compact their files. Garbage collectors pause. Queues fill briefly at several levels, in the server, the operating system and the disk. Processors throttle themselves when they get hot or hit power limits, and come out of energy-saving states slowly.

Almost none of these depend on the request. The same query sent to the same leaf a few milliseconds later would probably be fast. A common misreading is that the tail comes from a few bad servers that should be found and removed. Usually it comes from good servers having brief bad moments. That distinction is why redundancy works: if slowness is transient and independent of the request, a second copy of the request elsewhere will probably not hit a bad moment at the same time.

Reducing variability inside one server

The article first describes ways to make each component steadier, while admitting that they cannot remove the tail. Use service classes and prioritise interactive requests over background ones. Keep low-level queues short, because the operating system or disk cannot reorder by priority if requests are already queued beneath the server’s scheduler. Break long-running requests into smaller ones so that a cheap request does not wait behind an expensive one.

One technique is counter-intuitive. Suppose each of our 100 leaves runs a one-second background compaction at a random time once a minute. Then at any moment about one leaf in sixty is busy, and a 100-way request almost always hits one. If all the leaves do their compaction in the same one-second window, the requests in that window are slow and every other request is fast. Synchronising the disruption across machines shrinks the share of affected requests from most of them to about one in sixty.

Hedged requests, worked through

The article compares latency to fault tolerance. Reliable systems are not built from components that never fail; they are built from redundant components and routing around the ones that do. A slow reply can be treated like a failure: have more than one replica that can answer, notice when one is taking too long, and ask another.

The simplest form is the hedged request. Each leaf’s data is held by at least two replicas. The root sends the request to one. If no answer has come back by the 95th-percentile latency for that kind of request, it sends the same request to a second replica, uses whichever reply comes first, and cancels the other.

0 ms10 ms20 ms30 ms40 ms50 msreplica 1busy: this call would take 40 ms (a 1-in-100 case)replica 28 msp95 passed: send a backupanswer at 18 ms · cancel replica 1
A hedged request. The first replica is slow; once the wait passes the 95th percentile, the client sends a copy to a second replica, takes its fast answer and cancels the first.

Work it through for one leaf. Without hedging, the leaf is slow 1 per cent of the time. With hedging, it is slow only if the first replica is slow and the second replica is slow as well. If those are independent, that is 1 per cent of 1 per cent, or 0.01 per cent. For 100 leaves, the chance that some leaf is slow falls from about 63 per cent to about 1 per cent. The extra load is small because only requests that have not answered by the 95th percentile send a second copy, at most 5 per cent more requests.

There is a floor to this improvement. A hedged leaf cannot answer before the hedging delay plus the second replica’s response time, so hedging turns a one-second outlier into something nearer twice the 95th percentile. That is a large win, but not a fast request. The article reports a benchmark in which a client read 1,000 keys from a Bigtable spread over 100 servers. Sending a hedge after 10 ms reduced the 99.9th-percentile latency from 1,800 ms to 74 ms while sending only about 2 per cent more requests.

Hedging fails when its assumption fails. If a request is slow because it is expensive, such as a query that matches a huge posting list, the second replica is just as slow, and the system has now done the expensive work twice. If the whole cluster is overloaded, hedges add load where there is none to spare, and slowness spreads. That is why production hedging policies, such as the one in gRPC, put a cap on extra attempts and throttle hedging when the error or retry rate rises. Hedging is also only safe for requests that can be repeated without harm, such as reads.

Put numbers on the floor. Say a leaf’s median is 5 ms and its 95th percentile 10 ms. A request that hits a slow moment on the first replica now waits 10 ms, sends the hedge, and gets the second replica’s answer about 5 ms later. The slow leaf costs about 15 ms instead of a second. That is what turns the tail from a cliff into a step.

The independence assumption carries a lot of weight in the calculation above, so change it. Suppose both replicas of a leaf sit behind the same top-of-rack switch, or share a machine with the same noisy batch job, so that when the first is slow the second is slow 20 per cent of the time rather than 1 per cent. A leaf is then slow with probability 0.01 × 0.2, or 0.2 per cent, and a 100-way request is slow with probability about 1 − 0.998100, roughly 18 per cent. Hedging still helps, but far less. Where replicas are placed decides how much a hedge is worth.

When hedging makes things worse

Here is the failure to plan for. A team sets its hedging delay to a fixed 10 ms, the 95th percentile measured last month. One afternoon a dependency slows down and every leaf’s latency rises, so that 10 ms is now the 70th percentile. Thirty per cent of requests now cross the threshold and send a hedge. The leaves receive 30 per cent more work, which makes them slower still, so more requests cross the threshold. The mechanism meant to protect the tail is now adding to the overload.

Two fixes are standard, and both have a cost. Compute the hedging delay from recent latency, so it stays near the true 95th percentile; this keeps the extra load near 5 per cent, but under a broad slowdown the hedges arrive later and help less. And put a budget on hedges, for example allowing extra requests up to a fixed share of normal traffic and dropping hedges beyond it; this caps the damage, but in a bad hour the budget runs out and the tail returns. gRPC’s design does both: a hedging policy with a delay and a maximum number of attempts, and a throttle that stops retries and hedges when too many requests are failing. Redundancy is only cheap when there is spare capacity to spend on it.

Tied requests: the problem is the queue

Much of a server’s latency is time spent waiting in a queue before work starts. Once a request has started, it usually finishes quickly. A tied request exploits this. The client sends the request to two servers, each tagged with the other server’s identity. Both enqueue it. Whichever server starts it first sends a cancellation to the other, which removes its copy from the queue. The client waits a little, about twice the network message delay, before sending the second copy, so that the two servers are unlikely to start at exactly the same moment and both do the work.

The article reports experiments in which tied requests to Bigtable cut the high percentiles substantially, both on an idle cluster and on one running a heavy background sort, with only a small increase in disk work. An alternative is to ask servers how long their queues are and send the request to the shortest. The authors explain why this is worse: queue lengths change between the probe and the request, request costs are hard to predict, and many clients probing at once all pick the same lightly loaded server.

TechniqueWhat it improvesWhat it costsWhen it stops working
hedged requesta slow leaf now needs two unlucky replicasup to ~5 per cent more requeststhe request itself is expensive, or the cluster is overloaded
tied requesttime spent in a long queuea cancellation message, rare duplicate workqueues are short and service time dominates
micro-partitionsload can move in small piecesmore partitions to tracka single key is hot
selective replicationhot data has more replicas to share loadstorage and update cost for extra copieshotness changes faster than replication
latency-induced probationchronically slow machines are bypassedtemporarily less capacityslowness is spread evenly
good-enough resultsnever waiting for the last few leavesa slightly incomplete answerevery leaf’s result is required

Adapting across many requests

Hedged and tied requests act within one request. The article then describes techniques that act over seconds to minutes. With micro-partitions, data is split into many more pieces than there are machines, about twenty per machine, so load balancing and recovery move small pieces quickly. Selective replication adds copies of partitions that are hot, for example a part of the index that is getting many queries during a news event. Latency-induced probation stops sending regular traffic to a server that has been slow, while still sending it shadow requests to see when it recovers.

Two techniques are specific to large retrieval systems. Good-enough results return the answer once enough leaves have replied, accepting a slightly less complete result in exchange for not waiting for the last few. In the measurement quoted earlier, that is the difference between 70 ms and 140 ms. It is acceptable only when the missing leaves hold results that would rarely change what the user sees. Canary requests handle a different kind of tail: a query that triggers a bug and crashes every server it reaches. The root sends it to one or two leaves first and sends it to all of them only if those reply. The cost is a small amount of added latency.

Writes are easier than one might expect. The article notes that updates are often less latency-sensitive, and that quorum protocols such as Paxos tolerate tails naturally: a write that needs three of five replicas to acknowledge does not wait for the slowest two.

When not to bother

With no fan-out the arithmetic is gentle. A single-server request with a 1 per cent tail has a 1 per cent tail, and hedging spends up to 5 per cent extra load to fix it, which may or may not be worth it. Hedging is a poor fit when slowness is correlated with the request, when requests have side effects, or when the system has no spare capacity. Before adding hedges, look at a trace of a slow request. If it shows the same expensive operation every time, the fix is in that operation, not in redundancy.

How to read the original

The article is short, around eight pages in Communications of the ACM, and written for a broad audience, so read it all. Spend most of your time on the section about why variability exists, the table with the 10 ms, 70 ms and 140 ms measurements, and the sections on hedged and tied requests with their Bigtable numbers. Read the parts on cross-request adaptation and large information retrieval systems for the catalogue of techniques.

There is no evaluation section in the conference sense. The numbers are illustrations from Google systems, each with a specific workload, so when you quote one, check what the baseline was and what percentile it refers to. The closing section on hardware trends argues that the problem will get worse as systems grow, which matches what happened.

The next paper, Borg, is about the cause behind several of these tail sources: many workloads deliberately sharing the same machines, and the scheduler that decides who gets what.