Capacity Planning & Load Tests
Capacity planning answers one question ahead of time: will we have enough resources to meet the SLO at the next peak, even with a failure? It combines a demand forecast, a measured capacity per instance (from load tests), a target utilisation below the queueing knee, and redundancy for failures. Autoscaling helps with daily swings, but it doesn't remove the need to plan: quotas, database limits, reserved capacity and the time to provision all have lead times.
Step 1: forecast the peak
Use the peak (for example the busiest 5 minutes of each week), not the average. The model fits a straight line to 26 weeks of seeded weekly peaks and projects it 13 weeks ahead, then adds a known event (a sale that historically doubles the normal peak):
import random
rng = random.Random(4)
weeks = list(range(26))
peaks = [1200 + 35 * w + rng.gauss(0, 60) for w in weeks] # requests per second
n = len(weeks)
mx, my = sum(weeks) / n, sum(peaks) / n
slope = sum((x - mx) * (y - my) for x, y in zip(weeks, peaks)) / sum((x - mx) ** 2 for x in weeks)
intercept = my - slope * mx
resid = [y - (intercept + slope * x) for x, y in zip(weeks, peaks)]
spread = (sum(r * r for r in resid) / (n - 2)) ** 0.5
target_week = 38
forecast = intercept + slope * target_week
print(f"growth: {slope:.1f} rps per week; last observed peak {peaks[-1]:.0f} rps")
print(f"week {target_week} forecast: {forecast:.0f} rps (+2 sigma: {forecast + 2 * spread:.0f} rps)")
print(f"sale week (2x normal peak): {2 * (forecast + 2 * spread):.0f} rps")growth: 33.7 rps per week; last observed peak 2058 rps
week 38 forecast: 2500 rps (+2 sigma: 2611 rps)
sale week (2x normal peak): 5221 rps
Adding two standard deviations of the residuals is a simple way to plan for a "bad but normal" week rather than an average one. Business events (launches, sales, a marketing campaign) come from talking to the business, not from the curve.
Step 2: measure capacity per instance
A load test finds the throughput at which one instance stops meeting the SLO. Run it against a production-like environment (same instance type, same database size, realistic request mix), ramping the arrival rate in steps. A k6 script that does this (sample, not run here: k6 isn't in this course's image and needs a target to hit):
// loadtest.js — run with: k6 run loadtest.js
import http from 'k6/http';
import { check } from 'k6';
export const options = {
scenarios: {
ramp: {
executor: 'ramping-arrival-rate', // requests per second, independent of response time
startRate: 50,
timeUnit: '1s',
preAllocatedVUs: 200,
maxVUs: 1000,
stages: [
{ target: 100, duration: '2m' },
{ target: 200, duration: '2m' },
{ target: 300, duration: '2m' },
{ target: 400, duration: '2m' },
],
},
},
thresholds: {
http_req_failed: ['rate<0.001'], // the availability SLO
http_req_duration: ['p(99)<800'], // the latency SLO, in ms
},
};
export default function () {
const res = http.post('https://checkout.staging.example.com/checkout',
JSON.stringify({ cart: 'load-test-cart' }),
{ headers: { 'Content-Type': 'application/json', 'x-load-test': '1' } });
check(res, { 'status is 201': (r) => r.status === 201 });
}Use an arrival-rate executor: with a fixed number of virtual users, a slowing server receives fewer requests
(each user waits for its response), which hides exactly the overload you're looking for. Tag load-test traffic
(x-load-test: 1) so it's excluded from production SLIs.
The model reads the results of such a test (requests per second against p99 latency and error rate, per step) and finds the highest step that still meets the SLO:
RESULTS = [ # rps per instance, p99 latency ms, error ratio (from a stepped load test)
(50, 180, 0.0000), (100, 190, 0.0000), (150, 220, 0.0001), (200, 290, 0.0002),
(250, 450, 0.0004), (300, 790, 0.0009), (350, 1900, 0.0060), (400, 5200, 0.0410),
]
SLO_P99_MS, SLO_ERRORS = 800, 0.001
ok = [rps for rps, p99, err in RESULTS if p99 < SLO_P99_MS and err < SLO_ERRORS]
limit = max(ok)
print(f"highest step within SLO: {limit} rps per instance")
for rps, p99, err in RESULTS:
mark = "within SLO" if rps in ok else "violates SLO"
print(f" {rps:>3} rps: p99 {p99:>5} ms, errors {err:.2%} {mark}")highest step within SLO: 300 rps per instance
50 rps: p99 180 ms, errors 0.00% within SLO
100 rps: p99 190 ms, errors 0.00% within SLO
150 rps: p99 220 ms, errors 0.01% within SLO
200 rps: p99 290 ms, errors 0.02% within SLO
250 rps: p99 450 ms, errors 0.04% within SLO
300 rps: p99 790 ms, errors 0.09% within SLO
350 rps: p99 1900 ms, errors 0.60% violates SLO
400 rps: p99 5200 ms, errors 4.10% violates SLO
300 rps per instance is the edge of the cliff, not a place to run: one slow dependency and it falls over. Plan for a utilisation target, typically 60–70% of that limit.
Step 3: size it, with redundancy
import math
PEAK_RPS = 5220 # sale-week peak from step 1
PER_INSTANCE = 300 # SLO limit from step 2
TARGET_UTILISATION = 0.65
ZONES = 3
usable = PER_INSTANCE * TARGET_UTILISATION
needed = math.ceil(PEAK_RPS / usable)
print(f"instances to serve the peak at {TARGET_UTILISATION:.0%}: {needed}")
# N+1 zones: losing one of three zones must leave enough capacity in the other two
per_zone = math.ceil(needed / (ZONES - 1))
total = per_zone * ZONES
print(f"survive the loss of 1 of {ZONES} zones: {per_zone} per zone, {total} in total "
f"({total / needed - 1:.0%} extra)")
print(f"utilisation at peak with all zones up: {PEAK_RPS / (total * PER_INSTANCE):.0%} of the SLO limit")instances to serve the peak at 65%: 27
survive the loss of 1 of 3 zones: 14 per zone, 42 in total (56% extra)
utilisation at peak with all zones up: 41% of the SLO limit
Surviving a zone loss costs at least 50% extra capacity with three zones (a little more after rounding up to whole instances); with four zones it's 33%. Remember the dependencies too: the database, caches, queues and third-party quotas need the same exercise.
Keep the plan honest
- Re-run load tests after big changes (new features, framework upgrades); capacity per instance drifts.
- Compare forecasts with reality every month and adjust the model.
- Track saturation SLIs (CPU, memory, connection pools, queue length) as early warnings, routed as tickets.
- Test the failover capacity for real: drain a zone during a quiet period (Chaos Engineering).
Test yourself
Why should a load test use an arrival-rate executor rather than a fixed number of virtual users?
- It uses less memory
- With fixed users, a slowing server receives fewer requests, which hides overload
- k6 requires it
- It produces nicer graphs
One instance meets the SLO up to 300 rps. Why plan for about 200 rps per instance?
- Licences cost less
- 300 is the edge of the latency knee; headroom absorbs slow dependencies and forecast errors
- To use more instances
- Load tests always overestimate by 50%
A service needs 20 instances at peak and runs in 3 zones. How many instances survive the loss of a zone?
- 20, 7 per zone
- 30, 10 per zone
- 60
- 21