Skip to main content

Load Balancer

Distributes incoming traffic across multiple backend servers so no single server becomes a bottleneck.


Traffic Distribution

The load balancer health-checks each server continuously. Unhealthy servers are automatically removed from rotation.


Algorithms

AlgorithmHow it worksWhen to use
Round RobinRotate through servers in orderServers are roughly equal capacity
Least ConnectionsSend to server with fewest active connectionsLong-lived requests (WebSocket, uploads)
IP HashHash client IP → always same serverSession stickiness without shared session store
Weighted Round RobinRound robin, but some servers get more trafficMixed capacity servers
RandomPick a random serverSimple, surprisingly effective at scale

My default: Round Robin for stateless APIs. Least Connections for WebSocket services. IP Hash only when I can't use a shared session store.


L4 vs L7 Load Balancing

L4 (Transport Layer)L7 (Application Layer)
Routes byIP + TCP portHTTP headers, URL path, cookies
TLS termination❌ (passes through)✅ (inspects content)
Content-based routing✅ (/api/* → API servers, /* → web servers)
PerformanceFaster (less inspection)More flexible
ExampleAWS NLB, HAProxy TCP modenginx, AWS ALB, Traefik

My default: L7 (ALB / nginx) for web apps — path-based routing and TLS termination are worth it.


My Tool Choices

ContextTool
Cloud (AWS)ALB (L7) or NLB (L4)
Self-hosted / K8snginx or Traefik
Edge / CDNCloudflare
Dev environmentnginx or Caddy

Reference