Capacity planning — study guide
The concept's fragments, read in order.
For your capstone Take your own app to production
Your app's instance count, autoscaler bounds and memory limit are numbers someone has to choose, and capacity planning picks them from a load test and a forecast instead of a guess in a config file. Cost management comes next, and every number on the plan has a price.
Say your plan runs 3 instances today, two for the 50 requests per second peak plus a spare, with autoscaler bounds of 3 and 5 against a 60 percent CPU target. At the evening peak the three average 80 percent, and the autoscaler asks for 3 x 80 / 60 = 4. The ceiling of 5 is the forecast's answer, and it stays within what your one database can serve. The fourth needs minutes to start and pass its health checks, and your planned headroom carries the load until it does.
How much to run
Your app runs on a fixed amount of machine: some number of instances, each with its share of CPU and memory, and a database with a disk of a certain size. Capacity planning is choosing those amounts on purpose. Too little, and the busiest hour of the week turns into slow pages and errors; too much, and you pay every month for machines that sit idle. Guessing lands you in one of the two, and you usually find out which at the worst possible moment.
The plan is built from two inputs. The first is measured: how much load one copy of your app can carry while still meeting its latency target, which is the number a load test gives you. The second is forecast: how much load is coming, read from your production metrics today and projected over the months ahead. Divide the second by the first and you have a starting count.
The third ingredient is headroom, capacity you leave unused on purpose. Measuring one copy at its latency target already keeps response times off the steep part of the queueing curve; headroom on top of that absorbs the spike nobody predicted, covers forecast error and the time it takes to add capacity, and lets the service survive losing a machine. A capacity plan is those three things written down, for compute, memory and storage alike: what one instance can do, what demand will be, and how much spare you keep.
Size for the peak, not the average
Demand on a web app is never flat. Requests climb through the day, peak at whatever hour your users are most active, and fall away overnight, and the week has its own busy and quiet days layered on top. Your observability metrics already record this: the request rate on a dashboard over the last few weeks shows the shape, and the highest sustained point on it is the number capacity planning cares about.
Plan for that peak, not for the average like a restaurant that staffs its kitchen for the Friday dinner rush rather than for the average hour of the week, because the average hour is not when the customers are waiting. An average folds the quiet night hours into the busy evening and produces a number that is true of no hour at all; capacity sized to it falls short every day, at exactly the moment most users are watching. For your app, the busiest window runs at about 50 requests per second, and that is the figure the plan starts from.
Read the peak with some care. Use metrics at a fine enough interval to keep the real busy windows visible, and pay attention to how long the peak lasts as well as how high it goes. Look across several weeks so that one unusually quiet or loud day does not set the plan. Events the history cannot know about, a launch or a campaign, get added on top as separate, named assumptions rather than hoped to show up in an average.
What one copy can carry
To turn demand into a number of machines you need the other half of the division: how much load one instance of your app can carry. That number comes from a load test, and the right reading is not the rate at which the instance falls over. It is the highest request rate at which the instance still meets its target, for your app a p95 latency of 300 ms with the error rate still acceptable. Past that point the instance may keep answering, but too slowly to count.
Whatever caps that rate is the instance's bottleneck. One instance might run out of CPU first, or memory, or spend its time waiting on the database, and the resource readings taken during the test tell you which. That matters for planning, because adding instances only helps when the limit lives inside the instance. If every instance is waiting on the same single database, ten of them wait just as long as two.
Little's law is a useful companion. The average number of requests in flight equals the arrival rate times the average time each request spends in the system. If your app's average response time were 100 ms, then at a 50 requests per second peak about five requests are in progress at any moment: 50 x 0.1 = 5. That is how you sanity-check worker counts and database connection pools, which must cover the requests in flight at peak with room for bursts, and it carries a warning: if response time doubles, the number in flight doubles with it.
Headroom you leave on purpose
It is tempting to run every machine close to full, since idle capacity looks like waste. It is not, for three separate reasons. The first is queueing. Requests arrive at random, so some arrive while others are still being served and have to wait their turn. The busier the server, the more often that happens, and the wait does not grow gently like traffic on a highway, which flows freely while the road is partly full and then jams out of all proportion once the last few cars squeeze on.
The simplest queueing model makes the shape exact. With random arrivals and a single server, the average response time is the service time divided by one minus the utilization. At 50 percent busy a request takes twice its bare service time; at 80 percent, five times; at 90 percent, ten times; and at 100 percent the wait has no limit at all. Real systems differ in the details, especially when traffic is bursty, but the shape holds: flat for a long way, then a cliff, with the last few percent of utilization costing the most latency. This is why an instance's tested capacity sits where it does. Measured as the highest rate that still meets the latency target, it already stops short of the cliff, so the queueing penalty is built into that number rather than added on top.
The second reason is surprise. Traffic can jump faster than new machines can start, so the spare has to be running already. The third is failure. If your app needs exactly two instances at peak and one dies, the survivor is overloaded at the worst moment. Planning N+1, one more instance than the load needs, makes losing any single instance a non-event; larger services plan N+2, enough to carry peak load with two instances gone, so that one can be down for maintenance while another fails unexpectedly. The surprise and failure reasons are what headroom adds on top of tested capacity, written down as two numbers: a planning load factor, the fraction of tested capacity you plan to use at peak, and a count of spares.
Forecasting what is coming
Today's peak is only half of the demand input; the plan has to hold for the months ahead as well. The starting point is your own history: the peak request rate week by week, over as long a stretch as you have. Fit a simple trend through it and project it forward, then add the known events a trend cannot see, such as a launch, a campaign or a season that matters to your users.
The shape of the trend matters more than its precision. Linear growth adds roughly the same amount each period; compound growth adds roughly the same percentage. Start both from a 50 requests per second peak. Adding 2.5 a month gives 80 after a year, 50 + 12 x 2.5 = 80, and 110 after two. Growing 5 percent a month gives about 90 after a year, 50 x 1.05^12 = 89.8, and about 161 after two, 50 x 1.05^24 = 161.3. The two start out close and then pull apart, which is why the history, not hope, should decide which shape you assume.
How far ahead to forecast depends on how long it takes you to add capacity. Adding an instance through your deployment pipeline may take minutes, but a bigger database server, a migration or a purchase that needs approval can take weeks or months, and the forecast has to reach past the date you expect to need more by at least that lead time, plus the gap until your next review. A forecast is an assumption: write it down with its date, compare the real peak against it regularly, and re-plan when the two drift apart.
Sizing your app, step by step
Here is the whole calculation for your app, with every input marked as the assumption it is. Suppose the load test showed one instance holding a p95 under 300 ms up to 40 requests per second. Queueing is already accounted for in that figure, because capacity was measured at the latency target: the waiting that builds up as an instance gets busier is part of why it tops out at 40. Now choose a planning load factor of 70 percent, so you plan to use no more than 70 percent of that tested capacity at peak. Usable capacity per instance is then 40 x 0.7 = 28 requests per second. The 30 percent left over is allowance for spikes, for forecast error and for the time it takes to add capacity; failure is covered separately by a spare instance, so apply the factor once and do not pad the 28 again.
Today's peak is about 50 requests per second. 50 / 28 = 1.79, and since you cannot run part of an instance you round up to 2. Add one spare for N+1 (like carrying a spare tire, which exists precisely so that losing one wheel is an inconvenience rather than the end of the journey), and today's answer is 3 instances. Check the failure case rather than trusting it: if one of the 3 dies, the remaining 2 share 50 requests per second, 25 each, which is 25 / 40 = 62.5 percent of tested capacity and still under the 70 percent planning load factor.
Now the forecast. Suppose you plan for the peak to double to 100 requests per second within the year, a deliberately faster-growth planning assumption than either trend in the forecast, which gave 80 or about 90. 100 / 28 = 3.57, round up to 4, and one spare makes 5. With all 5 running, each carries 20 requests per second at peak, half its tested capacity; with one lost, the other 4 carry 25 each, the same 62.5 percent. Five instances is the number to have running before the forecast arrives.
Memory is sized from its own measurement, not from the request arithmetic. Suppose each instance peaked at 300 MB during the load test; a 30 percent margin gives 300 x 1.3 = 390 MB, so you would set the memory limit at the next size you can actually request above that, say 512 MB. The database sits outside this sum entirely: it is one machine, sized and watched on its own.
Storage only grows
Request load rises and falls every day; stored data mostly only rises. Every user, order and record your database keeps adds to it, and very little ever leaves. That makes storage the easiest resource to forecast and the most embarrassing to run out of, because a full disk does not slow the database down politely: writes start to fail.
Forecast storage as a growth rate. Read the used space on the database volume over the last few months, set aside one-off imports or clean-ups, and work out how many gigabytes a month it adds. The runway is then plain division. Suppose the volume is 100 GB, 40 GB is used, and it grows 2.5 GB a month. It is full in (100 - 40) / 2.5 = 24 months. You do not plan to the last byte, though: many teams start acting somewhere around 70 to 80 percent full, and if you choose 80 percent, 80 GB, the runway to that line is (80 - 40) / 2.5 = 16 months.
The catch is that the growth rate is not fixed; it tends to follow your users. If the forecast doubles them and the data they write doubles too, growth becomes 5 GB a month and the runway to 80 percent shrinks to (80 - 40) / 5 = 8 months. So re-measure the rate as usage changes, alert on the threshold rather than on full, and leave room for the things that live beside the data, such as transaction logs and temporary space, which a database needs even when the tables themselves are not growing.
Bigger machine or more machines
When one instance is not enough there are two ways to get more. Scaling up, or vertical scaling, replaces a machine with a bigger one: more cores, more memory, faster storage. Scaling out, or horizontal scaling, runs more copies of the same machine and spreads requests across them with a load balancer.
Scaling up is the simple one. Nothing about your app changes; it just has more room. But it has a ceiling, because there is a largest machine you can get, and you still have exactly one copy, so one failure takes everything with it. Scaling out avoids both problems, with a condition attached: the copies must be interchangeable, so that any healthy instance can serve any request. That holds when nothing a user needs, such as a session or an upload, lives only in one instance's memory or on its local disk. The app instances of a containerized web app are normally built that way, which is why they are the part you scale out.
The database usually is not. A single relational database holds the state every instance shares, and while extra read-only copies can take some read traffic, writes still go to one primary, and splitting them across machines is a much harder problem. So the common pattern for an app like yours is to scale the app instances out, scale the database up, and watch the database closely, because it is the one part where adding another copy is not a quick fix.
Letting the platform add copies
Autoscaling hands the decision to add an instance to the platform. You pick a metric that tracks load, such as average CPU utilization or requests per instance, and a target value for it, and a feedback loop adds instances when the metric runs above target and removes them when it runs below. The Kubernetes horizontal autoscaler, for example, which calls instances replicas, sets the desired count to the current count times the ratio of the current metric to the target, rounded up: 4 instances averaging 90 percent CPU against a 60 percent target becomes 4 x 90 / 60 = 6. That 60 percent is a CPU target, a measure of how busy the processors are, and it is a different number from the planning load factor, which is a share of the request rate one instance was tested to carry. One cloud provider's architecture guidance suggests 50 to 70 percent as a starting target for autoscaling, usually measured on CPU.
Every autoscaler takes two bounds, and both are planning decisions. The minimum is a floor that keeps a baseline running even in the quietest hour; for your app that means at least two, so losing one never leaves nothing serving. The maximum is a ceiling, so a bug or a flood of junk traffic cannot scale you without limit, and it should stay within what your database can serve, because more instances in front of a saturated database only queue more requests.
The catch is time. Before a new instance takes traffic, the metric has to be collected and evaluated, and the instance has to start, warm up and pass its health checks. Scaling in is deliberately slower still, so the loop does not thrash up and down: Kubernetes, by default, looks back over five minutes before reducing. So autoscaling follows demand with a lag, like calling in extra staff when the queue grows: they do come, but they take time to arrive, and the people already on shift have to cover the gap. The headroom you planned is what carries the load in that gap. Autoscaling turns the question from how many instances to run all day into what floor, what ceiling and what target; it does not remove the need for the plan.
A plan you revisit
Taking your own app to production is where these numbers stop being hypothetical. On your machine it served one user; in production it serves a few thousand a day and growing, peaking at about 50 requests per second, against a p95 target of 300 ms and an availability target of 99.9 percent. Without a plan, the instance count is whatever someone typed into a config file once, and the first sign it was wrong is users noticing.
The deliverable is short: one page that records what one instance carries (from which load test, on what date), the current peak and the forecast with its assumptions, the planning load factor and the number of spares, the resulting instance count and autoscaler bounds, the memory limit, and the storage runway with its action threshold. Keep it beside the configuration that applies those numbers, so the two cannot quietly disagree.
Then treat it as alive. Re-run the load test after releases that change how much work a request does, compare the real peak against the forecast every month, and re-plan when they drift apart. Every number on that page also has a price, and turning capacity into a bill you can explain is the work of cost management, which builds on this.
Next on your path to Take your own app to production: Incident response ▸