Skip to content

Instantly share code, notes, and snippets.

@mohashari
Created March 14, 2026 15:13
Show Gist options
  • Select an option

  • Save mohashari/3cf7db17b8061b26efd0dea89e179f45 to your computer and use it in GitHub Desktop.

Select an option

Save mohashari/3cf7db17b8061b26efd0dea89e179f45 to your computer and use it in GitHub Desktop.
Code snippets — Load Balancing Techniques
upstream backend {
server 192.168.1.10:8080 max_fails=3 fail_timeout=30s;
server 192.168.1.11:8080 max_fails=3 fail_timeout=30s;
}
Client → L7 LB → Routes /api/* to API servers
→ Routes /static/* to CDN
→ Routes /ws/* to WebSocket servers
# NGINX Plus or use Lua in open-source NGINX
upstream backend {
zone backend 64k;
server 192.168.1.10:8080;
server 192.168.1.11:8080;
health_check interval=5s passes=2 fails=3 uri=/health;
}
Request 1 → Server A
Request 2 → Server B
Request 3 → Server C
Request 4 → Server A (cycle repeats)
http {
upstream api_backend {
least_conn; # Algorithm
server 10.0.0.10:8080 weight=2; # Higher capacity
server 10.0.0.11:8080 weight=1;
server 10.0.0.12:8080 weight=1;
keepalive 32; # Reuse connections to backends
}
server {
listen 443 ssl http2;
server_name api.example.com;
ssl_certificate /etc/ssl/cert.pem;
ssl_certificate_key /etc/ssl/key.pem;
location /api/ {
proxy_pass http://api_backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 5s;
proxy_send_timeout 30s;
proxy_read_timeout 30s;
# Retry on errors
proxy_next_upstream error timeout http_500;
proxy_next_upstream_tries 3;
}
}
}
Server A (weight 3): handles 3x more traffic
Server B (weight 1): baseline
Sequence: A, A, A, B, A, A, A, B...
Server A: 45 connections
Server B: 52 connections
Server C: 38 connections → next request goes here
Server A: 38ms avg, 45 connections → score: 38 × 45 = 1,710
Server B: 22ms avg, 80 connections → score: 22 × 80 = 1,760
Server C: 45ms avg, 30 connections → score: 45 × 30 = 1,350 → winner
hash(client_ip) % num_servers = server_index
Hash ring with virtual nodes:
[ Server A ] [ Server B ] [ Server C ] [ Server A ] [ Server B ] ...
↑ ↑
Request 1 (hash → here) Request 2 (hash → here)
api.example.com →
US-East: 1.2.3.4 (primary)
US-West: 5.6.7.8 (failover)
EU: 9.10.11.12
upstream backend {
server 192.168.1.10:8080;
server 192.168.1.11:8080;
server 192.168.1.12:8080;
sticky cookie srv_id expires=1h domain=.example.com path=/;
}
Client → L4 LB (sees: TCP port 443) → Backend Server
↑ Doesn't look inside the packet
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment