Skip to content

Latency and throughput

“How fast is it?” can mean two different things, and they have different answers.

  • Latency is how long one request takes, from the moment it is sent until the answer is back. It is measured in time: milliseconds.
  • Throughput is how many requests the system completes in a given time. It is measured in a rate: requests per second.

A motorway shows the difference. How long one car takes to get from A to B is latency. How many cars arrive at B each hour is throughput. A wider motorway raises throughput and leaves the journey time alone. A faster speed limit shortens each journey.

Mental model. Latency is a property of one request. Throughput is a property of the stream. A system can have excellent throughput and bad latency, or the reverse. Users feel latency, and capacity planning starts from throughput.

Google’s SRE book lists both among the four signals worth watching on any service: latency, traffic, errors and saturation. Latency is the time to serve a request, and the book makes a point that is easy to miss: the latency of a failed request should be counted apart from the latency of a successful one, because an error that comes back instantly is very different from an error that comes back after a long wait.

The time a request spends in a service is not one thing. It adds up:

  • Network time: the request travelling to the service and the answer travelling back. Part of it is fixed by distance, as the lesson on constraints showed.
  • Waiting: the time in a queue, because the service was busy with other requests.
  • Service time: the time the service spends actually working on the request.
  • Time in the services it calls: each of those has its own network time, waiting and service time.

Inside one service, the response time of a request is the waiting plus the service time. The service time depends on the work. The waiting depends on how busy the service is, and it is the part that grows when load grows.

Here is the same service at three loads. It has four servers, and one request takes 20 ms of work.

Utilisation Mean time in the system What that is made of
10% 20.0 ms 20 ms of service, and almost no waiting
50% 21.7 ms 20 ms of service and 1.7 ms of waiting
90% 59.4 ms 20 ms of service and 39.4 ms of waiting

The work did not change. At 90% the same request takes three times as long, because of what else is going on. Latency is a property of the whole system under its current load, not of the code of one request. The next lessons build on this.

Most estimates of where time goes start from a handful of orders of magnitude. These come from a well-known list that circulated around 2012, credited to Jeff Dean and Peter Norvig. The hardware has changed since, and the digits here have not been updated, so use the list for the ratios between rows and not for the exact values.

Operation About
Read from main memory 100 ns
Random read from an SSD 150 µs
Round trip within one data centre 500 µs
Seek on a spinning disk 10 ms
A packet from California to the Netherlands and back 150 ms

Two things follow. First, the spread is enormous: the last row is 1,500,000 times the first, so the cost of a design is largely decided by which of these it waits on. Second, the numbers add when requests are made in a row. A page that makes 50 calls one after another inside the same data centre spends 50 × 0.5 ms = 25 ms on the network alone, before any call has done any work. The same 50 calls made in parallel cost one round trip. This is why “how many sequential calls does a request make?” is among the first questions to ask of a design.

Latency and throughput pull against each other more often than they agree. Batching is the standard example. Imagine a service that writes to disk, where each write needs a flush that takes 10 ms. Done one at a time, the service manages 100 flushes a second, so 100 writes a second. If it waits to collect 10 writes and then flushes once, it manages 1,000 writes a second. Throughput has risen tenfold. But a write that arrives first in a batch now waits for nine more to turn up, so its latency has risen. Batching raises throughput by making each request wait.

The same trade is everywhere: a bigger buffer, a longer timer before sending, a pipeline with more stages. Which side you favour depends on who is waiting. A person who is looking at a screen wants low latency. A nightly job that processes a billion rows wants high throughput.

The lab below is a small web system. Raise the traffic and watch three tiles: Traffic, Error rate, and the p99 under Latency. The p99 is the 99th percentile, the time that 99 requests out of 100 beat. The next lesson explains why it is the figure to watch. The number of requests per second that succeed is the traffic less the failing share.

Requests pass a load balancer to three API instances. The API reads through a cache and falls back to a database whose connection pool is sized for far less traffic than the API can take. Change the traffic, the number of instances, the cache hit rate, and the database connections, and watch the cause and effect play out.

12,000 req/s
API instances
3
90%
40
0.3 ms0.2 ms0.5 ms0.5 msClientsHealthy<1%Load balancerHealthy<1%APIHealthy25%CacheHealthy14%DatabaseHealthy46%
0.3 ms0.2 ms0.5 ms0.5 msClientsHealthy<1%Load balancerHealthy<1%APIHealthy25%CacheHealthy14%DatabaseHealthy46%

Each dot stands for many requests. The numbers show the real rate.

No bottleneck: every component has headroom.

Key figures

Traffic
12,000 req/s
CPU
25%HealthyAPI utilisation
Memory
20%Healthy404 MB of 2,048 MB per API instance
Latency
p50 4.5 msp95 19 msp99 28 msHealthyWhole request, all requests
Error rate
0%HealthyRequests that fail
Database load
46%HealthyDatabase utilisation
Cache load
14%HealthyCache utilisation

Cause and effect

Each step leads to the next. A step that is changing right now is marked.

0 ms
Metrics
ComponentStateUtilisationOffered (req/s)Served (req/s)Rejected (req/s)p50p95p99
ClientsHealthy<1%12K12K00 ms0 ms0 ms
Load balancerHealthy<1%12K12K00.03 ms0.15 ms0.23 ms
APIHealthy25%12K12K02.8 ms12 ms18 ms
CacheHealthy14%11K11K00.14 ms0.6 ms0.92 ms
DatabaseHealthy46%2.3K2.3K05.5 ms24 ms37 ms
Traffic
12,000 req/s
Successful
12,000 req/s
Error rate
0%
Mean latency
6.3 ms

Illustrative values, not benchmarks of any real product.

Model notes

Assumptions

  • Requests arrive at random, as a Poisson process, at a constant average rate.
  • Each node is one queue, served first-come first-served by several identical slots (instances × concurrency).
  • Service times are random with the stated mean; by default they are exponentially distributed.
  • Each node is analysed separately, with the traffic it receives treated as Poisson.
  • Nine in ten requests are reads. The cache answers the share set by the hit rate; the rest, and every write, go to the database.

Simplifications

  • Above capacity there is no steady state. The model switches to a fluid approximation: the node serves at capacity, its queue grows until it reaches its limit, and further requests are rejected.
  • Percentiles assume an exponential tail. A service-time variability other than exponential changes the mean wait but not the shape of the tail.
  • A node frees its slot as soon as it has done its own work, before waiting on the services it calls. Real thread-per-request servers hold the slot, so a slow dependency exhausts them faster than this model shows.
  • A request calls its dependencies one after another. Parallel fan-out is not modelled.
  • When a caller times out and gives up, the callee still finishes the work, so abandoned requests keep consuming capacity.
  • Retries add load. Their backoff and jitter decide when that load arrives, which only the per-request simulation shows.
  • Retry feedback is solved from the healthy starting point, so it finds one stable outcome. Systems that stay stuck in overload after the cause is gone are not shown.
  • A throughput limit (for example maximum writes per second) is modelled as slower service, not as a separate rate limiter.
  • A caller’s timeout counts the mean time its dependencies take, not their full distribution.
  • Latency percentiles for a whole request are estimated, over every request, including those that are rejected and fail quickly. The mean is exact, and the percentiles come from a gamma distribution that has that mean and the variance of the time spent in each component.
  • Memory is modelled simply: a fixed base for each instance, plus a fixed amount for every request the instance holds, whether it is being served or waiting.
  • Each database connection serves one operation at a time, about 8 ms each, so 40 connections can serve about 5,000 operations per second. Reads and writes cost the same here; real databases treat them differently, and writes are usually the scarcer resource.

Parameter values are illustrative. They are not benchmarks of any real product.

At 12,000 requests per second everything is served, and the p99 is 28 ms. At 40,000 requests per second, 6.4% of requests fail, so about 37,400 succeed each second, and the p99 is 143 ms. At 48,000 the system serves about 44,000 requests per second, and that number does not move: at 50,000 and at 60,000 requests per second it is still about 44,000, and the p99 at 60,000 is 824 ms.

That is the shape of every system. Throughput rises with load until it meets a capacity, and then it is flat. The traffic that cannot be served does not disappear. It becomes queueing and then errors. In this lab the limit is the database, as it was in the scalability lesson.

One detail is worth noticing. At 30,000 requests per second the p99 is 159 ms, and at 40,000 it is 143 ms, which is lower even though the system is busier. The latency tile counts every request, and the ones that are rejected fail quickly and pull the percentiles down. This is the point the SRE book makes: the latency of requests that fail should be read apart from the latency of requests that succeed, or an overloaded system can look better than it is.

A team has a service running at 20,000 requests per second with a 99th-percentile latency of 28 ms, and expects a campaign to double the traffic. A common reaction is “the latency will double”. It will not, and the way it fails is more useful to understand.

At 40,000 requests per second the system is not twice as slow. Some of the work is not done at all: 6.4% of requests fail. The p99 is 143 ms, five times what it was. Latency did not double. It rose by what the queue added, and a queue adds almost nothing while a system is idle and a great deal when it is nearly full. The curve of that rise is the subject of the lesson on utilisation.

  • Latency is how long one request takes. Throughput is how many a system completes per unit of time. They answer different questions.
  • Latency adds up from network time, waiting and service time, and from the same in every service the request calls. Only the waiting grows with load.
  • The same service with the same work is slower when it is busier: 20 ms at 10% utilisation and 59.4 ms at 90% in the example.
  • Orders of magnitude, such as 100 ns for memory, 500 µs for a round trip in a data centre and 10 ms for a disk seek, say where a design’s time goes. Sequential calls add up.
  • Batching raises throughput by making each request wait.
  • Throughput rises with load until it meets a capacity and is then flat: about 44,000 requests per second in the lab. The traffic that cannot be served becomes queueing, then errors.
  • Read the latency of failed requests apart from the latency of served ones, because a request that is turned away fails fast.

Next: percentiles and tail latency, because the mean hides the requests that hurt.

Your progress

Built by GhaziAlibi.

© ShardCraft. All rights reserved.