Sub topic 3
Finding the bottleneck before you scale
This chapter assumes you know what latency, throughput, and utilization mean, and that you can name the four basic server resources: CPU, memory, disk, and network. If those terms are new, read the previous chapter first.
Why measure first
Scaling costs money and adds complexity. If you add capacity to the wrong part of a system, you pay for it and the slowdown stays. A bottleneck is the one resource or component that limits the throughput of the whole system. Raise its capacity and the system speeds up. Raise anything else and nothing changes.
Suppose a web page takes 3 seconds to load. Doubling the web servers does not help if every request waits on one slow database query. The extra servers just queue up behind the same query.
The measure, locate, change loop
Use the same loop each time:
- Measure latency, throughput, and utilization under realistic load.
- Find the resource or component that is saturated first.
- Change that one thing, either by scaling it or by fixing it.
- Measure again to confirm the change helped.
The loop repeats because removing one bottleneck exposes the next one. A system always has a bottleneck. The goal is to have it be somewhere you can afford.
Reading the signals
Each resource shows a typical pattern when it is the limit.
| Resource | Signs it is the bottleneck | Common cause |
|---|---|---|
| CPU | CPU utilization near 100%, high run queue, latency grows with load | Heavy computation, inefficient code |
| Memory | Swapping, out-of-memory kills, frequent garbage collection | Large caches, memory leaks |
| Disk | High I/O wait, long disk queue, CPU idle while requests stall | Slow queries, unindexed scans, heavy logging |
| Network | Bandwidth near the link limit, dropped packets, retransmits | Large responses, chatty services |
| Downstream service | Threads waiting on a call, low CPU everywhere | Slow database or third-party API |
Low CPU does not mean the system is healthy. If CPU is at 20% and latency is high, the server is probably waiting on something else, such as disk or the database.
Utilization and queueing
Latency does not rise in a straight line as utilization rises. It stays flat for a while, then climbs steeply as a resource approaches full use. Requests start to queue, and each waits for the ones ahead of it.
For this reason, a resource at 60% utilization is usually fine, while one at 95% is already causing long waits. Look at latency percentiles, not only the average. The p99 latency is the time that 99% of requests beat. It shows the slowest requests, and those are usually the first to suffer when a resource saturates.
A worked example
This script simulates a service with two steps per request. Step one uses CPU for 20 ms. Step two waits on a database for 80 ms. It estimates the maximum throughput of one worker, then shows what happens when you speed up each step.
def max_throughput(cpu_ms, db_ms, cpu_workers, db_connections):
# Requests per second each stage can handle.
cpu_rate = cpu_workers * 1000 / cpu_ms
db_rate = db_connections * 1000 / db_ms
# The slower stage limits the whole system.
return min(cpu_rate, db_rate), cpu_rate, db_rate
for label, cpu_workers, db_connections in [
("baseline", 2, 4),
("double CPU workers", 4, 4),
("double DB connections", 2, 8),
]:
total, cpu_rate, db_rate = max_throughput(20, 80, cpu_workers, db_connections)
print(f"{label}: total={total:.0f}/s cpu={cpu_rate:.0f}/s db={db_rate:.0f}/s")The output is:
baseline: total=50/s cpu=100/s db=50/s
double CPU workers: total=50/s cpu=200/s db=50/s
double DB connections: total=100/s cpu=100/s db=100/sThe database stage is the bottleneck at 50 requests per second. Doubling CPU workers changes nothing. Doubling database connections doubles throughput. After that, CPU and database tie at 100 per second, so the next bottleneck is both of them.
Common errors
- Scaling on a guess. Adding servers because the site "feels slow" without looking at metrics.
- Watching averages only. Average latency can look fine while p99 is poor.
- Testing with unrealistic load. A test that skips the database or uses tiny data hides the real limit.
- Fixing the wrong layer. Buying a bigger machine when one missing index is the cause.
- Stopping after one fix. The next bottleneck appears as soon as the first is gone.
Exercise
Exercise
A service runs on one server. During a load test, CPU stays at 25%, memory is steady, and p99 latency rises sharply as traffic grows. Threads are mostly waiting on database calls. Would adding a second web server fix this? What would you check first?
Show answer
Probably not. The web server is not saturated, since CPU is only 25%, so a second server would add idle capacity while requests still wait on the same database. Check the database first: slow queries, missing indexes, connection pool size, and database CPU, disk I/O, and lock waits. Fix or scale the stage that is saturated, then measure again.