A load balancer distributes incoming requests across several servers that do the same job. It checks their health and stops sending traffic to failed ones, so the service stays up and can grow by adding servers. Examples include AWS Elastic Load Balancing, HAProxy, nginx and Cloudflare Load Balancing.
How it works
Layer 4 vs layer 7
- Layer 4 (transport): forwards TCP or UDP connections by address and port, without reading them. Very fast, protocol-agnostic.
- Layer 7 (application): understands HTTP, so it can route by path or header, terminate TLS, add cookies for stickiness and retry failed requests. It's a reverse proxy.
Choosing a server
- Round robin: each server in turn.
- Least connections: the server with the fewest active connections.
- Hashing: by client IP or a key, so the same client keeps landing on the same server.
- Weighted versions of all of these for servers of different sizes.
Health checks
The balancer regularly requests a health endpoint (like /healthz) from each server and removes ones that fail, adding them back when they recover.
DNS and global balancing
DNS can balance too, by returning different IPs per lookup or per region, and anycast routes users to the nearest data centre. Large sites combine all three.
Examples
Many big sites return several addresses for one name, a simple form of DNS load balancing:
$ dig +short www.google.com
142.251.153.119
Ask again from another city, or a few minutes later, and you'll often get a different address: DNS is spreading users across many front-end servers.
On a Mac
dig +short a busy domain several times to see DNS-level balancing. curl -sI repeated against a site sometimes shows different backend IDs in headers like x-served-by.
See it in the app
LightAtom's connections view shows an app's connections to the same service landing on different IP addresses: load balancing at work.
Frequently asked questions
What does a load balancer do?
It spreads incoming traffic across multiple servers and routes around failed ones, improving capacity and availability.
What is the difference between a load balancer and a reverse proxy?
A layer 7 load balancer is a reverse proxy with several backends. A reverse proxy may front a single server and focus on TLS, caching or routing.
What is a sticky session?
Keeping a client on the same backend server for the duration of its session, usually with a cookie or IP hash.
Is DNS round robin a load balancer?
A crude one: it spreads clients across addresses but can't detect failed servers by itself, and caching makes it uneven.