Sub topic 2
Measuring a System: Latency, Throughput, and Utilization
This chapter assumes you know how client-server and web applications are laid out, what CPU, memory, disk, and network do on a server, and what a web server and a database do. You do not need prior experience with monitoring tools.
Before you decide to make a machine bigger or add more machines, you need numbers. This chapter covers the three measurements that describe how a system behaves under load: latency, throughput, and utilization. It also covers how to read them together and the errors people commonly make.
Why measure first
The previous chapter defined capacity as how much work a system can handle and load as the work it is asked to handle. Neither can be seen directly. You infer them from measurements.
Three questions guide the measurements:
- How long does one request take? That is latency.
- How many requests does the system finish per unit of time? That is throughput.
- How much of each resource is in use? That is utilization.
Each answers a different question, and none is enough alone. A system can have low latency and low throughput (fast, but handling few requests). It can have high throughput and high latency (many requests finished, each slowly). Low utilization can hide a problem in a resource you are not watching.
Latency
Latency is the time between sending a request and receiving the response. It is measured in milliseconds or seconds.
A request's latency is the sum of several parts:
- Network time to reach the server and return.
- Queue time, spent waiting for a free worker or connection.
- Service time, when the server is actually doing the work.
- Time spent in downstream calls, such as the database or another service.
Queue time matters most for scaling. When a system is far below capacity, queue time is near zero and latency is close to service time. As load approaches capacity, requests wait, and latency rises sharply.
Percentiles, not averages
Latency varies from request to request, so a single number must summarize many samples. The average (mean) is a poor choice, because a few very slow requests are hidden by many fast ones.
Percentiles describe the distribution better. The p50 (median) is the latency that 50% of requests beat. The p95 is the latency that 95% of requests beat, and p99 is the same for 99%.
Here is a small example that computes these values from a list of samples.
import statistics
latencies_ms = [12, 14, 13, 15, 12, 16, 14, 13, 15, 14,
13, 12, 15, 14, 16, 13, 14, 15, 480, 950]
mean = statistics.mean(latencies_ms)
cuts = statistics.quantiles(latencies_ms, n=100, method="inclusive")
p50, p95, p99 = cuts[49], cuts[94], cuts[98]
print(f"mean={mean:.1f} ms")
print(f"p50={p50:.1f} ms")
print(f"p95={p95:.1f} ms")
print(f"p99={p99:.1f} ms")The mean is pulled up by two outliers (480 and 950 ms) and describes no real request well. The p50 shows the typical request, around 14 ms. The p95 and p99 show the slow tail. For a user-facing service, the tail matters because one page view often triggers many requests, so a user is likely to hit at least one slow one.
Latency under load
Latency is not a fixed property of a server. It depends on load. The diagram shows the typical shape: flat at first, then a sharp rise near saturation.
The values follow the simple queueing approximation response time = service time / (1 − utilization). At 50% utilization a request takes twice its service time. At 90% it takes ten times. At 95% it takes twenty times. Real systems differ in detail, but the pattern holds: the last stretch of utilization is expensive in latency.
Throughput
Throughput is the amount of work completed per unit of time. For web services it is usually requests per second (RPS). For databases it may be queries or transactions per second. For pipelines and storage it may be bytes per second.
Two related terms are easy to confuse:
- Offered load is the rate at which work arrives.
- Throughput is the rate at which work is completed.
Below capacity, throughput equals offered load. Every request gets served. At capacity, throughput stops rising. If offered load keeps climbing, the extra requests queue up, time out, or are rejected. Throughput may even fall, because the system spends effort on timeouts, retries, and context switching.
Maximum throughput
The maximum sustainable throughput of a system is its capacity. You find it with a load test: raise the request rate in steps and record throughput and latency at each step. The capacity is the highest rate at which latency still meets your target, not the highest rate at which the server stays alive.
Latency and throughput are linked
Little's law connects them. For a stable system:
concurrency = throughput × latency
Concurrency is the average number of requests in the system at once. If a service handles 200 requests per second and each takes 0.25 seconds, about 50 requests are in flight at any moment.
def in_flight(throughput_rps, latency_seconds):
return throughput_rps * latency_seconds
print(in_flight(200, 0.25)) # 50.0
print(in_flight(200, 1.0)) # 200.0This is useful in practice. If latency rises from 0.25 to 1.0 seconds at the same throughput, in-flight requests grow from 50 to 200. If your worker pool or connection pool holds 100, requests now queue, and latency rises further. This is how one slow dependency can bring down a healthy service.
Utilization
Utilization is the fraction of a resource's capacity that is in use over a period. It applies to each resource separately: CPU, memory, disk I/O, network bandwidth, and also software resources such as database connections and thread pools.
| Resource | Common measure | What high values often mean |
|---|---|---|
| CPU | Percent busy across cores | Compute-bound work; too few cores |
| Memory | Percent used, swap activity, evictions | Working set too large; risk of swapping or out-of-memory kills |
| Disk | I/O operations per second, bytes per second, queue depth | Storage cannot keep up with reads or writes |
| Network | Bytes per second against link speed | Link or instance bandwidth limit |
| Connection pool | Connections in use against pool size | Callers waiting for a connection |
Reading utilization carefully
Utilization has some traps.
- Averages hide imbalance. A server with 8 cores at 50% average CPU might have one core at 100% and the rest idle if the work is single-threaded. The process cannot use the idle cores.
- Averages over long windows hide bursts. A five-minute average of 40% CPU can include 20-second spikes at 100%. Look at short windows when latency spikes.
- Memory is different from CPU. High memory use is not always a problem, since operating systems use spare memory for caches. Watch swapping, evictions, and out-of-memory kills instead.
- Saturation matters more than percentage. Saturation is the amount of work waiting because a resource is fully busy, for example run-queue length or disk queue depth. A resource at 100% utilization with a long queue is the usual cause of bad latency.
A useful habit is to check utilization, saturation, and errors for each resource. Utilization says how busy a resource is, saturation says whether work is waiting, and errors say whether something is failing outright.
Putting the three together
The measurements are most useful side by side. The table shows common combinations and what they suggest.
| Latency | Throughput | Utilization | Likely meaning |
|---|---|---|---|
| Low | Rising with load | Low to moderate | Healthy, with headroom |
| Rising | Flat | One resource near 100% | That resource is saturated and is the bottleneck |
| Rising | Flat | All resources low | Waiting on something unmeasured: a lock, a pool, or a slow dependency |
| High only at p99 | Normal | Normal | Intermittent causes: garbage collection, cache misses, noisy neighbors |
| Rising | Falling | High | Overload, with retries and timeouts adding work |
The third row is the one that surprises people. If latency is bad and every CPU, disk, and network graph looks fine, the cause is not a hardware resource you charted. Look at software limits such as pool sizes, locks, and calls to other services. Adding hardware will not help here.
A worked example
A web service runs on one server with 4 cores. During a load test you record the following.
| Offered load (RPS) | Throughput (RPS) | p95 latency (ms) | CPU utilization |
|---|---|---|---|
| 100 | 100 | 40 | 25% |
| 200 | 200 | 50 | 50% |
| 300 | 300 | 90 | 75% |
| 400 | 395 | 400 | 98% |
| 500 | 380 | 2500 | 100% |
Reading the table:
- Up to 200 RPS, throughput matches load and latency barely moves. There is headroom.
- At 300 RPS, p95 latency has already doubled, though CPU is only 75%. Queueing has started.
- At 400 RPS, CPU is near 98%, and p95 latency is 10 times its starting value. Throughput has stopped tracking load.
- At 500 RPS, throughput falls below its earlier peak. The server is overloaded.
If the latency target is 100 ms at p95, capacity is about 300 RPS, not 400. Planning to run at 400 would put the server permanently on the steep part of the curve. In practice you also keep a safety margin for traffic spikes and for losing a server.
Common errors
- Watching only averages. Mean latency and mean CPU hide the tail and the imbalance that users feel.
- Measuring only the server. Latency seen by the client includes network time and load balancer time. Measure at more than one point.
- Testing with unrealistic traffic. A load test that sends one URL, or has no think time between requests, can give very different results from real users.
- Confusing capacity with the breaking point. Capacity is the load you can carry while meeting your latency goal, not the load at which the server crashes.
- Ignoring software resources. Pool sizes, file descriptor limits, and thread counts can saturate while hardware sits idle.
- Looking at one moment. Daily and weekly cycles mean a quiet-hour measurement can mislead you about peak behavior.
What comes next
With these three measurements, you can say where a system is on its latency curve and which resource is under pressure. The next chapter uses them to find the bottleneck, which decides whether a bigger machine, more machines, or neither is the right response.
Exercise
A service handles 150 requests per second, and its p95 latency is 0.4 seconds. Its connection pool allows 50 concurrent requests. Using Little's law, how many requests are in flight on average, and what does this suggest about the pool?
Show answer
Concurrency = throughput × latency = 150 × 0.4 = 60 requests in flight on average. Since the pool allows only 50, the pool is saturated: some requests must wait for a free slot. That waiting adds queue time, which raises latency further. Raising the pool size, or reducing latency by speeding up the work, would relieve it. Note that 0.4 seconds is the p95, not the mean, so the true average concurrency is somewhat lower, but the pool is still likely near its limit.
Exercise
During a load test, p95 latency climbs from 60 ms to 1.5 s while throughput stays flat at 250 RPS. CPU, memory, disk, and network utilization are all below 40%. Name two places to look for the bottleneck.
Show answer
Look at software and downstream limits that hardware graphs do not show. Examples: a database connection pool or thread pool that is full, lock contention in the application or database, or a slow call to another service. Queue depth or wait time on these resources will show where requests are waiting.