Sub topic 8
Performance requirements: latency, throughput, percentiles
This chapter assumes you know what a non-functional requirement is and how SLIs and SLOs frame a measurable target. You should also be comfortable reading short Python snippets. Here you will learn to write performance requirements that a team can measure and test, and to avoid the usual traps in how they are worded.
Two quantities: latency and throughput
Performance requirements usually describe two different quantities.
- Latency is the time one request takes from start to finish. It is measured in milliseconds or seconds.
- Throughput is how much work the system completes per unit of time. It is measured in requests per second (RPS), messages per second, or bytes per second.
They are related but not interchangeable. A system can have low latency and low throughput (one fast worker), or high throughput and high latency (a large batch pipeline). A requirement that names only one of them leaves the other unconstrained.
| Quantity | Question it answers | Typical unit | Example target |
|---|---|---|---|
| Latency | How long does one user wait? | ms | 99% of redirects under 100 ms |
| Throughput | How much can the system handle? | requests/s | Sustain 7,000 redirects/s at peak |
Where latency is measured
Latency depends on where you start and stop the clock. A request passes through the client, the network, a load balancer, the service, and often a database. Each measurement point gives a different number.
- Client-side latency includes the network and rendering. It is what the user feels, but you rarely control it.
- Service-edge latency starts when the request reaches your load balancer or gateway and ends when the response leaves. Most SLOs use this point because you control it.
- Internal latency covers a single hop, such as one database query. It is useful for budgeting, not for user promises.
The URL shortener requirement from the Topic Spine states its measurement point: "measured at the service edge over 30 days". Always state the point, the window, and the percentile.
Why averages mislead
The mean (average) hides slow requests. If 99 requests take 20 ms and one takes 2,000 ms, the mean is about 40 ms. That number describes no actual request. Users who hit the slow one see 2 seconds.
Latency distributions are usually skewed with a long tail. Most requests are fast, and a few are much slower because of garbage collection pauses, cache misses, lock contention, retries, or a cold connection. Percentiles describe this shape.
- p50 (median): half of requests are faster than this value.
- p95: 95% of requests are faster. One in twenty is slower.
- p99: 99% are faster. One in a hundred is slower.
- p99.9: one in a thousand is slower.
The following snippet uses the nearest-rank method on ten samples. The median looks healthy while p99 exposes the outlier.
import math
def percentile(samples, p):
"""Nearest-rank percentile: the smallest value with at least p% of samples at or below it."""
ordered = sorted(samples)
rank = math.ceil(p / 100 * len(ordered))
return ordered[rank - 1]
latencies_ms = [12, 15, 14, 18, 20, 22, 25, 30, 40, 250]
print(percentile(latencies_ms, 50)) # 20
print(percentile(latencies_ms, 99)) # 250With only ten samples, p99 is simply the maximum. A percentile is only meaningful when you have enough samples. For p99 you want at least several hundred requests in the window, and for p99.9 several thousand.
Choosing which percentile to require
The right percentile depends on how many users the tail affects and how costly a slow request is.
- p50 tracks the typical experience. It is a poor sole target because it ignores the tail.
- p95 or p99 is the common choice for user-facing endpoints.
- p99.9 suits services that others call many times per request, because tail latency compounds.
Tail latency compounds in fan-out
Suppose one page load calls 20 backend services in parallel and waits for all of them. If each service is slow 1% of the time, the chance that at least one is slow is 1 − 0.99^20, about 18%. A p99 target on each dependency becomes a roughly p82 experience for the page. When a request fans out, set stricter percentiles on the dependencies than on the user-facing endpoint.
def chance_of_slow_page(per_call_slow_rate, calls):
"""Probability that at least one of `calls` parallel calls is slow."""
return 1 - (1 - per_call_slow_rate) ** calls
print(round(chance_of_slow_page(0.01, 20), 3)) # 0.182
print(round(chance_of_slow_page(0.001, 20), 3)) # 0.02The second line shows the benefit of a p99.9 target on each dependency: the page-level slow rate drops from about 18% to about 2%.
Writing a good latency requirement
A testable latency requirement has five parts.
- The operation: which request, for example "redirect" rather than "the system".
- The percentile: p99, not "fast".
- The threshold: 100 ms.
- The measurement point: the service edge.
- The window and conditions: over 30 days, at or below a stated load.
Compare two versions.
- Weak: "The service must respond quickly."
- Strong: "99% of redirects complete in under 100 ms, measured at the service edge over 30 days, at loads up to the stated peak."
The strong version can be turned into an SLI (the fraction of redirects under 100 ms) and an SLO (at least 99% of them). The weak version cannot be tested.
Different operations deserve different targets. Creating a short URL writes to storage and may reasonably allow 300 ms, while a redirect is on the hot path and needs 100 ms. One global latency number usually fits neither.
Throughput requirements
Throughput requirements state the load the system must carry. They come from the usage assumptions you recorded during scoping, not from guesses made at design time. The calculation from the Topic Spine converts users into an average and a peak request rate.
daily_active_users = 10_000_000
requests_per_user_per_day = 20
peak_factor = 3
requests_per_day = daily_active_users * requests_per_user_per_day
average_rps = requests_per_day / 86_400
peak_rps = average_rps * peak_factor
print(round(average_rps)) # 2315
print(round(peak_rps)) # 6944Dividing by 86,400 (the seconds in a day) gives the average. Real traffic is not flat, so the peak factor scales the average to the busiest period. A factor of 2 to 5 is common for consumer traffic, but you should replace it with measured data when you have it. State the factor as an assumption so reviewers can challenge it.
Write the requirement with its units and conditions: "Sustain 7,000 redirects per second at peak while meeting the latency target." The final clause matters, as the next section shows.
Latency and throughput interact
A latency target holds only up to a certain load. As a server nears saturation, requests queue, and latency rises sharply. A throughput claim such as "handles 10,000 RPS" is meaningless if p99 latency is 8 seconds at that rate.
Because of this curve, you should combine the two quantities in one requirement and leave headroom. Planning to run at 50 to 70% of measured capacity at peak keeps the system on the flat part of the curve and absorbs surprises.
Throughput versus concurrency
Concurrency is the number of requests in progress at the same moment. Little's Law relates it to the other two quantities:
concurrency = throughput × average latency
At 7,000 requests per second and a mean latency of 50 ms (0.05 s), about 350 requests are in flight at once. Use this to size connection pools, thread pools, and queue limits. If latency doubles during an incident, concurrency doubles too, and a pool sized for the normal case runs out.
def concurrency(rps, mean_latency_seconds):
"""Little's Law: average number of requests in flight."""
return rps * mean_latency_seconds
print(concurrency(7000, 0.05)) # 350.0
print(concurrency(7000, 0.10)) # 700.0Measuring correctly
A requirement is only as good as the measurement behind it. Watch for these problems.
- Averaging percentiles. You cannot compute a correct p99 for a fleet by averaging each server's p99. Aggregate raw samples or use histograms with shared bucket boundaries, then compute the percentile.
- Coordinated omission. A load-test client that waits for a slow response before sending the next request under-reports slow periods. Use a tool that sends at a fixed rate regardless of responses.
- Wrong window. A 5-minute p99 and a 30-day p99 answer different questions. The long window tolerates short bad spells, and the short window catches them.
- Warm-up effects. Cold caches and cold JIT compilers distort early samples. State whether the target applies after warm-up.
- Excluding errors. Fast failures can improve latency numbers. Count latency for successful requests only if the availability target separately covers failures, and say so.
Common pitfalls in performance requirements
| Pitfall | Why it is a problem | Fix |
|---|---|---|
| "Fast" or "low latency" with no number | Cannot be tested | Give percentile, threshold, point, window |
| Only the mean is specified | Hides the tail | Use p95, p99, or p99.9 |
| Throughput with no latency condition | Saturated systems still "handle" the load | Require the latency target at that load |
| One number for all operations | Reads and writes differ in cost | Set per-operation targets |
| Peak equals average | Under-sizes the system | State and justify a peak factor |
| Targets stricter than the business needs | Raises cost and complexity | Tie each target to a user need |
Summary of the working model
Specify latency as a percentile with a threshold, a measurement point and a window. Specify throughput as a peak rate derived from stated assumptions. Tie the two together by requiring the latency target at the peak load, and keep headroom below saturation. Later chapters use these numbers for capacity estimation and for trade-offs against availability, consistency and cost.
Exercise
A checkout page calls 10 backend services in parallel and waits for all of them. Each service meets its latency target 99% of the time. Roughly what share of page loads will hit at least one slow call, and what per-service percentile target would bring that share to about 1%?
Show answer
The share with at least one slow call is 1 − 0.99^10, about 9.6%. To bring the page-level rate to about 1%, each service needs a slow rate near 0.1%, which is a p99.9 target: 1 − 0.999^10 is about 1.0%.
Exercise
A service handles 4,000 requests per second at a mean latency of 80 ms. How many requests are in flight on average, and what happens to that number if mean latency rises to 400 ms during an incident at the same request rate?
Show answer
By Little's Law, concurrency = 4,000 × 0.08 = 320 requests in flight. At 400 ms it is 4,000 × 0.4 = 1,600, five times higher. Connection and thread pools sized for 320 can be exhausted, which makes latency worse.