AlgoViz

Latency

easy 30 min

Latency is how long one request takes.

When it comes up

The moments this idea belongs in a design interview, and the moments it does not.

Signals

When it comes up

You hearSayBecause
"It must feel instant""So about 100 ms at the client, at P95"A word becomes a target
"Users everywhere""Reads from an edge, and what happens to writes?"Distance is a floor
"The home page calls eight services""In parallel, with timeouts, and some optional"The slowest decides
"Average is 50 ms""What is P99?"Averages hide the tail
"Send an email after sign-up""In the background"Nobody waits for it

A budget, word for word

  1. Interviewer

    Opening a recipe should be fast. What does that mean?

  2. You

    I will take P95 under 300 ms at the phone. Say 80 ms is the network round trip on a warm connection. That leaves 220 ms.

  3. You

    One indexed lookup is about 5 ms and the code about 15. So even with a cold cache we are near 100 ms. So no extra box is needed for speed. Photos come from a CDN because they are large and far.

  4. Interviewer

    And if the comments service is slow?

  5. You

    It gets its own timeout of 100 ms. After that the page opens without comments and loads them later.

Options

Compare your moves

P50 is 40 ms, P99 is 2 s. Users are unhappy. What do you do first?

BreaksBuy faster servers
How
Move every server one size up.
Limit
The typical request is already fast. The tail may be a lock, a slow query or a full pool, and stays.
Choose when
CPU is the proven cause.
WorksAdd a cache
How
Keep hot results in Redis.
Limit
Hits get faster, but misses are the tail and may stay at 2 s.
Choose when
Slow requests repeat the same reads.
StrongTrace the slow 1%
How
Compare traces of slow and normal requests, fix the part that differs, and add a timeout meanwhile.
Limit
Needs tracing that keeps slow requests.
Choose when
Almost always.