Sub topic 6
Spreading requests with a load balancer
This chapter assumes you know what a client, a web server, and a health check are. It also assumes you have read the previous chapter on horizontal scaling. Adding servers only helps if something sends each request to one of them. That component is the load balancer.
What a load balancer does
A load balancer is a service that accepts requests on one address and forwards each request to one server in a pool. Clients connect to the load balancer and never learn the addresses of the servers behind it. This gives you three things:
- Shared load. No single server takes all the traffic.
- A stable entry point. You can add or remove servers without changing what clients connect to.
- Failure handling. The balancer stops sending traffic to servers that fail.
Choosing a server: balancing algorithms
The algorithm decides which server gets the next request. These are the common ones:
| Algorithm | How it picks | Best when |
|---|---|---|
| Round robin | Next server in a fixed order, then wrap around | Requests cost about the same |
| Weighted round robin | Round robin, but bigger servers appear more often | Servers have different capacity |
| Least connections | Server with the fewest open connections | Requests vary in duration |
| IP hash | Hash of the client address picks the server | The same client should reach the same server |
Round robin is the simplest. This Python snippet shows the idea in miniature:
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))It prints requests 0 to 4 assigned to app-1, app-2, app-3, app-1, app-2. The pool wraps around when it reaches the end.
Round robin ignores how busy each server is. If a few requests are very slow, one server can pile up work while another sits idle. Least connections handles that case better, because it uses live information.
Layer 4 and layer 7
Load balancers work at different levels of the network stack:
- Layer 4 (transport). The balancer looks only at IP addresses and ports. It is fast and simple, and it works for any TCP or UDP protocol.
- Layer 7 (application). The balancer reads the HTTP request. It can route by URL path or header, terminate TLS, and add headers. It costs more CPU per request.
Use layer 7 when you need content-based routing, for example sending /api to one pool and /static to another. Use layer 4 when you need raw speed or a non-HTTP protocol.
Health checks
A balancer must know which servers are alive. It sends a probe, usually an HTTP request to a path such as /healthz, at a fixed interval. After a set number of failed probes, it removes the server from rotation. After enough successful probes, it adds the server back.
Common errors
- Sticky sessions hiding a state problem. Session affinity pins a client to one server so that server's memory holds the session. It works, but it makes the load uneven. When that server dies, the sessions on it are lost. The next chapter covers stateless design, which removes the need for affinity.
- The balancer as a single point of failure. One balancer in front of many servers moves the risk instead of removing it. Production setups run at least two balancers, or use a managed service that does this for you.
- Wrong client IP in logs. Servers see the balancer's address, not the client's. Read the client address from a header such as
X-Forwarded-For, and trust it only when it comes from your own balancer. - Connection draining skipped. If you remove a server abruptly, in-flight requests fail. Enable draining so the server finishes current requests before it leaves the pool.
- Slow detection of failure. A long probe interval combined with a high failure threshold means clients keep reaching a dead server for many seconds. Tune both values to your tolerance for errors.
Where the balancer fits in scaling
The balancer is what makes horizontal scaling usable. You add a server to the pool and traffic starts to reach it. Later chapters build on this. Autoscaling adds and removes servers automatically, and the balancer picks up each change through its health checks and registration.
Exercise
Your service has three servers. Most requests take 20 ms, but a few report requests take 10 seconds. Under round robin, one server often has a long queue while the others are idle. Which algorithm would you try, and why?
Show answer
Least connections. Round robin ignores current load, so slow requests can pile up on one server by chance. Least connections sends each new request to the server with the fewest open connections, so a server busy with long requests receives less new work.