Sub topic 2
Measuring a system: latency, throughput, and utilization
This chapter assumes you know what a web server and a database do. It also assumes you know that CPU, memory, disk, and network are the resources a server uses. You need numbers before you can decide whether to scale up or out, so this chapter covers the three basic measurements.
Latency: how long one request takes
Latency is the time between sending a request and receiving the response. It is measured per request, usually in milliseconds. Users feel latency directly.
A single average hides the slow requests. If nine requests take about 50 ms and one takes 900 ms, the mean is 137 ms. That number describes none of the requests. Use percentiles instead:
- p50 (the median): half of requests are faster than this value.
- p95: 95% of requests are faster than this value.
- p99: 99% of requests are faster than this value.
The slowest requests are called tail latency. They matter because a page that makes many backend calls is likely to hit at least one slow call.
import statistics
latencies_ms = [42, 45, 48, 50, 51, 53, 55, 60, 70, 900]
window_s = 2.0
def percentile(values, p):
ordered = sorted(values)
index = max(0, round(p / 100 * len(ordered)) - 1)
return ordered[index]
print("mean:", statistics.mean(latencies_ms))
print("p50:", percentile(latencies_ms, 50))
print("p95:", percentile(latencies_ms, 95))
print("throughput:", len(latencies_ms) / window_s, "req/s")This prints a mean of 137.4, a p50 of 51, a p95 of 900, and a throughput of 5.0 req/s. The mean looks acceptable. The p95 shows that some users wait almost a second.
Throughput: how much work per unit of time
Throughput is the number of requests the system completes per unit of time, such as requests per second. It can also count bytes per second or transactions per second. In the code above, 10 requests finished in a 2-second window, so throughput was 5 req/s.
Throughput and latency are related but different. A system can have low latency and low throughput, such as a fast single worker. It can also have high throughput and high latency, such as a deep queue of slow jobs. Capacity is usually described as the throughput the system can sustain while latency stays acceptable.
Utilization: how busy a resource is
Utilization is the fraction of time a resource is busy, or the fraction of its capacity in use. You measure it per resource: CPU at 80%, disk at 30%, memory at 60%, network at 10%.
Utilization explains why latency changes as load grows. At low utilization, requests rarely wait, so latency is close to the raw processing time. As utilization approaches 100%, requests queue behind each other and latency climbs steeply. A CPU at 95% is not "5% from full." It is already in the zone where small bursts of traffic cause long waits.
How the three relate
Little's law connects them: the average number of requests in the system equals throughput multiplied by average latency. If a service handles 100 req/s and each takes 0.2 seconds on average, about 20 requests are in flight at any moment. If latency rises while throughput stays fixed, more requests pile up. That pile-up uses memory, connections, and threads.
| Measurement | Unit | Question it answers | Typical warning sign |
|---|---|---|---|
| Latency | ms (p50, p95, p99) | How long do users wait? | p95 or p99 rising while p50 stays flat |
| Throughput | requests per second | How much work gets done? | Flat even though offered load increases |
| Utilization | % of a resource | How close is a resource to its limit? | Sustained above about 70-80% |
Common mistakes
- Reporting only the mean. It hides the tail.
- Measuring only one resource. A server with 20% CPU can still be saturated on disk or on database connections.
- Measuring at a quiet time. Capture the peak hour, because that is when the limits show.
- Confusing offered load with throughput. If clients send 500 req/s but the system completes 300 req/s, the rest are queued or failing. Track error rates and timeouts along with these three numbers.
Exercise
A service completes 200 requests per second, and the average request takes 0.05 seconds. Using Little's law, how many requests are in the system on average? If the average latency doubles while throughput stays at 200 req/s, what happens to that number?
Show answer
Average requests in the system = throughput × latency = 200 × 0.05 = 10. If latency doubles to 0.1 seconds, the number becomes 200 × 0.1 = 20. Twice as many requests are in flight at once, which means more memory, threads, and connections are held.