Load Balancer in Distributed Systems
A load balancer sits in front of a pool of backend servers and distributes incoming network or HTTP traffic across them so no single machine gets overwhelmed. By continuously checking server health and routing only to healthy nodes, load balancers enable horizontal scaling, zero-downtime deployments, and high availability.
How a Load Balancer Works
Every request from a client hits the load balancer’s single public address (or DNS name) first. The load balancer picks a healthy backend server using a configured routing algorithm, forwards the request, and returns the response to the client.
Behind the scenes, modern load balancers handle four core jobs:
- Traffic Distribution: Selecting which backend instance receives each incoming connection or HTTP request.
- Health Checks: Periodically probing each backend (via TCP connect or an HTTP
/healthendpoint). When a server fails consecutive checks, it is automatically drained and removed from rotation until it recovers — preventing isolated node crashes from becoming full outages. - TLS / SSL Termination: Decrypting HTTPS traffic at the load balancer so backend servers don’t burn CPU on handshakes and certificates only need to be managed in one place.
- Session Persistence (Sticky Sessions): Optionally pinning a user’s requests to the same backend instance (via cookies or IP hash) when legacy apps hold local session state. For modern stateless microservices, prefer storing sessions in a shared distributed cache (like Redis) instead of relying on sticky sessions.
Load Balancing Algorithms
Choosing the right algorithm depends on whether requests have uniform cost and whether servers have identical capacity:
| Algorithm | How It Routes | Best For | Watch-Out |
|---|---|---|---|
| Round Robin | Cycles through servers in order (1 → 2 → 3 → 1) |
Stateless servers with equal hardware and uniform request cost | Can overload a server stuck on slow requests |
| Weighted Round Robin | Sends more traffic to servers with higher assigned weights | Mixed fleets (e.g., newer larger instances alongside smaller ones) | Static weights don’t react to sudden CPU spikes |
| Least Connections | Picks the server with the fewest active open connections | Long-lived requests (WebSockets, file uploads, slow queries) | Slightly more state to track per backend |
| Least Response Time | Combines active connections with recent server latency | Workloads with variable request cost or noisy neighbors | Requires continuous latency sampling |
| IP Hash / Consistent Hashing | Hashes client IP or request key to a consistent backend | Local cache affinity or stateful routing | Uneven load if many users share one corporate NAT IP |
Layer 4 vs. Layer 7 Load Balancing
Load balancers operate at two different layers of the OSI model:
| Dimension | Layer 4 (Transport — TCP/UDP) | Layer 7 (Application — HTTP/gRPC) |
|---|---|---|
| Inspects | Source/destination IP and port only | Full HTTP headers, URL path, cookies, hostnames, gRPC methods |
| Routing flexibility | Forwards raw packets/connections without reading payload | Routes /api/* to API servers and /static/* to asset servers; supports canary splits and rate limiting |
| Performance & latency | Extremely fast, lower CPU overhead (no payload parsing) | Slightly higher latency/CPU due to TLS termination and HTTP parsing |
| Common examples | AWS Network Load Balancer (NLB), HAProxy TCP mode, LVS | AWS Application Load Balancer (ALB), NGINX, Envoy, Cloudflare |
Types of Load Balancers
- Cloud Load Balancers: Managed services built into cloud platforms (AWS ALB/NLB, Google Cloud Load Balancing, Azure Load Balancer, Cloudflare). They auto-scale, integrate with auto-scaling groups, and eliminate manual hardware ops.
- Software Load Balancers: Open-source proxies deployed on standard VMs or Kubernetes clusters — NGINX, HAProxy, Envoy, and Traefik are industry standards for both edge routing and internal service-to-service traffic.
- Hardware Load Balancers: Dedicated network appliances (F5, Citrix, A10) still used in on-premise data centers with strict regulatory or ultra-high-throughput networking requirements.
Avoiding a Single Point of Failure
If all traffic flows through a single load balancer, what happens if that load balancer crashes? Production setups prevent this by running load balancers in active-passive or active-active pairs:
- Active-passive: A standby load balancer monitors the primary over heartbeat checks and takes over its floating/virtual IP (VIP) automatically on failure.
- Active-active (DNS / Anycast): Multiple load balancers serve traffic simultaneously behind multi-record DNS or BGP Anycast, often paired with a global CDN at the edge.
How Load Balancers Fit Into Distributed Systems
- Edge + Load Balancer: A CDN serves static assets near users, an API Gateway enforces auth and rate limiting, and load balancers distribute dynamic API requests across backend pools.
- Availability under failures: While the CAP theorem governs consistency vs. availability inside stateful data stores, load balancers keep stateless service tiers available by routing around unhealthy nodes.
- Protecting synchronous paths: Combined with timeouts and circuit breakers, load balancers help limit blast radius in services with temporal coupling.