Show contents
Chapter 1 of 6 · 20 min
Measuring performance and estimating capacity
By the end of this chapter, you'll be able to
- Compute and interpret the p50, p95 and p99 percentiles of a latency
- Apply Little's law to link throughput, duration and concurrency
- Explain why latency explodes as saturation approaches
- Estimate the capacity needed for a traffic peak, with headroom
Percentiles rather than the average
Latency is the time between sending a request and receiving the response. Over a hundred requests, it varies: network, garbage collector, database lock, disk. The p95 percentile is the value below which 95% of the measurements fall: if p95 = 45 ms, then 95 requests out of 100 take 45 ms or less.
import math
def percentile(values, p):
"""Nearest-rank method: the smallest value covering p% of the measurements."""
ordered = sorted(values)
rank = math.ceil(p / 100 * len(ordered))
return ordered[rank - 1]
# 100 requests: most are fast, a few are very slow
latencies = [40, 38, 45, 41, 39, 42, 44, 43, 37, 40] * 9 + [41, 39, 40, 380, 42, 38, 900, 43, 1200, 44]
average = sum(latencies) / len(latencies)
print(f"average: {average:.0f} ms")
for p in (50, 95, 99, 100):
print(f"p{p}: {percentile(latencies, p)} ms")
average: 64 ms
p50: 41 ms
p95: 45 ms
p99: 900 ms
p100: 1200 ms
The average (64 ms) matches no real experience: neither the median (p50, 41 ms) nor the slow requests. The p99 reveals that one request in a hundred takes almost a second. Teams therefore set their objectives in percentiles: "p95 under 200 ms, p99 under 800 ms". Notice that p95 is 45 ms and p99 900 ms: the tail starts abruptly, which points the investigation towards a rare event (an SQL query without an index, a garbage collector pause) rather than general slowness.
Little's law
Little's law links three quantities of any stable system:
L = λ × W
average concurrency = throughput (requests per second) × average time spent in the system (seconds)
If your API receives λ = 400 requests per second and each takes W = 0.25 s, there are on average L = 100 requests in progress at any moment. With servers able to handle 20 simultaneous requests each, you need at least 5. This formula, valid for any system, is the starting point of every capacity estimate.
We aim for 70% utilisation at the peak: beyond that, latency soars.
Text description of the animation
- Start from the realistic peak, not the daily average.
- Each active user loads a page every 15 seconds on average.
- Little's law: throughput multiplied by the average duration of a request.
- A server handles 50 simultaneous requests in our load tests.
- We aim for 70% utilisation at the peak: beyond that, latency soars.
Why latency explodes near saturation
A server busy at 50% responds quickly. At 95%, requests queue up. The simplest queueing model (called M/M/1: random arrivals, one server) gives the average time spent in the system: W = S / (1 − ρ), where S is the service time and ρ (rho) the utilisation.
service_time_ms = 20
print("utilisation average time")
for rho in (0.5, 0.7, 0.8, 0.9, 0.95, 0.99):
w = service_time_ms / (1 - rho)
print(f"{rho:>10.0%} {w:>8.0f} ms")
utilisation average time
50% 40 ms
70% 67 ms
80% 100 ms
90% 200 ms
95% 400 ms
99% 2000 ms
Going from 50% to 90% utilisation multiplies the response time by 5; from 90% to 99%, by 10 again. The relationship is not linear: that is why capacity is planned to stay under 70 to 80% at the peak. Real systems do not follow this model exactly, but the shape of the curve shows up everywhere.
Mini-lab: simulating a queuePython 3.13
Output
Click “Run”: Python will load in your browser (about 10 MB the first time).Your code runs in your browser, isolated from the page. · Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Your turn
Worked exercise
Worked exercise: enrolment day
A university opens online enrolment at 8 am. 120,000 students are expected, 40% of whom connect in the first hour. During their session, each student makes 30 requests on average, and the session lasts 10 minutes. A request takes 0.15 s on average, and a server supports 40 simultaneous requests.
- How many students are connected at the same time during the first hour (Little's law, applied to sessions)?
- What is the throughput in requests per second?
- How many servers are needed to stay under 70% utilisation?
Hint 1
Hint 2
Show the solution and common mistakes
import math
students = 120_000 * 0.40 # first hour
arrivals_per_s = students / 3600
session_s = 10 * 60
simultaneous_sessions = arrivals_per_s * session_s # Little's law
throughput = simultaneous_sessions * 30 / session_s # requests per second
concurrency = throughput * 0.15 # Little's law, again
servers = math.ceil(concurrency / 40 / 0.70)
print(f"simultaneous sessions: {simultaneous_sessions:.0f}")
print(f"throughput: {throughput:.0f} req/s")
print(f"requests in progress: {concurrency:.0f}")
print(f"servers (under 70%): {servers}")
simultaneous sessions: 8000
throughput: 400 req/s
requests in progress: 60
servers (under 70%): 3
Little's law is used twice: to go from arrivals to simultaneous sessions, then from throughput to requests in progress. Three servers are enough in theory; in practice, you plan at least one more so that you can lose one without saturating the others, and you check the result with a realistic load test.
Common mistakes:
- Mixing units (requests per hour and durations in seconds): the result is wrong by a factor of 3,600.
- Starting from the daily average instead of the peak: the 120,000 students do not spread over 24 hours.
- Sizing for 100% utilisation: latency becomes unacceptable long before that.
Summary
- 1The average hides the tail: latency is measured in percentiles (p50, p95, p99).
- 2Little's law: concurrency = throughput × duration; it underpins every capacity estimate.
- 3Latency grows very quickly as saturation approaches: aim for 70 to 80% utilisation at the peak.
- 4A page that makes many calls often suffers the tail latency of one of them.
- 5An estimate is always checked with a realistic load test.
Your progress is saved on this device. Create a free account to keep your progress on all your devices.