Sub topic 5
Horizontal scaling: more machines
This chapter assumes you know how a client talks to a web server, what CPU, memory, disk, and network limits look like, and what a database does. You should also know what a bottleneck is from the earlier chapters.
What horizontal scaling changes
Horizontal scaling (also called scaling out) adds more machines that do the same job. Vertical scaling makes one machine bigger. Horizontal scaling changes the count of machines and leaves the size of each one alone.
Suppose one web server handles 200 requests per second before its CPU is saturated. Three identical servers can handle about 600 requests per second, if the work is spread evenly and nothing else becomes the limit.
What the application must provide
Adding machines does nothing by itself. Two things have to be in place.
- A way to distribute requests. Clients need one address, and something must pick a server for each request. This is the job of a load balancer, covered in the next chapter.
- Interchangeable servers. Any server must be able to handle any request. This usually means the servers keep no per-user data in local memory or on local disk. That design is called stateless, and it also has its own chapter.
The simplest distribution rule is round robin, which sends each request to the next server in a list and wraps around at the end. This script shows the idea:
from itertools import cycle
servers = ["app-1", "app-2", "app-3"]
pool = cycle(servers)
for request_id in range(5):
print(request_id, "->", next(pool))The output assigns requests 0 to 4 to app-1, app-2, app-3, app-1, app-2. A real load balancer also checks which servers are healthy and skips failed ones.
Benefits
- Capacity grows in small steps. You add one server at a time, so you pay for roughly what you need.
- Failure of one machine is survivable. With three servers, losing one leaves two serving traffic, at reduced capacity.
- No single machine has to be the biggest available. Many mid-sized machines are usually cheaper than one very large one.
- Maintenance is easier. You can take one server out, update it, and put it back while the others keep working.
Costs and common errors
Horizontal scaling moves complexity from the hardware into the design.
- Local state breaks. If a user logs in on app-1 and the session is stored in app-1's memory, the next request may land on app-2, and the user appears logged out. Fix this by moving sessions to a shared store or into a signed token.
- The database becomes the new bottleneck. Ten application servers all query the same database. Scaling the web tier without scaling the data tier only moves the limit.
- Local files disappear. A file uploaded to one server's disk is not visible on the others. Use shared object storage instead.
- Scheduled jobs run many times. A cron job that exists on every server runs once per server. Run it on one node or use a lock.
- Scaling something that is not the bottleneck. If a slow query causes the latency, extra web servers add cost and change nothing. Measure first.
- Debugging is harder. Logs and metrics are spread over many machines, so you need central logging and per-server metrics.
Limits
The speedup from more machines is not linear forever. Any part of the work that cannot be split up, such as a single database writer, caps the gain. Amdahl's law quantifies this, and a later chapter covers it in detail.
Checking your understanding
Exercise
A site runs on one server. Sessions are kept in that server's memory. You add a second server behind a round-robin load balancer, and users start reporting that they are randomly logged out. What is the cause, and what is one way to fix it?
Show answer
Requests from the same user now alternate between servers, but each server holds only the sessions it created. A request that reaches the other server finds no session and treats the user as logged out. One fix is to store sessions in a shared store such as Redis or a database. Another is to use signed tokens that the client sends with each request, so any server can validate them.
Exercise
Your three application servers each sit at 30% CPU, but response times are high and the database CPU is at 98%. Would adding a fourth application server help? Why or why not?
Show answer
No. The bottleneck is the database, not the application tier. A fourth application server would send even more queries to the saturated database and could make latency worse. The fix belongs in the data tier, for example by adding indexes, caching, read replicas, or a larger database machine.