Load balancing distributes traffic for higher availability and performance.
L4 vs L7¶
- L4 — IP + port, fast, content-unaware
- L7 — HTTP headers, URL, cookies, smarter
Algorithms¶
- Round Robin
- Least Connections
- IP Hash (sticky)
- Weighted
- Random
Examples¶
Nginx L4¶
stream { upstream db { server 10.0.1.1:5432; server 10.0.1.2:5432; } server { listen 5432; proxy_pass db; } }
Nginx L7¶
upstream api { server 10.0.1.1:3000; server 10.0.1.2:3000; } server { location /api/ { proxy_pass http://api; } }
Tools¶
- HAProxy — L4/L7, most popular OSS
- Nginx — L7, L4 via stream
- Traefik — cloud-native, auto-discovery
- Cloud LB — AWS ALB/NLB, Azure, GCP
L4 for Speed, L7 for Intelligence¶
L4 for DB/TCP. L7 for HTTP routing and SSL termination.
load balancingl4l7