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.

A load balancer takes the clients’ HTTPS traffic and sends it to Server 1 and Server 2; Server 3 failed its health check, so the load balancer skips it A load balancer takes the clients’ HTTPS traffic and sends it to Server 1 and Server 2; Server 3 failed its health check, so the load balancer skips it
A load balancer spreads traffic across the healthy servers and takes a failed one out of rotation · Made with Akriti

Behind the scenes, modern load balancers handle four core jobs:

  1. Traffic Distribution: Selecting which backend instance receives each incoming connection or HTTP request.
  2. Health Checks: Periodically probing each backend (via TCP connect or an HTTP /health endpoint). 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.
  3. 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.
  4. 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.

FAQs

What is the difference between Layer 4 and Layer 7 load balancing? Layer 4 load balancing operates at the transport layer (TCP/UDP) and routes traffic using only IP addresses and ports, making it ultra-fast. Layer 7 load balancing operates at the application layer (HTTP/HTTPS/gRPC) and can route based on URL paths, hostnames, headers, and cookies, as well as handle TLS termination and rate limiting.
How does a load balancer handle SSL/TLS traffic? Most Layer 7 load balancers perform TLS termination: they decrypt incoming HTTPS traffic using a centralized certificate and forward requests to backend servers over a private network (or re-encrypt with internal mTLS). This centralizes certificate renewal and offloads cryptographic CPU overhead from application servers.
Can a load balancer also function as a reverse proxy? Yes. Software load balancers like NGINX, HAProxy, and Envoy are reverse proxies that also handle TLS termination, gzip/Brotli compression, request buffering, rate limiting, and response caching.
Why should you avoid sticky sessions when possible? Sticky sessions pin a user to one backend server, which breaks even load distribution and causes session loss if that server crashes or deploys. Storing session data in an external distributed cache (like Redis) keeps application servers stateless so any node can handle any request.