Course contents

01. System Design Fundamentals Lesson 4 of 8

Latency

Break a request into network, queueing, processing and dependency time; read percentiles; see why busy servers are slow and what caching and distance do.

12 minBeginner7 steps
Loading the interactive simulation…
Loading the knowledge check…

Summary

  • Latency is the end-to-end time of one request as the user experiences it.
  • It is a sum: network both ways, queueing, processing, and every sequential dependency.
  • Report it as percentiles; the tail (P95, P99) is what users on busy pages feel.
  • Queueing makes latency rise steeply as utilisation approaches 100%, before any errors.
  • Caches avoid work and lower utilisation; regions and CDNs remove distance.
Deep diveHow it works, trade-offs, failure modes, misconceptions and the interview angle.

What it is

Latency is the time one operation takes, from the moment it is requested to the moment the result is available. For a user-facing request, that means from the click until the answer arrives. It is measured in milliseconds and reported per request, so a system does not have "a latency" but a distribution of them.

Why it exists

Every request travels over a network, may wait for a free worker, is processed, and usually calls other components that do the same. Each of these takes time, and some of it is variable. Latency matters because people notice it: interfaces that respond within roughly 100 ms feel instant, while delays of a second or more change behaviour, and services that call other services inherit all of their delays.

How it works

Latency is a sum of parts:

  • Network time. Bounded by distance: light in fibre covers roughly 200 km per millisecond, so a round trip across an ocean costs tens to over a hundred milliseconds before any work happens. It is paid on every round trip, and protocols such as TCP and TLS add round trips when a connection is set up.
  • Queueing time. When every worker is busy, a request waits. With random arrivals, waiting grows slowly at moderate load and steeply as utilisation approaches 100%. This is why a server can be "only" 85% busy and already noticeably slower.
  • Processing time. The work itself: parsing, logic, serialisation.
  • Dependency time. Calls to databases, caches and other services. Sequential calls add up; parallel calls cost roughly the slowest one.

Because these parts vary, latency is reported as percentiles. P50 (the median) is the typical request; P95 and P99 are the times that 5% and 1% of requests exceed. The average is easily dragged by a few outliers and hides the tail at the same time. The tail matters because a page that triggers many requests, or a service that fans out to many back ends, is likely to hit at least one slow response.

When to use it

Latency is the primary metric for anything interactive: page loads, API calls, search, checkout. It is also the most sensitive early signal of overload, because queueing raises it before errors appear. Service level objectives are usually written as percentile targets, for example "P99 under 300 ms over 28 days".

When not to

For batch jobs, data pipelines and background processing, the metric that matters is usually throughput or time to finish the whole job, not the latency of any single item. Optimising per-item latency there often wastes effort; batching items may even raise latency per item while finishing the job sooner.

Trade-offs

Technique Lower latency because Cost
Caching Skips slow work for repeated reads Staleness, invalidation, memory
Regions, CDNs, edge Removes distance Data replication, consistency, cost
Parallel calls Pays only the slowest dependency More concurrent load, harder error handling
Keeping headroom (lower utilisation) Shorter queues Paying for idle capacity
Hedged or duplicate requests Cuts tail latency Extra load on back ends
Batching Fewer round trips per item Higher latency for each item waiting in a batch

Alternatives

Latency is a property, not a choice, so the alternatives are in how you measure and hide it. Asynchronous processing returns immediately and finishes the work later, so the user-facing latency is short even if the job is long. Optimistic UI updates show the expected result before the server confirms it. Prefetching starts work before the user asks.

Failure modes

  • Averages in dashboards. A healthy average hides a terrible P99.
  • Coordinated omission. Load tests that wait for each response before sending the next under-report latency during stalls.
  • Fan-out amplification. A request that calls 50 back ends is as slow as the slowest of them; one slow shard slows everyone.
  • Retry storms. Slow responses trigger client timeouts and retries, which add load and make responses slower still.
  • No timeouts. A dependency that hangs ties up workers until the caller saturates too.

In production

Measure latency where the user experiences it (real-user monitoring) and per hop (tracing), and keep histograms rather than averages. Set timeouts on every outbound call, shorter than the caller's own deadline. Watch utilisation as a leading indicator: a component running near capacity will show latency growth first. Put static content on a CDN, serve users from nearby regions where data allows it, cache hot reads, and remove sequential round trips from critical paths.

Common misconceptions

  • "Latency and throughput are the same thing." A system can serve many requests per second while each one is slow, or answer quickly but only a few at a time.
  • "A faster CPU fixes latency." Only the processing part. Network distance and queueing at a saturated dependency remain.
  • "The server is at 80%, so we have 20% headroom." Latency rises well before 100%; the effective headroom is smaller.
  • "P99 is just outliers." On a busy page or a fan-out service, the P99 of a component becomes the typical experience of the user.

Interview angle

State latency targets as percentiles and build a rough budget: client to edge, edge to service, service to database, with numbers. Point out which calls can run in parallel, where a cache removes a round trip, and when users far away need a nearby region or CDN. Mentioning that queueing raises latency long before saturation, and that you would keep utilisation below about 70–80% for latency-sensitive services, shows you understand the mechanics.

Model assumptions

The simulation is a simplified model, not a benchmark. Network hops take a fixed time (20 or 80 ms each way to the users, 1–5 ms inside the data centre) with no jitter, loss or connection setup. The app server has 8 workers and a 30 ms mean service time; a worker waiting for the database stays busy. The database has 8 connections and a 40 ms mean query time; in the tail step its variability rises (lognormal, cv 2) while the mean stays the same. The cache answers a fixed 90% of lookups in about 2 ms. Arrivals are random (Poisson). Numbers show behaviour and orders of magnitude only.

The lesson as textEvery step of the simulation, written out.
  1. 1. Latency is the wait for one answer

    Latency is how long one request takes, measured from the moment the user sends it to the moment the answer arrives. Here the server is 20 ms away and needs about 30 ms of work.

    So a typical request takes about 70 ms: 20 there, 30 of work, 20 back. The tile shows P50, the median: half of all requests are faster than it.

    Scene: Users send 20 requests per second to an app server 20 ms away. The median request takes about 70 ms.

    Key idea: Latency is end-to-end time for one request, as the user experiences it.

  2. 2. Users move farther away

    Now the users are on another continent: 80 ms each way instead of 20. The server does exactly the same work, yet the median jumps from about 70 to about 190 ms.

    Distance is paid twice per round trip, and light in fibre cannot go faster. The only fixes are fewer round trips or putting the server (or a copy of the data) closer to the users.

    Scene: The users are now 80 ms away. The median request takes about 190 ms, although the server work is unchanged.

    Key idea: Network latency is set by distance and paid on every round trip; a faster server cannot remove it.

  3. 3. Every hop adds up

    Each request now also reads from a database 5 ms away, which needs about 40 ms per query. The server must wait for that answer before it can reply.

    Sequential steps add: the median grows by roughly 50 ms, to about 240 ms. A page that makes three such calls one after another pays three times. That is why latency budgets are split per hop.

    Scene: The app server now queries a database for every request. The median request takes about 240 ms.

    Key idea: Latencies of sequential calls add up; parallel calls cost only the slowest one.

  4. 4. A busy server is a slow server

    A second in, traffic rises from 20 to 85 requests per second. Each request still needs the same work, and nothing fails, but the server is now about 85% busy.

    When all workers are taken, new requests wait in a queue, and that waiting is added to their latency. P95 rises sharply even though no single step got slower. Near full utilisation, a little more load costs a lot more latency.

    Scene: At 85 requests per second the app server is about 85% busy and requests queue. Nothing fails, but P95 latency rises sharply.

    Key idea: Queueing delay grows steeply as utilisation approaches 100%: high load raises latency long before errors appear.

  5. 5. The average hides the slow ones

    Real queries are not all alike: most are quick, a few hit cold data or wait for a lock. Here the database’s average stays at 40 ms, but its times now vary a lot.

    The median rises somewhat, because slow queries hold workers longer at this load, but P99, the time that 1 request in 100 exceeds, jumps far more. The average hides this. Users who load many resources per page hit that tail often, which is why teams set targets on P95 and P99.

    Scene: Database query times now vary widely with the same average. The median request is a little slower, while P95 and P99 rise much more.

    Key idea: Percentiles describe what users actually feel; the slowest 1–5% matter because they happen to someone on every busy page.

  6. 6. The fastest request is the one you skip

    We add a cache next to the server: it answers 90% of lookups in about 2 ms, so most requests never touch the database.

    Two things improve at once. Hits skip the 50 ms database trip, and because each request now holds a worker for less time, the server is far less busy and the queue disappears. Both P50 and P95 drop. Only the 80 ms each way to the users remains.

    Scene: A cache answers 90% of lookups. Most requests skip the database, the app server is much less busy and both median and P95 latency drop.

    Key idea: Cut latency by avoiding work (caching) and by keeping utilisation away from 100%.

  7. 7. Where latency comes from

    Every request’s latency is a sum: network (distance × round trips), queueing (how busy each component is), processing and every dependency called in sequence. Read it as percentiles, because the tail is what users complain about.

    The cures map to the parts: move closer (CDN, regions), make fewer round trips, avoid work (caches), call dependencies in parallel, and keep headroom so queues stay short.

    Scene: The final setup: users 80 ms away, an app server with a cache and a database. Latency is dominated by the network distance.

    Key idea: Measure latency as percentiles and attack the biggest part of the sum first.