The traffic police that shares requests between many servers.
A load balancer sits in front of many servers. It takes every incoming request and sends it to one healthy server. This way no single server gets too busy.
A load balancer (LB) is a server, a device or a cloud service that receives traffic and shares it among a group of servers. That group is called a server pool (or backend pool).
Almost every website or app with more than one server has a load balancer.
These numbers come from the OSI model. A Layer 4 LB looks only at IP addresses and ports. A Layer 7 LB reads the actual request, like the URL, headers and cookies.
| Point | Layer 4 LB | Layer 7 LB |
|---|---|---|
| Decides using | IP address and port | URL path, host, headers, cookies |
| Speed | Very fast, simple | Slower, does more work |
| Smart routing | No. Cannot tell /images from /login | Yes. /api to one pool, /images to another |
| Can do TLS (HTTPS) offload | Usually no | Yes |
| Good for | Raw TCP/UDP, games, databases, huge traffic | Websites, APIs, microservices |
| Examples | AWS NLB, HAProxy (tcp mode) | AWS ALB, NGINX, HAProxy (http mode) |
The LB sends a small test request to every server, every few seconds. For example GET /health. If a server fails a few checks in a row, the LB stops sending users to it. When it passes again, the LB adds it back.
Normally each request can go to any server. With sticky sessions (session affinity), the LB sends the same user to the same server every time. It uses a cookie or the client IP.
If we have only one load balancer and it dies, the whole site goes down, even if all servers are fine. So we use a pair.
upstream my_app {
server 10.0.0.1:8080;
server 10.0.0.2:8080;
server 10.0.0.3:8080;
}
server {
listen 80;
location / {
proxy_pass http://my_app; # round robin by default
}
}| Term | Meaning in easy words |
|---|---|
| Server pool / backend | The group of servers behind the load balancer |
| Health check | A small test request that tells the LB if a server is OK |
| Layer 4 / Layer 7 | The LB decides by IP+port (L4), or by the full request such as URL and headers (L7) |
| Sticky session | Same user always goes to the same server |
| TLS termination | The LB decrypts HTTPS, so servers do less work |
| Failover | Switching to a standby (server or LB) when the main one fails |
| Active-passive | One LB works, the other waits as a backup |
| Virtual IP (VIP) | A shared address that moves to the backup LB when needed |
| Connection draining | Before removing a server, let its current requests finish |
| Reverse proxy | A server in front of other servers. A load balancer is a reverse proxy that also shares load |
| Choice | We gain | We lose |
|---|---|---|
| L4 instead of L7 | Speed, simplicity | Smart routing by URL |
| L7 instead of L4 | Smart routing, HTTPS handling | A bit more delay and cost |
| Hardware LB | Very high speed | High cost, hard to scale |
| Software / cloud LB | Cheap, flexible | You tune or pay per use |
| Sticky sessions | Works with stateful apps | Uneven load, harder failover |
| Active-active | No idle LB | More complex setup |
X-Forwarded-For header instead./health.