Load Balancing
1 min read
Load Balancing
A load balancer (LB) sits in front of your servers and distributes incoming traffic across them. It's the first thing you add when one server isn't enough.
┌── Server A
Client ──▶ LB ─┼── Server B
└── Server C
Why you need one
- Scale — spread load across many machines.
- Availability — route around a dead server (health checks).
- Zero-downtime deploys — drain one server at a time.
Layer 4 vs Layer 7
| L4 (transport) | L7 (application) | |
|---|---|---|
| Sees | IP + port | Full HTTP request |
| Routing | By connection | By URL / header / cookie |
| Speed | Faster | Slightly slower |
Common algorithms
- Round robin — each server in turn.
- Least connections — send to the least-busy server.
- Weighted — bigger servers get more traffic.
- IP hash — same client → same server (sticky sessions).
The catch: the LB itself is a single point of failure
Run at least two load balancers (active-passive or active-active) with a floating IP or DNS failover. Cloud LBs handle this for you.
Stateless services scale best. Keep session state in a shared cache (Redis), not in server memory, so any server can handle any request.