Load Balancing
A load balancer sits in front of a pool of identical servers and decides, for every incoming request, which one handles it. The goal is simple to state and easy to get wrong in practice: keep every server about equally busy, keep serving requests when one server dies, and never let clients notice any of this happening.
Why one server is never enough
A single server has a hard ceiling — CPU cores, memory, open file descriptors, network bandwidth. Once traffic passes that ceiling, requests queue up and latency climbs, no matter how well-written the code is. The usual fix is to run several identical copies of the service and put something in front that spreads requests across them. That something is the load balancer, and it turns a scaling problem ("make this one machine faster") into a much easier one ("add another machine").
Picking which server gets the request
The balancing algorithm is the load balancer's core decision, and different ones trade off simplicity against how evenly they actually spread load:
- Round robin hands requests to servers in a fixed rotation. It is trivial to implement and works well when every request costs roughly the same amount of work.
- Least connections sends the next request to whichever server currently has the fewest open connections — a better fit when request costs vary widely, since a server stuck on an expensive request stops receiving new ones.
- Consistent hashing routes based on a hash of some request property (often the client's IP or a session ID), so the same client lands on the same server across requests — useful when a server holds warm, per-client state like an in-memory cache.
A minimal round-robin picker looks like this:
class RoundRobin:
def __init__(self, servers):
self.servers = servers
self.next = 0
def pick(self):
server = self.servers[self.next]
self.next = (self.next + 1) % len(self.servers)
return server
Health checks: not routing to a dead server
None of this works if the load balancer keeps sending requests to a server
that has crashed or is failing. Load balancers periodically probe each
backend — an HTTP request to a /healthz endpoint is the usual shape — and
pull any server that stops responding correctly out of rotation. This is
what actually delivers the availability benefit: clients keep getting
served through a server failure because the load balancer stopped routing
to it within a few seconds, not because the failure never happened.
Layer 4 vs layer 7
Load balancers operate at one of two levels. A layer 4 balancer only looks at TCP/UDP connection info (source/destination IP and port) and forwards packets without understanding the application protocol — fast, but blind to anything inside the request. A layer 7 balancer terminates and reads HTTP itself, so it can route based on the request path or headers, handle TLS termination centrally, and retry a failed request against a different backend. Most public-facing web services use a layer 7 balancer precisely because that visibility enables smarter routing and better failure handling, at the cost of doing more work per request.
The load balancer itself eventually becomes the next bottleneck and single point of failure — which is why production setups usually run at least two, with a mechanism like DNS or a floating IP in front deciding which one is active.