retrystorm · Learn · Interview practice · Glossary · Components

Home › Learn › Latency Budgets

Chapter 8 · Foundations · 18 min

Latency Budgets

Fan-out, why the tail grows with every call, and deadlines for the parts a page can do without.

Inkwell's home page got a box of recommendations, served by a store another team runs. It's usually quick. Sometimes it isn't, and since the box shipped, the slowest home pages take a quarter of a second.

A page is rarely one call, and it's only as fast as the slowest call it waits for. Budgeting each call's time is how big systems stay fast.

A page is many calls

To build its home page, Inkwell's app server needs the user's notes, their profile, today's prompt and some recommendations. It asks for all of them at once, in parallel, and builds the page when the answers are back.

Four calls at once. The page waits for the slowest.

Recommendations come from another team's key-value store, a database that only looks values up by a single key. A later chapter covers how it works; here it's just one more call.

In parallel, the page takes as long as its slowest call, not the sum. That's much better than calling them one after another, but it has a catch.

The tail grows with every call

Suppose each service is slow 1% of the time. A page that calls one of them is slow 1% of the time too. A page that calls six is slow whenever any of the six is. It's fast only if all six are fast, so with n calls each slow with chance p, the share of slow pages is 1 − (1 − p)^n. For six calls that's 1 − 0.99^6, about 5.9%. The page's p99 is set by much rarer slow calls than any single service's p99.

Calls per page16100
Pages that hit a 1% slow call1%6%63%

Each call below is fast, except that 1% of them take ten times as long. Change how many calls the page makes and watch the share of slow pages follow the formula.

A budget for every call

A latency budget starts from the target for the whole page, say 99% of feed pages within 200 ms, and gives each call room inside it. Because the calls run in parallel, each one can use most of the budget. Each call then gets a deadline: if it hasn't answered by then, the page stops waiting for it.

Feed page, 200 ms budgetDeadlineIf it misses
Posts160 msthe page fails
Profile160 msthe page fails
Who to follow140 msthe box is left out

A deadline on a call the page can't do without just trades slowness for failure. The page waits less, then fails anyway. That's only right when a late answer is as useless as none.

Optional calls

Some parts of a page are nice to have. Without its "who to follow" box, the feed page still works: it shows the posts and leaves that box out. Marking the call optional with a deadline means a slow suggestion can't hold the page up or break it; the page simply goes without.

This is called graceful degradation: under trouble, show less rather than nothing.

Choosing the deadline

An optional call's deadline is a trade. Shorter, and pages are faster but miss the extra part more often. Longer, and the part shows up more often but slow calls push the page toward its budget.

In the wild

The ideas in this chapter, and where you'll meet them in real systems and interviews:

IdeaWhere you'll meet it
Tail latency under fan-outGoogle's paper "The Tail at Scale"
Deadlines and optional callsgRPC deadlines, fallbacks in Netflix's services
Hedged requestssending a second copy of a slow call and taking the first answer, also from "The Tail at Scale"

Your practice

Inkwell's new home page shows a writer's notes and a box of recommendations from a store that's usually quick but sometimes very slow. Since it shipped, the slowest pages have taken a quarter of a second. Get 99% of pages under 100 ms while still showing recommendations on almost every page.

Key takeaways

  • Parallel calls take as long as the slowest one, not the sum.
  • Each extra call is another chance to hit a slow one, so the page's tail grows with fan-out.
  • A latency budget gives each call a deadline; on a required call, a deadline just trades slowness for failure.
  • Optional calls let a page show less instead of failing: graceful degradation.
  • Pick the longest deadline that keeps the page in budget, then check how often it goes without.

Read first

  • Timeouts and Retries

Practise it

  • A Fast Home Page: The home page waits on a recommendations store that's sometimes very slow. Keep the page fast without dropping the box.

Terms

  • Deadline: How long a caller waits for a call before it stops waiting and goes on without it, or fails.
  • Fan-out (calls): One request that calls several services at once, and waits for the slowest.
  • Graceful degradation: Showing less rather than nothing when part of the system is slow or down.
  • Hedged request: A second copy of a slow call, sent after a short wait, taking whichever answers first. Only for calls that are safe to repeat.
  • Latency budget: A target for the whole request, with a deadline for each call inside it.
  • Tail latency: The slowest requests, the p99 and beyond. It grows with every call a page waits on.
AI system design mock interview · About · Contact · Privacy · Terms of use