Sub topic 8
Performance requirements: latency, throughput, percentiles
This chapter assumes you know what a non-functional requirement is and why it must be measurable. It also assumes you can read short Python snippets. Here you will write performance requirements that a team can test.
Latency and throughput
Latency is the time one request takes, from the moment it is sent to the moment the response arrives. Throughput is how many requests the system completes per unit of time, usually requests per second (RPS).
The two are related but not the same. A system can answer each request in 10 ms and still handle only 100 RPS if it has one worker. A system with many workers can have high throughput and still be slow for each request.
A useful rule links them. The number of requests in flight at once is roughly throughput multiplied by latency. At 6,944 RPS and 100 ms per request, about 694 requests are in progress at any moment. That number tells you how many connections, threads or workers you need.
Why averages hide problems
An average blends fast and slow requests into one number. A few very slow requests barely move it. Users who hit those slow requests still have a bad experience.
A percentile fixes this. The 99th percentile (p99) is the value that 99% of requests are at or below. The median is p50. In the example below, 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)) # 250Writing a testable target
A good performance requirement names four things: the metric, the percentile, the threshold, and how and over what window it is measured. Compare these two:
- Weak: "Redirects must be fast."
- Strong: "99% of redirects complete in under 100 ms, measured at the service edge over 30 days."
The strong version is an SLO built on a latency SLI. You can compute it from logs and decide whether it was met. You can also state several targets at once, such as p50 under 20 ms and p99 under 100 ms, because the tail often matters more than the middle.
Deriving throughput targets
Throughput targets come from usage assumptions. Divide daily requests by 86,400 seconds to get an average rate. Multiply by a peak factor, because traffic is never flat, to get the rate you must size for.
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)) # 6944Common errors
- Stating only an average latency, which hides the slow tail.
- Leaving out the measurement point. Latency at the client, at the edge and at the database are different numbers.
- Sizing for average load instead of peak load.
- Setting a latency target without a throughput level. A system may meet 100 ms at 100 RPS and fail at 5,000 RPS.
Exercise
A service handles 5 million requests per day with a peak factor of 4. Its p99 latency target is 200 ms. What is the peak RPS, and about how many requests are in flight at peak?
Show answer
Average RPS is 5,000,000 / 86,400, which is about 57.9. Peak RPS is about 57.9 × 4, which is about 231.5. In-flight requests are about 231.5 × 0.2 s, which is about 46.