Capacity planning and load testing
The latency knee measured — p99 from 14 ms to 110 ms for twelve percent more traffic — headroom, the real bottleneck, and the three ways a load test lies.
Capacity planning answers one question before it becomes an incident: how much traffic can this take, and what happens just before it cannot? The second half is the one people skip, and it is the one that matters, because systems do not fail at 100% — they fail well before it.
The curve nobody expects
Four workers, each request taking 10 milliseconds, so the theoretical capacity is 400 requests a second. Offer load at rising fractions of that and measure latency:
### utilisation offered/s p50 ms p99 ms
### 50% 200 10 12
### 70% 280 11 12
### 80% 320 11 14
### 90% 360 63 110
### 95% 380 218 397
### 99% 396 236 515Read it slowly, because it is the most important table in this course.
From 50% to 80% utilisation, latency barely moves. From 80% to 90% — twelve percent more traffic — the p99 goes from 14 ms to 110 ms. At 95% it is nearly 400 ms. The service time never changed. Every request still takes 10 ms of actual work. The rest is waiting in a queue.
That is queueing theory, and the shape is universal: latency stays flat until utilisation approaches capacity, and then rises steeply — a hockey stick, not a slope. The exact knee in that measurement is noisy, because the load generator paces with Thread.sleep and runs in a container; the shape is not noise, and it holds for a thread pool, a database connection pool, a CPU and a checkout queue in a shop.
Why the averages lie
In that table, the p50 at 90% utilisation is 63 ms and the p99 is 110 ms. An average would sit somewhere in between and look acceptable. Plan on the tail.
- A user loading a page that makes twenty requests hits the p95 of at least one of them most of the time. Tail latency is the latency your users actually experience.
- The requests in the tail are usually the expensive ones — the big customer, the long report — so the tail is disproportionately your most valuable traffic.
Estimating before you measure
The system design course's estimation lesson gives the arithmetic. For capacity it reduces to four questions:
- What is the peak, not the average? Traffic is not flat; the daily peak is often three to ten times the mean, and a marketing email or a sale is a multiple of that.
- What does one request cost — in CPU milliseconds, database queries, connections held?
- What is the bottleneck? It is almost never the application servers, which scale by adding more. It is the thing that does not: the database's connections, a downstream service's rate limit, a single lock.
- What is the growth, and how long does it take to add capacity? If provisioning takes a week, you need to know a week before you run out.
Question three decides everything. Doubling your application servers does nothing if they are all waiting on the same database connection pool — the connection pooling lesson's point about pool size being a latency knob, seen from the capacity side.
Load testing, and the three ways it lies
A load test is how you replace the estimate with a number. It is also easy to run one that produces a comforting and wrong number:
- Coordinated omission. A load generator that waits for each response before sending the next request slows down exactly when the system slows down — so it stops measuring the latency real users would see. Real users do not wait politely. Use a tool that sends at a fixed rate regardless of responses; wrk2, Gatling and k6 in open-model mode do.
- Unrealistic data. Testing against an empty database, or one customer, or a warm cache. The query that is fast on a hundred rows is the one that falls over on ten million.
- Testing in the wrong place. A load test from your laptop measures your laptop's network. Test from inside the same network, against an environment shaped like production.
What to actually measure
Not "how many requests per second before it errors". Measure:
- The throughput at which your latency SLO breaks. If the promise is p99 under 200 ms, capacity is the load at which p99 crosses 200 ms — which, in the table above, is well before any error.
- What fails first, and how. Does it slow down gracefully, shed load, or fall over and stay down?
- Recovery. When the load drops, does it come back on its own, or does a backlog keep it pinned?
The last one is underrated. A system that recovers by itself after a spike is operationally very different from one that needs a restart — and you only find out which you have by pushing it past its limit on purpose, somewhere it is safe to do so.
Under the hood: Little's Law, and why the hockey stick is mathematically inevitable
The table at the top of this lesson is not an empirical curiosity — it is what queueing theory predicts, and the underlying relationship is simple enough to use directly. Little's Law states L = λW: the average number of requests in a system (L) equals the arrival rate (λ) multiplied by the average time each request spends in the system (W). It holds for any stable queue, regardless of the arrival pattern or the service-time distribution, which is what makes it useful — it is not a model that needs tuning to your system, it is an identity that is always true for a system in steady state.
For a simple queue with utilisation ρ (the fraction of capacity in use, 0 to 1) and a single server, average wait time scales roughly as ρ/(1−ρ) times the service time — the textbook M/M/1 result. Read what that fraction does as ρ approaches 1: at ρ=0.5, the factor is 1; at ρ=0.8, it is 4; at ρ=0.9, it is 9; at ρ=0.95, it is 19; at ρ=0.99, it is 99. The denominator (1−ρ) is heading to zero, so the whole expression heads to infinity — there is no finite utilisation at which the curve is "almost flat" right up to 100%; the steepness is baked into the shape of 1/(1−ρ) itself, not into any particular system's quirks. That is the mathematical reason the lesson's table shows latency barely moving from 50% to 80% and then exploding from 80% to 95%: it is the same curve every queue produces, whether the servers are HTTP worker threads, database connections, or people at a supermarket till. A multi-server queue (more workers, like the four in this lesson's example) softens the curve somewhat and shifts where the knee sits, but it does not remove the shape — capacity headroom is still what keeps you on the flat part.
This is also why "run below about 70%" is not an arbitrary number: it is choosing to operate on the part of the ρ/(1−ρ) curve where the multiplier is still small (roughly 2.3x at ρ=0.7) and, critically, where a modest further increase in ρ (a traffic spike, a lost instance raising everyone else's share of the load) still lands you somewhere survivable rather than off the edge of the curve. Running at 90% leaves far less room: the same absolute traffic increase that was harmless at 70% can push utilisation past 95%, where the multiplier roughly doubles again.
Try it yourself
Use Little's Law to catch an undersized pool before load testing
A service is expected to handle 150 requests per second at peak, with a latency SLO of 300ms at p99 (each request, including any queueing, must finish within 300ms). The database connection pool is configured with 20 connections.
Using L = λW, roughly how many requests should the system be able to hold in flight to meet that SLO — and what does that suggest about whether 20 connections is enough, even before running a single load test?
Working the numbers
L = λW = 150 requests/second × 0.3 seconds = 45. To meet a 300ms p99 at 150 requests per second, the system needs to be able to hold roughly 45 requests in flight simultaneously without any of them queueing for a resource. A pool of 20 connections is well under that — if each request needs a connection for the bulk of its time in the system, more than half the in-flight requests at any instant would have no connection available and would be queueing for one, which directly eats into the 300ms budget before the query even starts. This does not prove the pool is definitely too small (it depends on how much of each request's time is actually spent holding a connection versus doing other work), but it is exactly the kind of back-of-envelope check that should raise the question before a load test is built — Little's Law turns "the pool feels small" into an actual number to check against.
Misconceptions
- "If load testing shows we can handle 2x current peak, we have 2x headroom." Only if the test measured the load at which the SLO breaks, not the load at which errors start. As this lesson shows, latency degrades severely well before a system starts rejecting requests outright — "it didn't error at 2x" and "it stayed within SLO at 2x" are different claims, and only the second one is the headroom number that matters.
- "Little's Law only applies to queueing systems like message brokers." It applies to any stable system where things arrive and eventually leave — HTTP request handling, database connections, thread pools, a support ticket queue. It is a general conservation law, not a Kafka-specific or MQ-specific formula, which is exactly why it is useful as a sizing check for a connection pool or a thread pool.
Sources
- John D. C. Little, "A Proof for the Queuing Formula: L = λW" (1961), and standard queueing-theory treatments of the M/M/1 model.
- John Allspaw and contributors, The Art of Capacity Planning (O'Reilly).
- Gil Tene, "How NOT to Measure Latency" — coordinated omission in load generators; k6 and Gatling documentation on open-model load generation.