Sub topic 11
How many servers do you need
Prerequisites
This chapter assumes you can already do these things from earlier chapters:
- Convert daily active users into average and peak queries per second (QPS).
- Use the 86,400-second day and round numbers to keep the math simple.
- Read the latency orders of magnitude for memory, SSD, network and disk.
- Read simple Python code.
You also need a basic idea of what a load balancer does: it spreads requests across a group of identical servers.
Why server count is an estimate too
Once you have a peak QPS figure, the next question is how many machines can serve it. You will not get an exact answer. Real capacity depends on the code, the hardware, the data and the traffic mix. What you can do is get the right order of magnitude: 3 servers, 30 servers, or 300 servers. Each of those leads to a different design.
The basic formula is short:
servers = peak_qps / qps_per_server, then add headroom and redundancyMost of the work goes into the two inputs: where qps_per_server comes from, and how much extra capacity to add.
Step 1: Estimate per-server capacity
A server's capacity is limited by whichever resource runs out first. For a typical request, the candidates are CPU, memory, network and disk I/O. Pick the one you expect to be the bottleneck and estimate from it.
CPU-bound work
If each request needs a fixed amount of CPU time, the capacity of one core is the number of requests it can finish in a second.
- A request that takes 10 ms of CPU time lets one core serve about 100 requests per second.
- A server with 8 cores can then serve about 800 requests per second.
This is the most common estimate for application servers. Use the CPU time per request, not the total latency. A request that takes 100 ms end to end may spend 90 ms waiting on a database and only 10 ms using the CPU.
I/O-bound work
When requests mostly wait on other systems, a single thread or process can be idle for most of the request. Servers that handle many requests at once, using threads or an event loop, can then serve far more requests than they have cores. In that case the limit is often the number of concurrent connections, memory per connection, or the capacity of the downstream database.
Little's law gives a useful link between these quantities:
concurrent_requests = qps * latency_in_secondsIf a server handles 500 QPS and each request takes 200 ms, it holds about 100 requests in flight at any moment. If each in-flight request needs 1 MB of memory, that is about 100 MB. Check that the number fits the server.
Typical starting figures
When you have no measurement, state a figure as an assumption and say so. These are common starting points for a modern general-purpose server. They are assumptions, not facts, and a benchmark of your own code should replace them.
| Workload | Rough capacity per server | Likely bottleneck |
|---|---|---|
| Simple web or API server | 1,000 to 10,000 QPS | CPU |
| Server doing heavy work per request | 100 to 1,000 QPS | CPU |
| In-memory cache node | 50,000 to 100,000+ QPS | Network or CPU |
| Relational database node | 1,000 to 10,000 QPS | Disk I/O or locks |
The spread inside each row is large on purpose. Writes are usually more expensive than reads, and complex queries are more expensive than key lookups.
Step 2: Add headroom
You should not plan to run servers at 100% of capacity. At high utilization, queues build up and latency rises sharply. A server that is 95% busy handles bursts badly, and one slow request can delay many others.
A common rule is to plan for 50% to 70% utilization at peak. Headroom also covers:
- Traffic spikes above your peak estimate.
- Deployments, when some servers are out of rotation.
- Garbage collection pauses, background jobs and noisy neighbors.
- Errors in your own assumptions.
If you choose a target utilization of 60%, the usable capacity per server is 60% of its measured maximum.
Step 3: Add redundancy
Capacity is not enough. If your load needs exactly 10 servers and one fails, you are now short. Add spare machines so the service survives losing some of them. This is often called N+1 (one spare) or N+2 planning.
Redundancy should reflect your failure domains. If all servers sit in one data center or availability zone, one outage removes all of them. With three zones, a common approach is to size the fleet so that losing one zone still leaves enough capacity. That means each zone must carry about half of the peak load, not a third. So the total fleet is about 1.5 times the minimum.
The link to availability is direct. The more servers you have and the more places they run, the more likely the service stays up when parts of it fail. You will turn an availability target into allowed downtime in its own chapter. Here, treat it as the reason spare capacity is not waste.
Putting it into code
This script applies the three steps. It estimates per-server capacity from CPU time, applies a utilization target, and then adds spare servers for failures.
import math
peak_qps = 2315 # from the traffic chapter
cpu_ms_per_request = 10 # assumed CPU time per request
cores_per_server = 8 # assumed
target_utilization = 0.6 # plan to run at 60% at peak
spare_servers = 2 # N+2 redundancy
qps_per_core = 1000 / cpu_ms_per_request
max_qps_per_server = qps_per_core * cores_per_server
usable_qps_per_server = max_qps_per_server * target_utilization
servers_for_load = math.ceil(peak_qps / usable_qps_per_server)
total_servers = servers_for_load + spare_servers
print(max_qps_per_server) # 800.0
print(usable_qps_per_server) # 480.0
print(servers_for_load) # 5
print(total_servers) # 7Walk through the numbers:
- One core handles 1,000 ms / 10 ms = 100 requests per second.
- Eight cores handle 800 requests per second at the maximum.
- At 60% utilization, the usable figure is 480 QPS per server.
- 2,315 / 480 is about 4.8, which rounds up to 5.
- Two spares bring the fleet to 7 servers.
Always round up with ceil. A fleet of 4.8 servers does not exist, and rounding down leaves you under capacity.
Notice how much the answer depends on the assumptions. If CPU time were 50 ms instead of 10 ms, the load would need 24 servers. A factor of 5 in one input produces a factor of about 5 in the result. This is why you state the assumption and say what you would measure to confirm it.
Estimating by other resources
CPU is not the only limit. Check at least one other resource before you commit to a number.
Network
A server with a 10 Gbps network card moves at most about 1.25 GB per second. Real throughput is lower, so assume about half. If each response is 100 KB and the server can sustain 600 MB per second, it can send about 6,000 responses per second. If that is below the CPU estimate, network is the real limit.
Memory
If each server holds a share of a cache or an in-memory index, divide the total memory you need by the usable memory per server. For example, a 300 GB cache on servers with 64 GB of usable memory needs at least 5 servers. Add replicas if you want the cache to survive a node failure.
Storage
Storage nodes are sized by data volume as well as by request rate. From the storage chapter, 91 TB over five years on machines with 10 TB of usable disk needs at least 10 machines. Replication multiplies that figure. With three copies, you need about 30. Take the larger of the storage-based count and the traffic-based count.
The general rule: compute the server count for each resource, then use the largest.
Common errors
- Using total latency as CPU time. Waiting on a database does not use your CPU. Separate compute time from wait time.
- Planning for average QPS. Servers must handle the peak. Use the peak figure from the earlier chapter.
- Forgetting headroom. A fleet sized at 100% utilization fails under any surprise.
- Rounding down. Always round the server count up.
- Ignoring the database. Adding application servers does not help if the database is the bottleneck. A fleet of 7 application servers sending 2,315 QPS of queries may overload a single database node.
- Ignoring write cost. If writes are a large share of traffic, the capacity of a single primary database often decides the design.
- Treating the table figures as facts. They are starting guesses. Say so, and replace them with measurements when you can.
Summary of the method
- Start with peak QPS.
- Estimate per-server capacity from the bottleneck resource, usually CPU time per request.
- Apply a utilization target of about 50% to 70%.
- Divide and round up.
- Add spare servers, and consider failure domains such as zones.
- Repeat for network, memory and storage, and keep the largest count.
- Write down every assumption.
Exercise
A service has a peak of 6,000 QPS. Each request uses 20 ms of CPU time. Each server has 16 cores and you plan for 50% utilization at peak. You also want 2 spare servers. How many servers do you need in total?
Show answer
One core handles 1000 / 20 = 50 requests per second. Sixteen cores handle 800 QPS at the maximum. At 50% utilization, the usable figure is 400 QPS per server. 6,000 / 400 = 15 servers for the load. Adding 2 spares gives 17 servers in total.
Exercise
Your fleet is spread evenly across 3 availability zones, and you want the service to keep serving peak load if one whole zone fails. The load alone needs 12 servers. What is the smallest total fleet size, and how many servers go in each zone?
Show answer
If one zone fails, the remaining two zones must hold all 12 servers' worth of capacity, so each zone needs 6. Three zones of 6 servers give a total of 18. This is 1.5 times the load-only minimum of 12.
Exercise
A request takes 250 ms end to end. Only 5 ms of that is CPU time, and the rest is waiting on a database. A server has 4 cores. Is it more reasonable to estimate capacity as 4 / 0.25 = 16 QPS or as 4 / 0.005 = 800 QPS? What other limit should you check?
Show answer
The CPU-based figure of 800 QPS is the better starting estimate, because only CPU time occupies a core. The 16 QPS figure wrongly treats waiting time as work. At 800 QPS and 250 ms latency, Little's law gives 800 * 0.25 = 200 requests in flight, so check that the server has enough memory and connections to hold them, and that the database can handle the query load.