Implementing Load Balancing

1. Using Round Robin Algorithm

AspectDetail
MechanismCyclic distribution across instances
ProsSimple, no state
ConsIgnores instance load / latency
UseHomogeneous, stateless services

2. Using Least Connections Algorithm

AspectDetail
MechanismPick instance with fewest active conns
Variant"Least request" (Envoy: P2C — power of two choices)
UseLong-lived requests, varying workloads

3. Using Weighted Round Robin

AspectDetail
MechanismHigher-capacity instances get more turns
UseMixed instance sizes; canary (90/10)

4. Using IP Hash Algorithm

AspectDetail
MechanismHash(client IP) → instance
ProsSticky w/o cookies
ConsNATs cause skew
VariantConsistent hashing (ring) for cache affinity

5. Implementing Health-Based Routing

MechanismDetail
Active health checkLB probes endpoint
Passive (outlier detection)Eject after N consecutive errors
DrainStop new traffic before shutdown
Slow-startRamp traffic to fresh instance

6. Implementing Sticky Sessions

MechanismDetail
Cookie-basedLB issues affinity cookie
Application cookieApp sets; LB respects
IP affinityHash IP
CaveatHurts elasticity; prefer stateless services + external session store

7. Using Geographic Load Balancing

MechanismDetail
GeoDNSRoute to nearest region by IP
AnycastSame IP advertised globally
Latency-basedPick lowest-RTT POP (Route 53, Cloudflare)
FailoverHealth-aware regional routing

8. Implementing Layer 7 Load Balancing

CapabilityDetail
HTTP-awarePath/header routing
TLS terminationDecrypt at LB
Retry / timeoutPer-route policies
ExamplesEnvoy, Nginx, HAProxy, ALB, Traefik

9. Using Layer 4 Load Balancing

CapabilityDetail
Protocol-agnosticTCP/UDP forwarding
PerformanceHigher throughput, lower CPU
ExamplesNLB, IPVS, HAProxy TCP mode, kube-proxy
Trade-offNo HTTP awareness

10. Handling Load Balancer Failures

TacticDetail
Multiple LBsHA pair w/ VRRP or cloud-managed
Health-checked DNSRoute 53 failover records
AnycastReroute via BGP
Client-side LBSurvives proxy failure

11. Using Load Balancing Tools

ToolBest For
EnvoyModern L7, mesh data plane
NginxL7, mature, scriptable
HAProxyL4/L7, very fast
TraefikK8s/Docker auto-discovery
AWS ELB (ALB/NLB)Managed AWS
Cloud Load Balancing (GCP)Global anycast

12. Implementing Dynamic Load Balancing

SignalAction
CPU / RPS / latencyAdjust weights live
Endpoint healthAdd/remove from pool
LocalityPrefer same zone (reduce $/latency)
AlgorithmsP2C least-request, EWMA