How load balancers work: algorithms and types
round-robin, least connections, IP hash, layer 4 vs layer 7 load balancing, health checks, sticky sessions, load balancer as SPOF
How load balancers work: algorithms and types
What a Load Balancer Does
A load balancer sits in front of your server pool and distributes incoming requests across instances. It also handles health checks โ removing unhealthy instances from rotation automatically.
Routing Algorithms
Round-Robin: requests distributed sequentially. Simple, assumes all servers have equal capacity.
Least Connections: route to the server with fewest active connections. Better for variable request durations such as long uploads vs fast API calls.
IP Hash: hash the client IP to always route the same client to the same server. Use only when sticky sessions are unavoidable โ it defeats horizontal scaling benefits and skews load if IPs are unevenly distributed.
Layer 4 vs Layer 7
L4 load balancers operate at TCP level โ fast, no HTTP parsing. L7 load balancers read HTTP content and can route based on URL path, headers, or cookies:
# Nginx L7 routing
location /api/ { proxy_pass http://api_servers; }
location /static/ { proxy_pass http://static_servers; }
location /ws/ { proxy_pass http://websocket_servers; }Health Checks
Always expose a /health endpoint. Load balancers poll it at regular intervals and remove a failing instance from rotation within one check interval. Without health checks, you route traffic to dead servers and users get intermittent errors.
