Load Balancer Guide
Algorithms Comparison
| Algorithm | How It Works | Best For |
|---|---|---|
| Round Robin | Distributes requests sequentially | Homogeneous servers, stateless apps |
| Weighted Round Robin | Servers get traffic proportional to weight | Heterogeneous capacity servers |
| Least Connections | Sends to server with fewest active connections | Long-lived connections (WebSocket, DB) |
| Weighted Least Conn | Combines weight and connection count | Mixed workloads |
| IP Hash | Routes same client IP to same server | Session persistence without sticky cookies |
| Random | Picks server randomly | Simple stateless workloads |
| Resource Based | Routes to server with most available resources | CPU/memory-intensive tasks |
Nginx Upstream Configuration
http {
# Round Robin (default)
upstream backend_rr {
server 10.0.0.1:8080;
server 10.0.0.2:8080;
server 10.0.0.3:8080;
}
# Weighted
upstream backend_weighted {
server 10.0.0.1:8080 weight=5;
server 10.0.0.2:8080 weight=3;
server 10.0.0.3:8080 weight=2;
}
# Least Connections
upstream backend_lc {
least_conn;
server 10.0.0.1:8080;
server 10.0.0.2:8080;
}
# IP Hash (sticky sessions)
upstream backend_sticky {
ip_hash;
server 10.0.0.1:8080;
server 10.0.0.2:8080;
}
server {
listen 80;
location / {
proxy_pass http://backend_rr;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}
Health Checks
upstream backend {
server 10.0.0.1:8080 max_fails=3 fail_timeout=30s;
server 10.0.0.2:8080 max_fails=3 fail_timeout=30s;
server 10.0.0.3:8080 backup; # used when others fail
}
# Nginx Plus active health check
upstream backend_health {
zone backend 64k;
server 10.0.0.1:8080;
server 10.0.0.2:8080;
}
server {
location /api/ {
proxy_pass http://backend_health;
health_check interval=5 fails=2 passes=3 uri=/health;
}
}
HAProxy Configuration
frontend http_front
bind *:80
default_backend http_back
backend http_back
balance roundrobin
option httpchk GET /health
server s1 10.0.0.1:8080 check inter 5s rise 2 fall 3
server s2 10.0.0.2:8080 check inter 5s rise 2 fall 3
server s3 10.0.0.3:8080 check inter 5s rise 2 fall 3 backup
Layer 4 vs Layer 7
| Type | Level | Capabilities | Examples |
|---|---|---|---|
| L4 (Transport) | TCP/UDP | Fast, no content inspection, IP/port routing | HAProxy TCP mode, AWS NLB |
| L7 (Application) | HTTP/HTTPS | URL routing, header inspection, SSL termination, compression | Nginx, HAProxy HTTP, AWS ALB |