01. Foundations / Lesson 2 of 3

Latency & Throughput

How long one request takes, and how many you can finish each second.

  • 8 min
  • Beginner
  • 9 steps
  • p50 latency20 ms (good)
  • p99 latency20 ms (good)
  • Throughput50/s (good)
  • Queue length0 (good)
UsersWaiting lineWorker
  • Request
  • Write
Step 1/9

Step 1 of 9: A server is like a one-lane road

Read every step as text
  1. Step 1: A server is like a one-lane road

    Picture a server with one worker. A request comes in, the worker spends 20 ms on it, and sends the answer. Then it takes the next one.

    Two things describe how this road behaves, and they are easy to mix up. We will name them, then see how they move apart.

  2. Step 2: Latency: how long one request takes

    Latency is the time from sending one request to getting its answer. Here it is 20 ms, because that is how long the work takes and nobody is waiting ahead of us.

    It is what a single user feels: the delay between a click and the result. We measure it in milliseconds, and lower is better.

    Latency is about one request. Ask: "how long did I wait?"

  3. Step 3: Throughput: how many requests finish each second

    Throughput is the number of requests completed per second. One worker needs 20 ms each, so in 1,000 ms it finishes 1000 / 20 = 50 requests.

    It is what the whole system delivers: a measure of capacity. Higher is better, and it is often written as requests per second (RPS or QPS).

    Throughput is about many requests. Ask: "how many can you finish per second?"

  4. Step 4: They are not the same thing

    Add three more workers and throughput becomes 200 requests a second. But one request still takes 20 ms: extra workers do not make any single request faster.

    The other way round also holds: a faster worker lowers latency and raises throughput together, but a system can have great latency and poor throughput, or the reverse.

    More workers (parallelism) buys throughput. Less work per request buys latency, and usually throughput too.

  5. Step 5: Over capacity, a queue builds up

    Go back to one worker (50/s). Now 60 requests arrive every second. The worker falls 10 requests behind each second, and the extra ones wait in a queue.

    After 10 seconds 100 requests are waiting, and a new one must wait 100 / 50 = 2.0 s before its 20 ms of work even starts. Over those 10 seconds the middle request waited about 1 s (p50 1,020 ms) and the slowest 1% almost 2 s. The work did not change; the waiting did.

    Latency = waiting time + work time. Near full capacity, waiting time explodes, so keep a safety margin of unused capacity.

  6. Step 6: Percentiles: the average hides the slow ones

    Back to four workers with room to spare. Out of 1,000 requests, 980 take 20 ms and 20 take 500 ms (a pause or a slow query). The average is 29.6 ms, which describes nobody.

    p50 (the median) is 20 ms: half the requests are faster. p99 is 500 ms: 99 in 100 are faster than this. A page that makes 50 calls hits at least one slow one 1 - 0.98^50 = 64% of the time.

    Watch p99 (and p99.9), not the average. At scale, the slow tail is what many users meet.

  7. Step 7: Little's law: how many requests are in flight

    Little's law says: requests in the system = arrival rate × time each one spends there. At 200 requests a second and 20 ms each, that is 200 × 0.02 = 4 requests in flight at any moment, so we need at least 4 workers. That is why four is right.

    Now say the database slows down and each request takes 40 ms. The same 200 a second means 8 in flight. With only 4 workers, a queue appears even though traffic did not change.

    Use it to size pools: workers or connections needed ≈ requests per second × seconds per request.

  8. Step 8: Batching: trade a little latency for throughput

    Saving a row to the database costs 4 ms of fixed overhead (the round trip and the commit) plus 0.1 ms for the row. One row per call gives 1000 / 4.1 = 244 rows a second.

    Grouping 100 rows in one call costs 14 ms, so the rate is 7,143 rows a second, about 29 times more. The price: a row may wait for its batch to fill, so each row's latency goes up.

    Batching suits logs, events and bulk imports. It does not suit a click that must answer instantly.

  9. Step 9: Latency and throughput in one picture

    Latency is the wait of one request (20 ms here); throughput is the number finished per second (50 per worker). Adding workers raises throughput but not latency.

    Past capacity, queues make latency blow up. Judge a service by p99, size it with Little's law, and use batching when you can spend a little latency for much more throughput.