Sub topic 1
What scaling means: capacity versus load
Prerequisites
This chapter assumes you know the following:
- How a client sends requests to a web application and gets responses back.
- What CPU, memory, disk, and network do on a server.
- What a web server and a database do, at a basic level.
You do not need to know any scaling techniques yet. This chapter sets up the vocabulary that the rest of the tutorial uses.
Two quantities: load and capacity
Every discussion of scaling compares two quantities.
- Load is the amount of work that users and other systems ask a system to do. Examples are requests per second, concurrent connections, messages arriving in a queue, or gigabytes of data to process.
- Capacity is the maximum amount of work the system can handle while still meeting its performance goals. Examples are the requests per second one server can serve before response times become unacceptable.
Scaling means changing capacity so it stays ahead of load. Load is usually something you observe and forecast. Capacity is something you change.
The comparison matters because a system does not fail at a single sharp line. As load approaches capacity, queues form and response times grow. Past capacity, requests time out or get rejected. A system that is "fine" at 60% of capacity may behave very differently at 95%.
Capacity is defined against a goal
Capacity has no meaning without a performance target. A server that handles 2,000 requests per second with a median response time of 50 ms may handle 3,000 requests per second if you accept a response time of 800 ms. Which number is its capacity?
The answer depends on the goal you set. Teams usually state the goal as a service level objective (SLO). An SLO is a measurable target such as "99% of requests complete in under 300 ms." Capacity is then the highest load at which the SLO still holds.
This is why you cannot read capacity from a spec sheet. A machine with 16 cores does not have a fixed capacity of "16 cores' worth of requests." The real figure depends on the code, the request mix, the data, and the latency target. You find it by measuring, usually with a load test.
Headroom
Headroom is the gap between current load and capacity. If a service handles 600 requests per second at peak and its capacity is 1,000, headroom is 400 requests per second, or 40% of capacity.
You want headroom for several reasons:
- Traffic spikes arrive faster than you can add capacity.
- If one server fails, the others absorb its share.
- Deployments and maintenance take servers out of service temporarily.
- Latency rises sharply when utilization is high, so running near 100% hurts users even before anything breaks.
Headroom has a cost, because unused capacity is paid for but idle. Choosing how much to keep is one of the central trade-offs in capacity planning. Later chapters return to it when they cover cost and autoscaling.
What counts as "load"
Load is not a single number. Different parts of a system feel different kinds of load, and a useful scaling decision starts by describing load in the terms that matter for that component.
| Component | Typical load metric | What it stresses |
|---|---|---|
| Web or API server | Requests per second, concurrent connections | CPU, memory, network |
| Database | Queries per second, read/write ratio, rows touched | Disk I/O, memory (cache), CPU |
| Message queue | Messages per second, backlog size | Disk, network |
| Batch job | Data volume, number of records | CPU, memory, disk throughput |
| Storage | Bytes stored, IOPS (I/O operations per second) | Disk space, disk throughput |
Two details are easy to miss.
First, the mix of requests matters as much as the count. One thousand requests per second of cheap cache reads is a very different load from one thousand requests per second of report generation. A rise in the share of expensive requests raises load even if the request count stays flat.
Second, load is not constant. It has a daily and weekly shape, and it can have sudden bursts. Capacity planning needs to cover the peak, not the average. A system sized for average traffic will fail every time traffic peaks.
Two kinds of load growth
Load changes in two broad ways, and they call for different responses.
- Growth is a steady, often predictable rise. A product gains users month after month. You can plan for it, order hardware, and schedule upgrades.
- Spikes are short, sharp rises. A marketing email, a news mention, a sale, or a retry storm after an outage can multiply traffic within seconds. You cannot plan a purchase around a spike. You need spare headroom or a way to add capacity quickly.
Many production problems come from treating one as the other. Buying a bigger server fixes slow growth but may not help a burst that lasts ten minutes. Automatic capacity changes help bursts but add complexity that steady growth may not justify.
Two ways to add capacity
There are two basic ways to raise capacity.
- Vertical scaling (scaling up) makes one machine more powerful. You give it more CPU cores, more memory, faster disks, or a faster network card.
- Horizontal scaling (scaling out) adds more machines and spreads the work across them.
The following sections of this tutorial cover each in detail. Here you only need the central difference. Vertical scaling changes the size of a unit. Horizontal scaling changes the number of units.
Scaling can also go down. Reducing capacity when load falls saves money. The terms apply in both directions, so you will see "scale in" and "scale down" used for removing capacity.
A worked example: estimating capacity from measurements
Suppose a single application server runs a load test. You raise the request rate in steps and record the 95th percentile (p95) response time. The p95 is the time under which 95% of requests complete.
| Requests per second | p95 latency (ms) | CPU utilization |
|---|---|---|
| 200 | 40 | 25% |
| 400 | 45 | 50% |
| 600 | 60 | 72% |
| 800 | 150 | 90% |
| 900 | 700 | 98% |
Your SLO says p95 must stay under 200 ms. The server meets it at 800 requests per second and fails it at 900. The test suggests a capacity of about 800 requests per second.
You would not run production at 800, though. Latency climbed from 60 ms to 150 ms between 600 and 800, which shows how steeply it rises as the server fills up. A common practice is to plan for a peak load that uses 60 to 70% of tested capacity. That leaves room for spikes and failures. The exact figure is a judgment call.
Now you can use a small script to turn these figures into a server count. The script takes the expected peak load, the tested capacity per server, and the target utilization. It then adds one spare server so the service survives a single failure.
import math
def servers_needed(peak_rps, capacity_per_server, target_utilization, spare=1):
usable_per_server = capacity_per_server * target_utilization
return math.ceil(peak_rps / usable_per_server) + spare
peak = 3000 # expected peak requests per second
tested = 800 # tested capacity of one server at the SLO
target = 0.65 # plan to use 65% of tested capacity
print(servers_needed(peak, tested, target))Running this prints 7. Each server is planned to carry 520 requests per second (800 × 0.65). Dividing 3,000 by 520 gives 5.77, which rounds up to 6, and the spare server brings the total to 7.
This calculation assumes the servers share work evenly and that adding servers adds capacity in a straight line. Both assumptions can fail, and a later chapter on Amdahl's law shows why.
Common mistakes
These errors come up often when people first reason about scaling.
- Confusing average with peak. Sizing for average load leaves no capacity when traffic peaks.
- Quoting capacity without a latency target. "This server handles 3,000 requests per second" says little unless you also state the response time at that rate.
- Using one number for all load. Requests per second hides differences in request cost. Track the mix.
- Scaling before measuring. Adding servers to a system limited by a slow database query adds cost and fixes nothing. The next chapters cover how to find the real limit first.
- Ignoring failure. Capacity that only just covers peak load with every server healthy is not enough. When one server fails, the remainder must still carry the load.
- Treating test capacity as production capacity. A load test uses synthetic traffic. Real traffic has a different mix, cache state, and data size, so verify against production metrics.
How this chapter connects to the rest
The remaining chapters build on the load and capacity framing.
- The next chapter defines latency, throughput, and utilization, the measurements used to compare load with capacity.
- After that, you will see how to find the bottleneck, which is the single resource that limits capacity first.
- Vertical and horizontal scaling then follow as the two responses, with the load balancing, stateless design, and data tier work that horizontal scaling demands.
- The later chapters cover scaling limits, trade-offs, autoscaling, and how to choose a strategy.
Key points
- Load is the work requested. Capacity is the work a system can handle while meeting a performance goal.
- Capacity only has meaning against a target such as an SLO, and you find it by measuring.
- Headroom is the gap between load and capacity. It absorbs spikes, failures, and maintenance.
- Load has a type, a mix, and a time shape. Plan for peak, not average.
- Vertical scaling makes a machine bigger. Horizontal scaling adds machines.
Exercise
A server was load tested against an SLO of "p95 latency under 250 ms." It met the SLO at 1,200 requests per second and failed at 1,400. Expected peak traffic is 5,000 requests per second. You plan to run each server at no more than 60% of tested capacity and keep one spare server. How many servers do you need, and why do you plan for 60% instead of 100% of tested capacity?
Show answer
Usable capacity per server is 1,200 × 0.6 = 720 requests per second. 5,000 / 720 = 6.94, which rounds up to 7. Adding one spare gives 8 servers. You plan for 60% because latency rises steeply near full utilization, and the headroom absorbs traffic spikes, a failed server, and servers taken out for deployments. The tested figure is also only an estimate of production behavior.
Exercise
A team says, "Our database has a capacity of 10,000 queries per second." What two pieces of information are missing before this statement is useful for planning?
Show answer
The performance target at that rate, such as the latency the queries must meet (an SLO), and the type of queries behind the figure, meaning the read/write ratio and the cost of each query. Without the target, 10,000 could be a point where the system is already failing its users. Without the mix, a change in query types could change the real capacity.