Latency
easy 30 minLatency is how long one request takes.
See it run
Where the milliseconds of one request go, how to describe many requests, and what makes the slow ones slow.
Operations
Follow one request
The same request there and back, with a running clock.
Step 1 of 6
Tap
- Time so far, of 71 ms
- 2 ms
The phone needs 2 ms to build the request.
Step 1: Tap
way_in = 2 + 20 + 1work = 15 + 10way_back = 1 + 20 + 2total = way_in + work + way_back # 71 ms- Answers
- Where does the time go?
- You leave with
- A budget
Only the travel happens twice. The work happens once.
In the real world
The same latency doing real jobs. Pick one and step through it.
P50 is 40 ms and calm. P99 jumped to 2 seconds after Tuesday's release.
Step 1 of 4
Look at both
| Monday | Tuesday | |
|---|---|---|
| P50 | 40 ms | 40 ms |
| P99 | 180 ms | 2,000 ms |
| Average | 45 ms | 64 ms |
The average hardly moved, so nobody noticed. P99 did.
Step 1: Look at both
Common mistakes
The bugs interviewers watch for, with the fix beside each.
Reporting the average
criticalOne request in ten can be 41 times slower and the average still looks good.
Wrongprint(sum(v) / len(v)) # 50 msRightprint(percentile(v, 50), percentile(v, 99)) # 10, 410Calling in a row for no reason
majorIndependent calls add up when they could overlap.
Wrongp = await profile(u) # 30 mso = await orders(u) # 50 ms, 80 in allRightp, o = await gather(profile(u), orders(u)) # 50 msAveraging percentiles
majorPercentiles of parts do not give the percentile of the whole.
Wrongp99 = mean(s.p99 for s in servers)Righttotal = add_buckets(s.histogram for s in servers)p99 = percentile_of(total, 99)Waiting without a limit
criticalOne stuck dependency holds threads and connections until everything waits.
Wrongrequests.get(url)Rightrequests.get(url, timeout=(0.5, 1)) # connect, read
Interview tips
How to explain it
- How long one request takes.
- Network, queue, work, disk.
- Say it as P50, P95, P99.
Memory trick: LATENCY
- Lower is better. Always measure. Tail matters.
- Edge caching. Network is slow when long.
- Cache what is read often. Your users feel it.
Cut it by
- Fewer and shorter trips.
- Parallel calls, kept connections.
- Background work, indexes, caches.
Watch out
- Doubling the work in a round trip.
- Queues near 100%.
- Tests that wait for answers.