Algorithms · Huge scale
Hedged requests
How Google makes slow requests rare: ask twice. One slow server out of a hundred makes most big pages slow. Unless you quietly ask a second one.
The problem
When you search at Google, your one request doesn't go to one computer. It fans out to many servers, each holding a piece of the answer, and the page is only done when the last one replies.
Jeffrey Dean and Luiz André Barroso described the catch in their 2013 paper The Tail at Scale. If just 1% of server calls are slow, a request touching 100 servers is slow most of the time. Rare slowness becomes the normal experience.
The obvious way
Ask one server, wait for its answer. It's simple, and for one call it's fine. But a page needing 100 answers is a coin you flip 100 times: each flip has a 1% chance of landing on "slow", and the page waits for the worst one. That's about a 63% chance of a slow page, even though 99% of servers are healthy.
The clever idea
Picture ordering coffee for a whole office. You ask one barista, and if your drink hasn't appeared after a reasonable wait, you quietly ask a second barista too. Whichever hands you a cup first wins, and you ignore the other.
That's a hedged request. Send to one replica, and if it hasn't answered within roughly the 95th percentile time, send a backup to another replica and use whichever answers first. The cost is a little extra load, since only the slow few trigger a backup.
How the code does it
The simulated server is fast almost always, but 2 calls in 100 are painfully slow. A page makes 50 such calls and takes the max, so about 64% of pages catch at least one slow server. That's why even the plain median looks bad.
The hedged version only does extra work when the first answer is later than 30 ms. If it is, the page gets the faster of the original and the backup. Both pages are built in these two lines, and the last lines show what the safety net cost. Run it and compare the bars.
Trade-offs
Hedging spends extra requests to buy lower latency, and the cutoff matters: too early and you flood servers with backups, too late and you gain little. It's a poor fit for requests that change data, since running them twice could do harm, and when servers are slow because they're overloaded, extra load makes things worse.
Where else you’ll see it
Any system that fans out to many parts and waits on all of them can use this idea: search, databases spread over many machines, and services that call many other services.
Why does hedging add only a small amount of extra load in this simulation?
- Backups are sent only when the first reply is late
- Backups are sent to every server at once
- Backups replace the original request
Show the answer
Backups are sent only when the first reply is late. A backup goes out only if the first answer takes over 30 ms, which happens for the roughly 2% of slow calls, not for every call.
- Change 0.02 to 0.10
- Change the 30 cutoff to 15
- Change range(50) to range(5)
The code
Python, with what each line means beside it. It compares the clever way with waiting for whichever server you asked.
import randomBring in the random toolsrandom.seed(7)Fix the dice so results repeatbackups = []Keep a tally of backup requests def server_ms():Simulate one server answering if random.random() < 0.02:Two times in a hundred... return random.uniform(200, 1000)...it answers very slowly return random.uniform(10, 20)Otherwise it answers in 10–20 ms def plain():The obvious way: ask one server return server_ms()Wait for whatever it says def hedged():The clever way: ask, maybe back up first = server_ms()Ask the first server if first <= 30:If it answers within 30 ms... return first...great, use that answer backups.append(1)Otherwise, note a backup was sent return min(first, 30 + server_ms())Take whichever finishes first def page(call):A page is 50 server calls return max(call() for _ in range(50))It takes as long as the slowest def pct(xs, p):Find a percentile of a list xs = sorted(xs)Sort times fastest to slowest return xs[int(len(xs) * p / 100) - 1]Pick the value at that rank plain_pages = [page(plain) for _ in range(500)]Build 500 pages the obvious wayhedged_pages = [page(hedged) for _ in range(500)]Build 500 pages with hedging show([round(pct(plain_pages, 50)), round(pct(plain_pages, 99))], "No hedging: median, p99 ms")Show median and slow tail, obviousshow([round(pct(hedged_pages, 50)), round(pct(hedged_pages, 99))], "Hedged: median, p99 ms")Show median and slow tail, hedgedtotal = 500 * 50Count all server calls madeshow({"calls": total, "backups sent": len(backups), "extra load %": round(100 * len(backups) / total, 1)}, "Cost of hedging")Show how many extra requests it cost- the rare slow server lines 5–8Most calls take 10–20 ms, but 2% take 200–1000 ms. Those few are the entire tail-latency problem.
- the hedge lines 15–18The backup starts at 30 ms, so its finish time is 30 plus its own delay. min picks the winner.
- slowest wins line 21max models fan-out: the page can't finish until its slowest server does.
Read more
- research.google: Hedged requests
- Ask Bento: What is a 99th percentile exactly?
- Ask Bento: Why not always send two requests?
- Ask Bento: How do replicas stay in sync?
Run it. Then change it.
The code runs right in the page and draws as it goes. Free, nothing to install.