Latency numbers that matter
Every design question is a latency budget wearing a product costume. Memory is nanoseconds, a local SSD read is microseconds, a round trip in one datacenter is a fraction of a millisecond, and a cross-country round trip is tens of milliseconds. If you cannot add those up, the rest of the diagram is decoration.
A user does not experience your median. They experience the slowest hop they had to wait for, and if the page fans out into twenty calls, they experience the slowest of those twenty. The rest of this course — replication, caches, queues, models — is a set of ways to move work between those hops. You need the numbers before you need the patterns.
Core concepts
Think in orders of magnitude, then tighten. A factor of ten changes the design. A factor of two is a tuning problem. These are the bands worth memorizing.
Jeff Dean published a teaching list around 2012 that is still the right skeleton. The units below are the same events; the modern note says where the hardware moved.
| Event | Time | In one line |
|---|---|---|
| L1 cache reference | 0.5 ns | The CPU already has it |
| Branch mispredict | 5 ns | The pipeline guessed wrong |
| L2 cache reference | 7 ns | Still on the chip |
| Mutex lock and unlock | 25 ns | Coordination inside one process |
| Main memory reference | 100 ns | Off the chip, on the machine |
| Compress 1 KB (Zippy / Snappy class) | 3 μs | CPU can chew a small payload |
| Send 1 KB over a 1 Gbps link | 10 μs | Serialization delay, not distance |
| Random 4 KB read, 2012 SSD teaching number | 150 μs | The old “SSD” row |
| Round trip inside one datacenter | 0.5 ms | Dean’s same-building number |
| Disk seek on a spinning disk | 10 ms | Mechanical, still real for cheap bulk |
| Round trip California to the Netherlands | 150 ms | Oceans and routers |
A nanosecond is 10⁻⁹ seconds. A microsecond is a thousand nanoseconds. A millisecond is a thousand microseconds. People mix these up under pressure and design a cache for a problem that was a disk, or a disk for a problem that was the Atlantic.
What changed since that list. A current NVMe drive often serves a random 4 KB read in tens of microseconds, not 150. Use 20–100 μs as a planning band for local flash, and measure the drive you actually have. Spinning disks did not get faster; they got rarer in the hot path. Network distance did not get a pass from physics.
Distance you can derive. Light in fiber travels at roughly two thirds of the vacuum speed, about 200,000 km/s, which is 200 km per millisecond. A 4,000 km path — think Virginia to northern California, the long way fiber actually runs — is 20 ms one way before a single router. A round trip has a floor near 40 ms. Planning for 50–80 ms of coast-to-coast RTT is physics plus switching, not a vendor benchmark. A same-city trip has a tiny propagation term; the milliseconds you see there are queues, TLS, and the other service.
Inside a cloud region, use bands, not a brochure. A round trip in the same availability zone is often well under a millisecond. Crossing availability zones in one region is commonly around a millisecond. Crossing regions is the fiber math above. If your design needs five cross-region calls on the user path, you already spent a large fraction of a second, and no cache of local objects will hide it.
Percentiles. The median (p50) is the typical request. p99 means 1 in 100 requests is slower than that number. p99.9 means 1 in 1,000. Averages hide the tail because a few multi-second requests barely move a mean of millions of fast ones, and those few are exactly the ones a user retries. Report p50, p99, and the timeout. If you only have a mean, you do not yet have a latency number.
Fanout. If one user action waits on N parallel calls, and each call is independent with latency distribution L, the user waits on the maximum. The max of N draws sits much farther into the tail than one draw. Twenty calls at a 1% chance of being slow is not a 1% chance that the page is slow. It is close to a certainty that one of them is. This is why a “fast” service still produces a slow product, and why tail latency is a product problem.
The budget. Pick the experience you owe — say a search box that feels instant, or a page that should be interactive in 200 ms — and subtract the hops you cannot remove. What remains is the budget for your own code and your own database. If the remainder is negative, the design is already wrong. Move work off the path, or move the user closer to the data, or do less.
A worked example
You are sketching the first byte of a logged-in home page. The user is in the same continent as the region. You assume, and you say so out loud:
- New connection, not a warm one. TLS 1.3 costs about one round trip after the TCP handshake, so budget two RTTs for a cold connection. A resumed or HTTP/3 connection can be one, sometimes zero extra. We will use the cold number.
- Continental RTT of 30 ms. That is a planning assumption for “same country, not next door,” not a measurement of one provider.
- DNS is cached on the client. Budget 0. If it is not cached, add a lookup; do not pretend it is free the first time.
- Application work, including auth token check, 15 ms of CPU and local cache.
- One database read on the critical path, 8 ms at p99 inside the region.
- Everything else is async after first paint.
cold connection 2 * 30 ms = 60 ms
app + auth cache 15 ms
database p99 8 ms
---------------------------------
first byte 83 ms
83 ms is inside a 200 ms goal with room for a slow tail. Now change one assumption. The token check is a synchronous call to another region because that is where the session store lives. Add a 60 ms round trip. You are at 143 ms before the database, and a p99 on that remote call can erase the rest of the budget. The fix is not a faster language. The fix is to keep session validation in-region, or to accept a signed token that this region can check without a call. That is a latency decision that looks like a security decision. Both descriptions are true.
Second arithmetic, the one interviewers use to see if you believe the table. Dean’s main-memory reference is 100 ns. His same-datacenter round trip is 0.5 ms, which is 500,000 ns.
500,000 ns / 100 ns = 5,000
One local network round trip costs as much as about five thousand memory lookups. A process that chats across the network inside a loop is not “a bit slower.” It has left the memory world. This is the entire argument for batching, for caching, and for not splitting a hot path into ten services before you have measured a reason.
Third, fanout. Suppose the home page also asks five recommendation services in parallel, each with p99 of 40 ms and p50 of 8 ms. The page cannot render until the slowest returns. You do not get to quote 8 ms. You quote something closer to the p99, and if those tails are correlated — a shared cache miss, a shared dependency — you quote worse than that. A practical rule: the latency of a fanout is the latency of the worst branch, plus the cost of the join.
flowchart LR
user[User]
edge[DNS cached]
conn[Cold TLS about 2 RTT]
app[App and auth 15 ms]
db[Database 8 ms p99]
rec[5 recommenders in parallel]
user --> edge --> conn --> app
app --> db
app --> rec
The diagram is the budget. If you cannot put a number on an arrow, you do not yet know if that arrow belongs on the path the user waits for.
Tradeoffs
| Choice | What you gain | What you spend |
|---|---|---|
| Call fewer services on the critical path | A smaller max, a simpler tail | You push work into a coarser service, or into a precomputed view |
| Cache the hot read | Microseconds instead of a remote p99 | Stale data, and a stampede when the cache dies |
| Parallelize independent reads | Wall clock near the slowest, not the sum | Tail gets worse as N grows; you pay N times the load |
| Place data in the user’s region | You delete a fiber RTT | You now have a consistency and failover problem |
| Hedge a slow request | You cut the tail by trying twice | Extra load, exactly when the system is already slow |
| Set a tight timeout | A stuck dependency cannot eat the page | You fail requests that would have succeeded a moment later |
Timeouts are part of the latency design. A timeout that is longer than the user’s budget is not a safety net. It is a way to make the user wait for a failure you have already decided not to show them. A timeout that is shorter than the dependency’s normal p99 fails healthy calls. Set it from the budget and the dependency’s tail, and leave a little room for one retry only if the retry still fits.
Pitfalls
- Quoting a mean. Ask for p50 and p99. If the dashboard only has a mean, treat it as a hint, not a budget.
- Adding retries without subtracting them from the budget. Two attempts at 80 ms do not fit in a 100 ms page.
- Forgetting the cold connection. A warm keep-alive hides TLS. The first request from a phone that just woke up pays it.
- Treating “same cloud” as “same rack.” Cross-zone and cross-region are different products.
- Using the 2012 SSD row as if it were this year’s NVMe, or using a laptop SSD number for a networked database.
- Optimizing CPU before the round trips. A 2 ms function is invisible next to a 40 ms call you did not need.
- Ignoring fanout. “Each service is 10 ms” is not “the page is 10 ms” when you wait for all of them.
- Measuring only the server. The user starts the clock at the tap, which includes DNS, TLS, and the last byte, not your handler’s log line.
In the interview
Open the design with a budget, even a rough one. “I want the interactive path under 200 ms, so I get one database round trip and no cross-region calls on the way.” That sentence does more work than a box labeled “microservice.”
When you add a box, say whether the user waits for it. Boxes the user does not wait for can be slow, retried, and eventually consistent. Boxes the user waits for have to fit in what is left of the budget. Interviewers are listening for that split.
If you mention a cache, say what latency you are removing and what staleness you just accepted. If you mention a queue, say that you moved the work off the user’s clock. Those are latency decisions. Name them as such.
You do not need Dean’s table recited to the microsecond. You need the bands: memory, local disk, same-region network, cross-country network. And you need to do the addition out loud.
Check yourself
Using Dean's 100 ns memory reference and 0.5 ms same-datacenter round trip, how many memory lookups fit in one round trip?
0.5 ms is 500,000 ns. Divided by 100 ns, that is 5,000 memory references. A network hop inside the building is thousands of RAM accesses, which is why chattiness dominates CPU.
A page waits on 30 parallel calls. Each call misses its deadline 1 percent of the time, independently. Is the page's miss rate about 1 percent?
No. The page misses if any call misses. The chance that all 30 succeed is 0.99^30, about 0.74, so roughly a quarter of pages miss even though each dependency looks fine. Independent tails add up. Correlated tails are worse.
What is a defensible round-trip floor for a 4,000 km fiber path, and what did you assume?
Fiber is about 200 km per millisecond. One way is 4,000 / 200 = 20 ms of propagation. The round trip is at least 40 ms before routers and queuing. A planning number of 50–80 ms is that floor plus ordinary network overhead. It is not a measured SLA.
Your p99 budget for a dependency is 50 ms and the user budget is 120 ms. A teammate sets the timeout to 2 seconds and adds three retries. What is wrong?
A single attempt can already outlive the page, and three retries make the worst case many seconds. The user gave up long before your client did. The timeout has to fit in the remaining budget, and a retry only belongs there if the two attempts together still do.
Based on: Jeff Dean, Numbers Everyone Should Know; Gil Tene, How NOT to Measure Latency; Designing Data-Intensive Applications, Kleppmann and Riccomini.