Sub topic 11
How many servers do you need
This chapter assumes you can convert daily active users into queries per second (QPS) and tell peak load from average load. It also assumes you know what a load balancer does. Here you turn a peak QPS figure into a server count.
The basic formula
A server count comes from three numbers:
- Peak QPS the system must handle.
- Capacity of one server, in QPS, at a safe load.
- Redundancy, so that losing a server does not cause an outage.
import math
peak_qps = 2315 # from the traffic chapter
qps_per_server = 500 # assumed, measured or guessed
target_utilization = 0.6 # leave 40% headroom
usable_qps = qps_per_server * target_utilization
servers_for_load = math.ceil(peak_qps / usable_qps)
servers_total = servers_for_load + 1 # N+1 redundancy
print(servers_for_load) # 8
print(servers_total) # 9Always round up. Seven and a half servers means eight.
Where per-server capacity comes from
You rarely know this number exactly. Pick a defensible one and state it as an assumption. Latency numbers help you reason about it.
If each request takes 20 ms of work on one worker thread, that thread serves about 1000 / 20 = 50 requests per second. A server with 10 such workers serves about 500 QPS. This is why latency matters: a request that waits on a 10 ms disk read or a 500 microsecond network round trip holds a worker for that time.
The type of work changes the answer a lot:
| Workload | Typical bottleneck | Rough capacity per server |
|---|---|---|
| Cached reads, simple API | CPU, network | Thousands of QPS |
| Business logic with database calls | Worker threads, DB connections | Hundreds of QPS |
| Image or video processing | CPU | Single digits to tens of QPS |
Treat these as starting points. A load test replaces them with real numbers.
Headroom and redundancy
Do not plan to run servers at 100% of capacity. Latency rises sharply as utilization approaches the limit, because requests start to queue. A common target is 50 to 70 percent utilization at peak.
Redundancy is separate from headroom. With N+1, you add one spare server so that one failure leaves enough capacity. For small fleets this is a large fraction of the cost. For large fleets, plan for the loss of a whole failure zone. If servers are spread over 3 zones, losing one zone removes one third of capacity, so the remaining two zones must carry the full peak.
import math
servers_for_load = 8
zones = 3
# Survive the loss of one zone: the other (zones - 1) must carry everything.
per_zone = math.ceil(servers_for_load / (zones - 1))
total = per_zone * zones
print(per_zone) # 4
print(total) # 12That is 12 servers, not 9. The cost of surviving a zone failure is 50 percent more machines in this case.
Other limits to check
CPU is not the only limit. Check each of these against the same peak figures:
- Network: peak bandwidth divided by the server's network link. A 10 Gbps link carries about 1.25 GB/s.
- Memory: the cache or working set must fit in RAM on each server.
- Connections: databases accept a limited number of connections, so many app servers can overload the database.
The server count is the largest number any one of these limits requires.
Common mistakes
- Using average QPS instead of peak QPS.
- Forgetting to apply headroom, which gives a plan that fails at peak.
- Counting redundancy once for the whole fleet when failures happen by zone.
- Presenting a capacity guess as fact. State it as an assumption and say what would change it.
Exercise
Peak load is 6,000 QPS. One server handles 400 QPS at full load, and you want 50% utilization at peak. You also want N+1 redundancy. How many servers do you need?
Show answer
Usable capacity per server is 400 x 0.5 = 200 QPS. Servers for load: 6000 / 200 = 30. Add one spare for N+1, which gives 31 servers.
Exercise
Your 30 servers for load are spread over 3 zones. How many total servers do you need to survive the loss of one zone?
Show answer
The two remaining zones must carry all 30 servers' worth of load, so each zone needs 30 / 2 = 15 servers. Total is 15 x 3 = 45 servers.