Sub topic 1
Why one server is not enough: scaling and availability
Prerequisites
This chapter assumes you know how a client talks to a server over HTTP, what a TCP connection, an IP address and a port are, and how to edit a plain-text configuration file. No load balancer experience is needed.
The limits of a single server
A single server is simple. It has one address, one copy of the data, and one place to look when something breaks. It also has two hard limits.
- Capacity. CPU, memory, network bandwidth and the number of open connections are finite. When requests arrive faster than the server can handle them, they queue, latency rises, and eventually requests time out or are refused.
- Availability. Availability is the share of time a service answers requests correctly. One server is a single point of failure (SPOF), which is a component whose failure takes the whole service down. A crash, a bad deploy, a full disk, a reboot for patching or a network fault all cause an outage.
These limits need different fixes, and one technique helps with both: running several servers.
Vertical and horizontal scaling
Scaling means adding capacity. There are two directions.
| Row | Vertical scaling (scale up) | Horizontal scaling (scale out) |
|---|---|---|
| What changes | A bigger machine: more CPU, RAM, faster disks | More machines of the same size |
| Complexity | Low. The application does not change | Higher. Work must be spread across machines |
| Ceiling | The largest machine you can buy | Much higher, limited by design rather than hardware |
| Failure impact | Still one SPOF | One machine fails, the others continue |
| Downtime to grow | Often needs a restart | Usually none. New servers join the pool |
Vertical scaling is a reasonable first step. It stops working when the biggest machine is still too small, or when its price grows faster than its capacity. It also does nothing for availability.
Horizontal scaling fixes both limits, but it creates a new problem. Clients need one address to connect to, and something has to decide which of the many servers handles each request. That component is the load balancer, covered in the next chapter.
Availability in numbers
Availability is usually quoted as a percentage. The table shows how much downtime each level allows per year.
| Availability | Downtime per year |
|---|---|
| 99% | about 3.65 days |
| 99.9% | about 8.8 hours |
| 99.99% | about 53 minutes |
Redundancy raises availability only if failures are independent. If one server is up 99% of the time and fails independently of a second identical server, both are down together about 0.01 × 0.01 = 0.0001 of the time. The pair is available 99.99% of the time, as long as something routes traffic away from the failed server quickly.
The following script checks that arithmetic.
def pool_availability(single, servers):
"""Chance that at least one of `servers` independent servers is up."""
return 1 - (1 - single) ** servers
for n in (1, 2, 3):
print(n, f"{pool_availability(0.99, n):.6f}")It prints 0.990000, 0.999900 and 0.999999 for one, two and three servers.
What horizontal scaling requires
Adding servers does not help unless the design allows it.
- Routing. Requests must be spread across the servers, and requests to failed servers must be avoided. This is the job of a load balancer.
- Stateless servers where possible. If a server keeps a user's session in its own memory, that user must always return to that server, and a failure loses the session. Moving state to a shared store such as a database or cache lets any server handle any request. Later chapters on session persistence cover the cases where this is not possible.
- Shared data. All servers must see the same data. A single database can become the next bottleneck, so scaling the application tier does not remove the need to plan the data tier.
- Identical configuration. Servers in a pool should run the same code and settings, so a request gives the same result wherever it lands.
The shape of the solution
The diagram shows the change from one server to a pool.
Clients still see one address. Behind it, capacity grows by adding servers, and a failed server can be skipped. The load balancer itself is now a component that must not fail, which a later chapter on high availability addresses.
Common mistakes
- Treating a bigger server as high availability. It raises capacity but is still one SPOF.
- Adding servers without a way to detect failure. Traffic keeps going to dead servers, and some requests fail. Health checks solve this.
- Ignoring shared bottlenecks. Ten application servers all calling one small database simply move the limit.
- Keeping local state. Sessions or uploaded files on one server's disk break when requests spread out.
Exercise
A service runs on one server at 99% availability. You add a second identical server that fails independently, and traffic is routed only to working servers. What is the new availability, and name one reason the real figure could be lower?
Show answer
The new availability is 1 - (0.01 × 0.01) = 99.99%. The real figure could be lower because failures are often shared (for example a bad deploy, a shared database or a common power feed), or because failure detection and rerouting are not instant.
Exercise
Why does vertical scaling not remove the single point of failure?
Show answer
A larger machine is still one machine. If it crashes, is patched or loses network access, the whole service is down.