Load Balancer Guide

Algorithms Comparison

AlgorithmHow It WorksBest For
Round RobinDistributes requests sequentiallyHomogeneous servers, stateless apps
Weighted Round RobinServers get traffic proportional to weightHeterogeneous capacity servers
Least ConnectionsSends to server with fewest active connectionsLong-lived connections (WebSocket, DB)
Weighted Least ConnCombines weight and connection countMixed workloads
IP HashRoutes same client IP to same serverSession persistence without sticky cookies
RandomPicks server randomlySimple stateless workloads
Resource BasedRoutes to server with most available resourcesCPU/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

TypeLevelCapabilitiesExamples
L4 (Transport)TCP/UDPFast, no content inspection, IP/port routingHAProxy TCP mode, AWS NLB
L7 (Application)HTTP/HTTPSURL routing, header inspection, SSL termination, compressionNginx, HAProxy HTTP, AWS ALB