Course contents

15. Performance Engineering Lesson 1 of 8

Capacity Planning

Estimate peak traffic, compute per-instance capacity, size a fleet at a target utilisation with N+1, verify it in a simulation, and project growth.

16 minAdvanced9 steps
Loading the interactive simulation…
Loading the knowledge check…

Summary

  • Average rate = daily users × requests per user ÷ 86,400 s; the design target is the peak (often 2–4× the average).
  • Per-instance capacity ≈ workers ÷ service time; plan at a target utilisation such as 70%, and verify with a load test.
  • Instances = ⌈peak ÷ usable capacity per instance⌉, then add the failure margin (N+1 or a zone).
  • Growth silently eats the margin: project the peak forward by at least the provisioning lead time.
  • Every tier (database, cache, queue, quotas) needs its own plan.
Deep diveHow it works, trade-offs, failure modes, misconceptions and the interview angle.

What it is

Capacity planning answers "how much infrastructure do we need, and when?" with arithmetic you can check. It combines an estimate of demand (requests per second at peak), a measurement of supply (what one instance can do at an acceptable latency), a failure margin, and a growth projection, and repeats the exercise for every tier of the system.

Why it exists

Too little capacity shows up as latency, errors and outages at the worst moment: the evening peak, a launch, a failover. Too much is money spent for nothing. Autoscaling narrows the gap but does not remove it: it reacts with a delay, it is bounded by quotas, and stateful tiers (databases, brokers) rarely scale on demand. A plan tells you where the limits are before users find them.

How it works

1. Demand. Start from what you know: daily active users × requests per user per day ÷ 86,400 s = average requests per second. Then apply a peak factor for the busiest period (2–4× is common for consumer traffic; measure it from your own data where possible). Add known events: launches, campaigns, seasonal peaks. Distinguish reads from writes if they hit different tiers.

2. Supply per instance. For a worker-based service, throughput at 100% busy ≈ workers ÷ service time (8 workers at 40 ms ≈ 200 req/s). Pick a target utilisation at peak: queueing delay grows like ρ ÷ (1 − ρ), so latency-sensitive services usually aim for 60–80%. Then verify with a load test at the target load: measured utilisation and latency should match the prediction. If they do not, your model of the service is wrong (often a hidden dependency or lock), and that is worth knowing before production does.

3. Divide and round up. N = ⌈peak ÷ (capacity per instance × target utilisation)⌉. Predict the resulting utilisation (peak ÷ (N × capacity)) and check it.

4. Failure margin. Size so that the fleet minus its largest failure domain still carries the peak: N+1 for single-instance failures and rolling deploys, or zone-level redundancy (with three zones, each zone's share must be carried by the other two, so each zone runs at most at ~2/3 of the target). The survivors' utilisation after a failure is the number to check, not the healthy utilisation.

5. Growth and lead time. Project the peak forward: peak × (1 + monthly growth)^months. Re-plan at least one provisioning lead time ahead (quota requests, reserved capacity, hardware, database migrations take weeks).

6. Every tier. Repeat for each shared resource: database connections and IOPS, cache memory and network, broker partitions and consumer throughput, third-party rate limits, cloud quotas. The tier with the least margin is your next incident.

When to use it

  • Before a launch, a migration or an expected traffic event.
  • In a regular review (monthly or quarterly) for growing services.
  • When choosing instance sizes or reserved capacity commitments.
  • In system design interviews, as back-of-the-envelope estimation.

When not to

  • For tiny internal tools where one instance is idle at peak: a minimum of two instances for availability is the whole plan.
  • As a substitute for measurement: estimates guide, load tests and production metrics decide.
  • When the workload is genuinely unpredictable and elastic infrastructure (serverless, autoscaling with generous quotas) can absorb it at acceptable cost; even then, plan the non-elastic tiers.

Trade-offs

Choice You gain You pay
Low target utilisation (50–60%) Stable latency, burst and failure headroom More instances per unit of traffic
High target utilisation (85%+) Lower cost Volatile latency; failures and bursts become incidents
N+1 per service Survive any one instance loss or a rolling deploy One idle-ish instance per service
Zone-level redundancy Survive a zone outage Up to ~50% extra capacity with two zones, ~33% with three
Reserved / committed capacity Lower unit price, guaranteed availability Paying for it whether used or not
Autoscaling Pay closer to actual use Reaction delay, quota limits, cold starts; still needs a minimum plan

Alternatives

  • Autoscaling covers short-term variation inside a planned envelope (minimum, maximum, quotas). See the autoscaling lesson.
  • Load shedding and graceful degradation keep the core working when demand exceeds the plan anyway.
  • Queue-based load levelling moves non-urgent work out of the peak, lowering the peak you must size for.

Failure modes

  • Sizing for the average: fine at noon, overloaded every evening.
  • Healthy-only plans: the fleet carries the peak only with every instance up; one failure or deploy at peak overloads the rest.
  • Stale plans: growth consumes the N+1 margin silently; the dashboard shows 69% busy until an instance fails.
  • Planning one tier: the app tier scales, the database saturates.
  • Trusting unverified numbers: a per-instance estimate that ignores a lock, a GC pause pattern or a slow dependency.
  • Retries and queues as "capacity": longer queues turn overload into latency; client retries add load exactly when there is none to spare. The lesson's challenge shows both.

In production

Keep the plan as a small, versioned model (a spreadsheet or code) with its inputs: peak traffic per endpoint, per-instance throughput from the latest load test, target utilisation, failure domain, growth rate, lead time. Re-run load tests after significant changes. Alert on utilisation of the surviving capacity (fleet utilisation × N ÷ (N − 1)), not just the healthy average. Track quotas as capacity. Review before known peaks (sales events, launches) with the teams that own each tier.

Common misconceptions

  • "We're at 60% CPU, so we have 40% headroom." Latency rises well before 100%, and a failure redistributes load: three instances at 70% become two at 105%.
  • "Autoscaling means no capacity planning." It needs minimums, maximums, quotas, and capacity in the tiers it does not scale.
  • "Peak is just the average times two." The factor varies by product and day; measure it, and include planned events.
  • "Capacity is about the app servers." Databases, caches, brokers and third-party limits are usually the binding constraints.

Interview angle

Interviewers expect quick, explicit arithmetic: users → average RPS → peak RPS (state the peak factor), per-instance throughput from concurrency and latency, instance count at a target utilisation, plus N+1 or zone redundancy. Then storage and bandwidth if relevant. Round numbers aggressively (86,400 ≈ 100,000 s/day is a common shortcut, stated as such), say which tier will bind first, and how you would verify (load test) and keep the plan current (growth, lead time).

Model assumptions

The simulation is a simplified model, not a benchmark. Each app instance has 8 workers and a 40 ms mean service time (lognormal, cv 0.5), so at most about 200 req/s, and queues up to 30 requests before rejecting with 503. Arrivals are Poisson. The load balancer uses round robin with health checks every 500 ms (2 failures eject, 2 successes restore). Network time is about 23 ms each way. Utilisation is the fraction of busy workers, not CPU; the engine has no CPU, memory or GC model. The demand figures (120,000 users, 50 requests a day, a 3× peak factor, 12% monthly growth) are illustrative. Only the app tier is modelled here; a real plan covers every tier.

The lesson as textEvery step of the simulation, written out.
  1. 1. Start from demand: average requests per second

    Capacity planning starts with a number you can defend. The product has about 120,000 daily active users, and analytics say each makes about 50 requests a day. That is 6 million requests ÷ 86,400 seconds ≈ 70 requests per second on average.

    One app instance handles that easily: it is about a third busy. But nobody runs at the average.

    Scene: Users send about 70 requests per second, the daily average, through a load balancer to one app instance, which is about a third busy.

    Key idea: Average load = daily users × requests per user ÷ 86,400 s. It is the starting point, never the design target.

  2. 2. Design for the peak, not the average

    Traffic is not flat: evenings, Mondays and campaigns concentrate it. Here the busiest hour carries about 3× the average, so the design load is about 210 requests per second. Peak-to-average ratios of 2–4× are common for consumer products; measure yours.

    One second in, the evening peak arrives. The single instance cannot finish 210 per second: its queue fills, requests are rejected and P95 climbs.

    Scene: Traffic rises to 210 requests per second, the evening peak. The single app instance is overloaded: its queue is full and some requests are rejected.

    Key idea: Peak = average × peak factor. Size for the peak (and its bursts), because that is when users notice.

  3. 3. What one instance can do, and what it should do

    Next, the supply side. An instance has 8 workers and a request takes about 40 ms of work. Its ceiling is 8 ÷ 0.04 s = 200 requests per second, but only at 100% busy, where queues explode. We plan at a target utilisation of 70%: 140 requests per second per instance.

    Verify the estimate before trusting it: we send exactly 140 per second to the one instance. The model predicts 140 × 0.04 s ÷ 8 = 70% busy, and that is what it shows, with P95 still reasonable.

    Scene: A load test sends 140 requests per second to one instance. It is about 70% busy, matching the prediction, and nothing fails.

    Key idea: Per-instance capacity = workers ÷ service time. Plan at a target utilisation (here 70%), and verify the prediction with a load test.

  4. 4. Instances = peak ÷ usable capacity per instance

    Now divide: 210 ÷ 140 = 1.5, so round up to 2 instances. Predicted utilisation at peak: 210 ÷ (2 × 200) ≈ 53% each.

    The peak is back on, and the engine agrees: both instances settle around half busy, no request is rejected, and P95 drops back to near the bare service time.

    Scene: Two app instances share the 210 requests per second peak. Each is about half busy and nothing is rejected.

    Key idea: N = ⌈peak ÷ (capacity × target utilisation)⌉, then check the predicted utilisation against measurements.

  5. 5. An instance dies at peak

    One and a half seconds in, App 2 crashes at the evening peak. After the health checks notice (about a second), all 210 requests per second go to App 1, which can do at most 200.

    App 1 saturates, its queue fills and requests are rejected for as long as App 2 is gone. Two instances were enough for the load but not for the load plus a failure. Deployments, which take instances out one at a time, cause the same squeeze.

    Scene: App 2 crashes. Once the load balancer removes it, App 1 receives all 210 requests per second, saturates and rejects some of them.

    Key idea: Capacity that only covers the peak with every instance healthy is not enough: failures and deployments happen at peak too.

  6. 6. N+1: one spare at peak

    Add a third instance. With App 2 still down, the two survivors share 210 per second: about 53% busy each, the same as the healthy two-instance plan. Nothing is rejected.

    This is N+1: N instances for the peak, plus one so that losing any single instance (or deploying one at a time) costs nothing visible. Larger fleets plan for losing a whole availability zone instead (for example 3 zones, each able to carry half the peak).

    Scene: A third instance is added. App 2 is still down, but App 1 and App 3 share the 210 requests per second at about half busy each and nothing fails.

    Key idea: N+1: size so that the fleet minus its largest failure domain still carries the peak.

  7. 7. Six months later: the plan has expired

    App 2 is repaired, and the product grows about 12% per month. Six months on, the peak is 210 × 1.12⁶ ≈ 415 requests per second. With all three instances healthy that is about 69% each, right on target, so the dashboard looks fine.

    Then, two seconds in, App 3 fails. The two survivors would need to take about 207 per second each, more than their 200 maximum: their queues fill, requests are rejected and P95 jumps. The N+1 margin was eaten by growth without anyone noticing.

    Scene: Six months of growth raise the peak to 415 requests per second. Three instances handle it at about 69% busy, but when App 3 fails the two survivors are saturated, reject requests and P95 rises.

    Key idea: A capacity plan has an expiry date: project growth and re-check N+1 against the future peak, not today’s.

  8. 8. Re-plan: ⌈415 ÷ 140⌉ + 1 = 4

    Redo the arithmetic for the projected peak: 415 ÷ 140 = 3.0 (2.96), round up to 3, plus one spare: 4 instances. A fourth is added while App 3 is still down.

    The three survivors run at about 69% each and P95 is back to normal. In practice you would buy this capacity *before* the growth arrives: provisioning, quota increases and hardware lead times take weeks, and autoscaling only covers what you have quota for.

    Scene: A fourth instance is added. With App 3 still down, App 1, App 2 and App 4 share 415 requests per second at about 69% busy each, and latency is normal again.

    Key idea: Plan for the projected peak plus the failure margin, early enough to cover lead times.

  9. 9. The capacity planning loop

    App 3 is back: four instances run at about 52% at the projected peak. That spare capacity is the plan working, not waste.

    The loop: estimate demand (average × peak factor), measure supply (per-instance throughput at a target utilisation, verified by a load test), divide and add failure margin (N+1 or a zone), project growth and re-check before lead times bite. Then repeat it for every shared tier: the database, caches and queues behind these instances have limits too.

    Scene: All four instances are healthy and share 415 requests per second at about half busy each.

    Key idea: Demand × peak factor ÷ (capacity × target utilisation), plus failure margin, projected forward. Repeat for every tier.