Chapter 10: Load Balancing
A load balancer distributes incoming requests across multiple servers, ensuring no single server is overwhelmed.
L4 vs L7 Load Balancing
| Layer | Operates On | Can See | Speed | Examples |
|---|---|---|---|---|
| L4 (Transport) | TCP/UDP packets | IP + port only | Very fast | AWS NLB, HAProxy (TCP mode) |
| L7 (Application) | HTTP requests | URL, headers, cookies, body | Slower but smarter | nginx, Envoy, AWS ALB |
Algorithms
| Algorithm | How It Works | Best For |
|---|---|---|
| Round Robin | Rotate through servers sequentially | Equal-capacity servers |
| Weighted Round Robin | More requests to higher-capacity servers | Mixed server sizes |
| Least Connections | Send to server with fewest active connections | Varying request durations |
| IP Hash | Hash client IP → consistent server | Session affinity (sticky sessions) |
| Consistent Hashing | Hash-ring based distribution | Cache servers, minimal redistribution |
Health Checks
// Load balancer periodically checks each server:
GET /health → 200 OK (server is healthy, keep sending traffic)
GET /health → 503 or timeout (server is unhealthy, remove from pool)
// When server recovers → add back to pool
High Availability for the Load Balancer Itself
Active-Passive LB pair:
┌──────────┐ ┌──────────┐
│ LB (A) │◄───►│ LB (B) │ ← heartbeat
│ ACTIVE │ │ STANDBY │
└────┬─────┘ └──────────┘
│ ↑
▼ (takes over if A dies)
[App Servers]
Key Takeaways
- L4: fast, simple, TCP-level. L7: smart, can route by URL/header.
- Round robin for simple cases, least connections for varying loads
- Health checks automatically remove failed servers
- LB itself needs redundancy (active-passive pair)
- In Kubernetes: Services + Ingress act as built-in load balancers