Load Balancers: Scaling Out, Algorithms and High Availability
Vertical versus horizontal scaling, routing algorithms, health checks, stateless servers, layer 4 versus layer 7, and how the load balancer itself avoids being a single point of failure.
A load balancer sits in front of a group of servers and distributes incoming requests among them. It is what allows a website to keep working when hundreds of thousands of people arrive at once, for example when exam results are published.
These notes explain why adding servers is preferred over buying a bigger one, how a load balancer chooses a server, how it detects failures, and how the load balancer itself is made reliable.
Vertical versus horizontal scaling
Vertical scaling means making one machine more powerful: more CPU, more memory, faster disks. It is simple, because the application does not change. It has two limits. There is a largest machine you can buy, and the price rises steeply as you approach it. More importantly, one machine is a single point of failure: when it goes down, the whole service goes down.
Horizontal scaling means adding more machines and sharing the work among them. Capacity grows by adding servers, and the failure of one server removes only a fraction of capacity. The cost is that something must distribute requests among the servers and the application must be written so that any server can handle any request. That distributor is the load balancer.
What a load balancer does
- Distributes requests across a pool of servers so that no single server is overloaded.
- Detects unhealthy servers and stops sending them traffic.
- Allows servers to be added or removed without clients noticing, which enables scaling and zero-downtime deployments.
- Often terminates TLS, so that certificates and encryption are handled in one place.
- Presents a single address to clients and hides the internal topology.
DNS -> virtual IP
|
+---------------+---------------+
| Load balancer (active) | <-- heartbeat --> Load balancer (standby)
+---------------+---------------+
|
+----------------------+----------------------+
v v v
App server 1 App server 2 App server 3 (stateless)
| | |
+----------------------+----------------------+
v
Shared state: Redis (sessions), databaseRouting algorithms
- Round robin: requests go to servers in turn. Simple and fair when all servers are identical and requests cost about the same.
- Weighted round robin: servers with more capacity receive proportionally more requests.
- Least connections: a new request goes to the server with the fewest active connections. Better when request durations vary widely or connections are long-lived, such as WebSockets.
- Least response time: prefers servers that are currently answering fastest.
- IP hash or consistent hashing: the same client or key always maps to the same server. Useful for cache locality or when some session affinity is required.
Why round robin is not always enough
Round robin counts requests, not work. If some requests take 10 ms and others take 10 seconds, one server can receive several heavy requests in a row and become overloaded while its neighbours are idle. Least connections corrects this by looking at how busy each server actually is at the moment of the decision.
Health checks
A load balancer must not send traffic to a dead server. It runs health checks: every few seconds it sends a small request to each server, typically an HTTP request to a dedicated endpoint such as /health. If a server fails several consecutive checks, it is removed from rotation. When it passes several checks again, it is added back.
A good health endpoint verifies that the application can do real work, for example that it can reach its database, without being so strict that a brief dependency glitch removes every server at once. Passive health checking complements this: the load balancer watches real responses and ejects a server that returns many errors.
Sessions and stateless servers
Suppose a user logs in and the session is stored in the memory of server 1. The next request is routed to server 2, which has no record of the session, and the user appears logged out.
One fix is sticky sessions, where the load balancer always sends a given user to the same server. This works but has drawbacks: load becomes uneven, and when that server fails or is replaced, all its sessions are lost.
The better fix is to make servers stateless. Session data is moved to a shared store such as Redis, or carried by the client in a signed token. Any server can then handle any request. Stateless servers are the easiest thing in a system to scale: adding capacity means starting more identical instances, and removing one loses nothing.
Layer 4 versus layer 7
A layer 4 load balancer works at the transport layer. It sees IP addresses and TCP or UDP ports, and forwards packets without looking at their contents. It is very fast and can handle any protocol, but it cannot make decisions based on the request itself.
A layer 7 load balancer works at the application layer. It understands HTTP, so it can read the URL, headers and cookies. This allows content-based routing: requests to /api go to one group of servers, requests to /images go to another, and requests with a particular header go to a canary version. It can also terminate TLS, compress responses and retry failed requests. It does more work per request, so it is somewhat slower, which rarely matters in practice.
Large systems commonly use both: a layer 4 balancer at the edge to spread connections across a fleet of layer 7 balancers or API gateways.
The load balancer as a single point of failure
If every request passes through one load balancer and it fails, the entire service is unreachable, no matter how many healthy servers sit behind it. The load balancer must be redundant too.
The common arrangement is an active-standby pair. Two load balancers share a virtual IP address. The active one handles traffic, and the standby monitors it with heartbeats. If the heartbeats stop, the standby takes over the virtual IP within seconds. An active-active arrangement runs several load balancers at once, with DNS or network-level routing spreading traffic among them. Managed cloud load balancers provide this redundancy for you. The design goal has a name: no single point of failure.
Global load balancing
When a service runs in several regions, traffic must first be sent to the right region. This is done with DNS-based routing, which returns different addresses depending on the user's location or the health of each region, or with anycast, where the same IP address is announced from many locations and the network delivers each user to the nearest one. Within the region, the local load balancers described above take over.
Interview summary
- Scale horizontally, not vertically, and put a load balancer in front.
- Algorithm: round robin for uniform requests, least connections for uneven or long-lived ones.
- Health checks remove failed servers from rotation.
- Keep servers stateless; store sessions in a shared store.
- Run load balancers as an active-standby pair, and know the difference between layer 4 and layer 7.
Summary
A load balancer makes horizontal scaling possible by spreading requests over many servers and routing around failures. Choose the algorithm to match the workload, keep application servers stateless so any of them can serve any request, and make the load balancer itself redundant so that it does not become the single point of failure it was meant to remove.
Puneet Tiwari
Full Stack Developer
