Part 01 · Thinking About Scale

Latency vs Throughput

p50/p95/p99RPSTail latency

Two numbers that describe how a system performs, why averages lie, and why p95 is the number users feel.

⏱ 9 min · Station 3 of 93

The problem

Two engineers look at the same shop. One says "it's fast: a product page takes 2 milliseconds". The other says "it's slow: during the evening rush pages take 50 milliseconds and we can't serve more than about 570 a second". Both measured correctly. They measured different things.

Before we can scale anything, we need two separate numbers and a clear idea of how they pull on each other: how long one request takes, and how many requests the system finishes per second.

The idea

  • Latency is the time one request takes, from the moment it's sent to the moment the answer arrives. It's what a user feels. We report it as percentiles: p50 (half the requests are faster than this), p95 and p99 (95% and 99% are faster). The Reliability course spends a whole lesson on why p99 matters and averages lie.
  • Throughput is how many requests the system finishes per second. It's what the business needs: how many customers can shop at the same time.

They are linked by one more number, concurrency: how many requests are in flight at once. On average,

requests in flight = throughput × latency

(this is Little's Law; the Reliability course derives it). Read it in both directions. If the system finishes 570 requests per second and each takes 14 ms, about 8 are in flight at any moment. And if you force 64 requests in flight on a system that can only finish 570 per second, each one must take about 64 / 570 = 112 ms. The extra time is waiting.

How it works

Take the shop's database, whose single CPU thread is the bottleneck, and a group of users who each send the next request the moment the previous answer arrives.

  1. One user. The CPU is idle between requests (while the answer travels back and the next request arrives). Latency is pure work, about 2 ms. Throughput is limited by the user, not the server: one user can't send more than 1 / 2 ms = 500 per second.
  2. Two users. While one request travels, the other is being worked on, so the CPU idles less. Throughput goes up. Latency goes up a little too, because sometimes both arrive at once and one waits.
  3. Four or more users. The CPU is now busy all the time. It can't finish requests any faster than its maximum, about 570 per second. Throughput stops growing.
  4. Every extra user past that point only adds waiting. With 8 users, each request waits behind about 7 others. With 64, behind about 63. Latency grows in a straight line with concurrency while throughput stays flat.

So there are two regimes. Below the bottleneck's limit, more concurrency buys more throughput for a little latency. At the limit, more concurrency buys nothing but latency.

Predict first

With 8 busy users the shop does 572 requests/s with p50 at 14 ms. You go to 64 users. What happens?

Visual

1 usersend, wait, repeatdb CPUone at a time468 req/sp50 1.9 msreq8 userssend, wait, repeatdb CPUone at a time572 req/sp50 14 mswaitwaitwaitwaitwaitwaitwaitreqsame CPU, same work per request1 user: every request finds the CPU free, latency is just the work8 users: the CPU is busy all the time, so each request waits behind ~7 othersthroughput +22%, latency x7: once the bottleneck is full, more users only add waiting
The same database CPU with 1 busy user and with 8. With 8, throughput is only 22% higher, but every request first waits behind about 7 others.

Common misconception

"Lower latency and higher throughput are the same goal." They often move together when you make the work cheaper (an index makes each request faster and lets the CPU finish more of them). But they come apart as soon as you're at a limit: you can push throughput up a little by allowing more concurrency, and pay for it with much higher latency. Many "performance" arguments are two people optimising different numbers.

"More threads (or more workers) means more throughput." Only until the bottleneck is full. Past that point, more threads just form a longer line. If the database CPU is the limit, 200 app threads instead of 20 make every page slower and get nothing more done.

Try it

Measured on the lab (Intel i3-10110U, 4 threads, 7 GB RAM; PostgreSQL and the shop each pinned to one CPU thread). Instead of a fixed rate, this test uses a fixed number of virtual users (k6's constant-vus), each sending its next request as soon as the last one returns. That way we choose the concurrency and measure the throughput.

// A fixed number of users (VUS), each sending the next request as soon as the last one returns.
import http from "k6/http";
import { sleep } from "k6";
import { summary } from "./summary.js";

const TARGET = __ENV.TARGET || "http://api:8080";
const PATH = __ENV.PATH_TPL || "/products/{id}";

export const options = {
  scenarios: {
    users: { executor: "constant-vus", vus: Number(__ENV.VUS || 1), duration: __ENV.DURATION || "20s" },
  },
  // -e NEW_CONN=1 opens a new TCP connection for every request instead of keeping one alive.
  noConnectionReuse: __ENV.NEW_CONN === "1",
};

export default function () {
  const id = 1 + Math.floor(Math.random() * 20000);
  http.get(TARGET + PATH.replace("{id}", id), { timeout: "10s" });
  // -e SLEEP=0.02 makes each user pause 20 ms between requests ("think time").
  if (__ENV.SLEEP) sleep(Number(__ENV.SLEEP));
}

export const handleSummary = summary;
  vus  got/s      p50      p95      p99  failed  dropped
    1     468   1.9 ms   2.9 ms   4.7 ms   0.0%       0
    2     551   3.2 ms   5.3 ms   7.0 ms   0.0%       0
    4     571   6.6 ms   9.9 ms    13 ms   0.0%       0
    8     572    14 ms    23 ms    26 ms   0.0%       0
   16     571    26 ms    47 ms    58 ms   0.0%       0
   64     573   110 ms   135 ms   146 ms   0.0%       0

Reading it:

  • 1 user: 468 requests per second, p50 1.9 ms. Latency is just the work. Throughput is limited by having only one request at a time.
  • 2 users: 551 per second (+18%). Latency went up to 3.2 ms: the CPU now sometimes has two requests at once.
  • 4 users and beyond: about 571 per second, flat. The CPU is saturated. 64 users get exactly as much done as 4.
  • Latency grows in step with users: p50 6.6 ms at 4, 14 at 8, 26 at 16, 110 at 64. Check it with the formula: 64 / 573 = 0.112 s, the measured p50.
Users in flightThroughputp50What the extra users bought
1468/s1.9 ms(baseline)
4571/s6.6 ms+22% throughput for 3.5× latency
64573/s110 msnothing but 58× latency

Trade-offs

  • A little queueing is healthy. Running with zero waiting means the bottleneck sits idle some of the time. A short queue keeps it busy. The question is how much latency you'll accept for that last bit of throughput.
  • Limit concurrency at the bottleneck. Since requests past the limit only wait, it's better to make them wait somewhere cheap (or reject them) than to let them all pile onto the database. Connection pools (Part 5) and load shedding (Part 8) both do this.
  • Batching trades latency for throughput. Grouping work (writing 100 rows in one query) gets more done per second but makes each item wait for its batch. Good for background jobs, bad for a user waiting on a page.
  • Your target decides. "Product pages p95 under 100 ms" tells you the most concurrency you can allow at the bottleneck, and so how much throughput one instance can honestly deliver.

Key takeaway

Latency is what one user feels, throughput is how many users you serve, and concurrency links them: in flight = throughput × latency. Below the bottleneck's limit, more concurrency buys throughput. At the limit, it buys only waiting.

Practice

Objective: use the formula to predict a measurement before you make it.

Challenge: predict throughput and p50 latency with 32 virtual users. Then run ./vus.sh vus.js "32" 15s from the lab folder and compare.

Expected result: a prediction within about 20% of the measured p50, and an explanation of any difference.

Hint 1

Is 32 users below or above the point where throughput stopped growing?

Hint 2

Throughput at the limit is about 570 per second. Rearrange the formula: latency = in flight / throughput.

Hint 3

The formula gives the average latency. p50 is close to the average when latencies are fairly even, as they are here.

Solution

32 users is far above the limit (4), so throughput should stay near 570 per second, and latency should be about 32 / 570 = 56 ms.

  vus  got/s      p50      p95      p99  failed  dropped
   32     507    61 ms    86 ms   100 ms   0.0%       0

Our run did 507 per second (a little lower: the laptop was doing something else during this run) and p50 61 ms. With the measured throughput, the formula gives 32 / 507 = 63 ms, almost exactly the measured p50. The formula holds even when the machine is noisy; you just have to use the throughput you actually got.

Interview check

Q: Your API's average latency doubled, but throughput stayed the same. What does that tell you?

Model answer

The system is probably at a bottleneck: it's finishing as many requests per second as it can, and the extra latency is time spent waiting, which means more requests are in flight (in flight = throughput × latency, so in flight doubled too). Something is sending more concurrent requests (more users, retries, a client that raised its concurrency) or the bottleneck got slower for some requests. Look at which resource is saturated and at the queue in front of it, not at the code path itself.

Q: Would you rather have 10 servers at 10 ms latency or 1 server at 1 ms latency, for the same total throughput?

Model answer

It depends on what users need and what fails. The 1 ms server gives a better experience per request but is a single point of failure and has no room to grow. The 10 servers survive one failing and scale out, but 10 ms is a floor no request can beat. For a user-facing page, 10 ms is usually fine and redundancy wins; for a hot internal call made many times per page, the 1 ms matters because latencies add up along the chain.

Station stamp

Finish the chapter

Done reading? Press Complete to save your progress. XP is Pro-only — on Pro this chapter is worth +10 XP.

Go Pro to earn XP →