Skip to content
Tech Atlas, home
Search
Esc
  • All roadmapsBrowse every learning path
  • ExploreFind a technology area
  • ProjectsGuided projects and builds
  • My AtlasYour learning dashboard
Lesson 1 of 6 · Concept · 20 min · revision 3

Percentiles, Throughput and Little's Law

Understand service level indicators using percentiles over averages, analyse tail latency amplification, and apply Little's Law to calculate concurrent in-flight capacity.

Contentsand lesson path

On this page

  1. Why it matters
  2. Why averages mislead: percentiles and tail latency
  3. Little's law and capacity sizing
  4. Check your understanding
  5. Apply it
  6. Review prompt

Lessons of API Performance & Profiling

  1. Percentiles, Throughput and Little's Law Concept · 20 min
  2. Lab: A Reproducible Load Test Lab · 30 min
  3. Profiling a Slow Endpoint Worked example · 25 min
  4. Targeted Fixes and Overload Guards Concept · 25 min
  5. Lab: Prove the Fix, Then Push Past Capacity Lab · 30 min
  6. Check: Diagnose from the Numbers Knowledge check · 8 questions

What you will learn

  • State a performance goal in latency percentiles and throughput, and run a reproducible load test that reports p50, p95, p99 and error rate.

01. Why it matters

Suppose your dashboard says checkout takes 75 ms on average, yet support tickets say the page sometimes hangs for five seconds, and a launch next month will double the traffic. Two questions follow: how slow are the slow requests, and how much load can the service take before requests start to queue? This lesson gives you a tool for each. Percentiles describe the slow end of the response times, which an average hides. Little's law links the request rate, the time per request and the number of requests in flight, so you can size connection pools and workers from measurements instead of guesses.

02. Why averages mislead: percentiles and tail latency

The mean (arithmetic average) adds up all response times and divides by their number. It is easy to compute and easy to put on a dashboard, but it describes a request that may not exist, and it hides the slow requests that users actually notice.

Take an endpoint where 99 requests take 20 ms and one request waits on a database lock for 5,500 ms. The mean is (99 × 20 + 5,500) ÷ 100 = 74.8 ms. A dashboard showing "75 ms" looks healthy, yet one request in a hundred takes more than five seconds. At 10,000 requests a minute, that is about 100 slow responses every minute, each from a real person waiting.

Response times are also rarely spread evenly around one typical value. Suppose 80% of requests are served from a cache in 2 ms and the other 20% run a database query that takes 150 ms. The mean is about 32 ms, a time that almost no request actually takes: requests are either much faster or much slower. Cold starts, new TLS connections and garbage-collection pauses add further groups of slow requests in the same way.

A percentile answers a more useful question: how long did a given share of requests take, at most? The 50th percentile, p50 or the median, is the time that half of the requests beat. p95 is the time that 95% beat, and p99 the time that 99% beat. The slow end of the distribution, beyond p95 or p99, is called the tail. Lock waits, pauses, retries and slow queries usually appear in the tail first, often while p50 does not move at all.

The tail matters more when one page needs many calls. If a page waits for 20 calls made in parallel, and each call has a 1% chance of being slow, the chance that at least one of them is slow is 1 − 0.99^20 ≈ 18%. With 100 calls it is 1 − 0.99^100 ≈ 63%. As fan-out grows, a rare slow call per service becomes a common slow page, which is why performance goals are written as percentiles at a stated request rate, not as averages.

03. Little's law and capacity sizing

Little's law, proved by John Little in 1961, links three averages of a stable system. In symbols, L = λ × W. Here L is the average number of requests in the system at once (in flight), λ (lambda) is the average arrival rate, for example requests per second, and W is the average time a request spends in the system, in the same unit of time.

The law holds for long-run averages whatever the shape of the arrival pattern or of the response times, as long as the system is stable: work leaves as fast as it arrives, so the queue does not keep growing. That is why it is useful for sizing. You can measure two of the three numbers and compute the third.

Here is a worked example. A checkout service receives 800 requests per second, and each request holds a PostgreSQL connection for 25 ms on average. Convert the time to seconds first, then multiply: L = 800 × 0.025 = 20. On average, about 20 connections are busy. Because this is an average, short bursts need more. With a pool of only 10 connections, requests queue for a connection and W grows; a pool of 25 to 30 leaves headroom. A much bigger pool is not automatically better either, because the database can run only so many queries at once, as lesson 3 explains.

The same formula works in the other direction. If you know that a service holds about 40 requests in flight while it serves 400 requests per second, the average time per request is W = L ÷ λ = 0.1 s.

Little's law needs a stable system. When the arrival rate λ exceeds what the service can complete, μ, there is no steady state, so the law no longer applies: the queue grows by about λ − μ requests every second, waiting time keeps rising, memory fills with queued sockets, and the service fails unless concurrency caps or load shedding step in. Latency already climbs steeply before that point, as utilisation approaches 100%.

The script below repeats the numbers from this lesson with Python's standard statistics module. statistics.quantiles interpolates between neighbouring values, so with exactly one slow request in a hundred, p99 lands between the 99th value (20 ms) and the 100th (5,500 ms). Other tools may round this boundary differently, which is one reason to look at max as well when samples are small.

Python · latency_math.py (standard library only)
"""Percentiles and Little's law for the examples in this lesson (stdlib only)."""
import statistics


def littles_law(rate_per_s: float, time_ms: float) -> float:
    """Average number in the system: L = lambda x W, with W in seconds."""
    return rate_per_s * time_ms / 1000


def summarise(latencies_ms: list[float]) -> dict[str, float]:
    # quantiles(n=100) returns the 99 cut points p1 ... p99
    cuts = statistics.quantiles(latencies_ms, n=100)
    return {
        "mean": round(statistics.fmean(latencies_ms), 1),
        "p50": round(cuts[49], 1),
        "p95": round(cuts[94], 1),
        "p99": round(cuts[98], 1),
        "max": max(latencies_ms),
    }


latencies = [20.0] * 99 + [5500.0]  # 99 fast requests and one lock wait
print(summarise(latencies))
print(f"busy connections at 800 req/s x 25 ms: {littles_law(800, 25):.0f}")
print(f"pages with a slow call, 20 calls: {1 - 0.99 ** 20:.1%}")
print(f"pages with a slow call, 100 calls: {1 - 0.99 ** 100:.1%}")
Terminal
python3 latency_math.py
Expected output
{'mean': 74.8, 'p50': 20.0, 'p95': 20.0, 'p99': 5445.2, 'max': 5500.0}
busy connections at 800 req/s x 25 ms: 20
pages with a slow call, 20 calls: 18.2%
pages with a slow call, 100 calls: 63.4%

04. Check your understanding

Practice check · 3 questions

Percentiles and Little’s law

Three questions about reading percentiles, applying Little’s law and fan-out.

Q1. Out of 100 requests, 98 take 20 ms and 2 take 3 s. Which statement is true?
Q2. Traffic stays at 500 requests per second, but a slow query raises the time each request holds a connection from 20 ms to 50 ms. The pool has 15 connections. What happens?
Q3. A page waits for 20 backend calls made in parallel, each with a p99 of 1 s. Why is the page often slower than most single calls?
Practice only: not graded, not saved, not evidence.

05. Apply it

In the skill project you will state goals for real endpoints, so start measuring the right numbers now:

  • Record latency as a histogram, not an average. In Prometheus, choose bucket boundaries around your goals (for example 0.05, 0.1, 0.25, 0.5 and 1 second), because histogram_quantile can only interpolate inside a bucket and wide buckets give rough percentiles.
  • Chart p50, p95 and p99 together, for example with histogram_quantile(0.99, sum by (le) (rate(http_request_duration_seconds_bucket[5m]))), and alert on p99 even when the mean looks healthy.
  • Size thread pools and database pools with Little's law from your measured peak rate and holding time, then add headroom for bursts; the exact margin is a judgement you check under load.
  • When p99 rises while p50 stays flat, look first at lock waits, garbage-collection pauses and slow outbound calls.

The next lesson turns a percentile goal into a reproducible load test.

06. Review prompt

Without looking back, explain why a mean can look healthy while users wait, what p50, p95 and p99 each tell you, and why fan-out makes the tail more important. Write Little's law with the meaning of each letter, then use it to explain what happens to busy connections when latency doubles at the same request rate.

References

Your progress

Reading state

Sign in to save that you finished reading this lesson.

Sign in to mark reading complete

Prove this skill

8 questions · about 6 minutes · scored automatically. Marking a lesson as read never demonstrates a skill.
Take the skill check