Sub topic 9
Where scaling stops: hardware ceilings and Amdahl's law
This chapter assumes you know the difference between vertical scaling (a bigger machine) and horizontal scaling (more machines). It also assumes you can read basic Python. Both approaches eventually stop paying off. Knowing where and why lets you plan before you hit the wall.
Ceilings of vertical scaling
A single machine has a maximum size. Once you rent or buy the largest available server, you cannot scale up any further.
- Hardware limits. Sockets, cores, memory slots, and PCIe lanes are finite. A cloud provider's largest instance is the practical ceiling.
- Rising price per unit. Doubling a machine's capacity often costs more than double. Top-end parts are rare, so you pay a premium.
- Diminishing returns inside the machine. More cores do not help if the program uses one thread. Memory bandwidth, disk throughput, and the network card are also fixed.
- One failure domain. A bigger machine is still one machine. If it fails, everything on it fails.
Ceilings of horizontal scaling
Adding machines has no fixed upper bound, but other limits appear.
- Shared components. Every server still talks to the same database, cache, or message queue. That shared piece becomes the new bottleneck.
- Coordination cost. Servers that must agree on something, such as a lock or a leader, spend time communicating instead of working.
- Network overhead. More machines mean more traffic between them, and each hop adds latency.
- Operational load. Deployments, monitoring, and debugging all get harder as the fleet grows.
Both ceilings share a cause: some part of the work cannot be split up. Amdahl's law measures that.
Amdahl's law
Suppose a fraction p of a job can run in parallel and the rest, 1 - p, must run serially. With n workers, the best possible speedup is:
speedup = 1 / ((1 - p) + p / n)As n grows, p / n approaches zero. The speedup then approaches 1 / (1 - p). The serial fraction sets the ceiling, however many machines you add.
def amdahl_speedup(parallel_fraction, workers):
return 1 / ((1 - parallel_fraction) + parallel_fraction / workers)
print(round(amdahl_speedup(0.95, 16), 2)) # 9.14
print(round(amdahl_speedup(0.95, 1000), 2)) # 19.63Here 95% of the work is parallel and 5% is serial. Sixteen workers give about 9.14x, not 16x. One thousand workers give about 19.63x. The limit is 1 / 0.05 = 20x, and no number of workers can exceed it.
| Parallel fraction | Maximum speedup |
|---|---|
| 50% | 2x |
| 90% | 10x |
| 95% | 20x |
| 99% | 100x |
Each extra point of parallel fraction matters more as you approach 100%. Going from 90% to 99% raises the ceiling tenfold.
Finding the serial part in a real system
In a web service, the serial fraction is rarely a line of code. It is usually a shared resource that requests must pass through one at a time or in limited numbers:
- A single primary database that takes all writes.
- A global lock or a row that every request updates.
- A single load balancer, queue, or rate-limited external API.
- Work done before requests can be split, such as parsing a large input.
Adding application servers speeds up the parallel part only. If each request spends 5% of its time waiting on a single-writer database, you are in the 95% case above.
What to do when you approach a ceiling
- Measure first. Find the serial or shared portion with the methods from the bottleneck chapter.
- Shrink the serial part. Cache reads, batch writes, and remove unnecessary locks.
- Split the shared resource. Sharding a database turns one serial writer into several parallel ones.
- Accept the limit. If the remaining ceiling already exceeds your expected load, stop scaling and spend effort elsewhere.
Exercise
A job is 90% parallelizable. What is the maximum speedup with any number of workers, and what speedup do 10 workers give? Use amdahl_speedup(0.9, 10).
Show answer
The maximum is 1 / (1 - 0.9) = 10x. With 10 workers the speedup is 1 / (0.1 + 0.9 / 10) = 1 / 0.19, which is about 5.26x. Ten workers deliver roughly half of the theoretical maximum.
Exercise
Your web tier scales well, but every request writes one row to a single primary database, and that write is 10% of each request's time. Name two ways to raise the scaling ceiling.
Show answer
Reduce the serial share by batching or caching writes, or split the shared resource by sharding the database so writes run in parallel. Either change shrinks the serial fraction and raises the 10x ceiling.