⚖️ Load Balancing
What Is Load Balancing?
Imagine a supermarket checkout — with only one checkout counter (server), there's a long queue. After opening multiple counters, customers are distributed across them and things move fast. Load balancing is the dispatcher that assigns customers.
Round Robin — Default Strategy
nginx
# Define a group of backend servers
upstream backend_servers {
server 192.168.1.101:3000;
server 192.168.1.102:3000;
server 192.168.1.103:3000;
}
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://backend_servers;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}Requests are distributed in order: 1st → server 1, 2nd → server 2, 3rd → server 3, 4th → server 1, and so on.
Weighted Round Robin
nginx
upstream backend_servers {
server 192.168.1.101:3000 weight=5; # High-spec server, gets more traffic
server 192.168.1.102:3000 weight=3; # Mid-spec
server 192.168.1.103:3000 weight=1; # Low-spec, gets less traffic
}IP Hash — Session Affinity
nginx
upstream backend_servers {
ip_hash; # Same IP always goes to the same server
server 192.168.1.101:3000;
server 192.168.1.102:3000;
}> 💡 Tip: > 💡 When should you use ip_hash? When the backend uses Sessions (not JWT) for login state management, every request from a user must go to the same server.
Least Connections
nginx
upstream backend_servers {
least_conn; # Prioritize the server with fewest connections
server 192.168.1.101:3000;
server 192.168.1.102:3000;
server 192.168.1.103:3000;
}Health Checks and Failover
nginx
upstream backend_servers {
server 192.168.1.101:3000 max_fails=3 fail_timeout=30s;
server 192.168.1.102:3000 max_fails=3 fail_timeout=30s;
server 192.168.1.103:3000 backup; # Backup server, not used normally
}> 📝 Note: > 📝 max_fails=3 fail_timeout=30s means: if 3 consecutive requests fail, mark that server as unavailable, then retry after 30 seconds.