Design a Load Balancer
Understanding the Problem
⚖️ What is a load balancer? A load balancer sits between clients and a pool of servers and decides which server handles each connection or request. Clients see one address; behind it, dozens or thousands of servers share the work. Done well, it lets you add and remove servers freely, routes around ones that fail, and keeps any single server from being overwhelmed. Think AWS ELB, Google Cloud Load Balancing, or a fleet of NGINX, HAProxy or Envoy proxies.
You'll also hear "design a distributed load balancer." That's the same question with the emphasis on the part most candidates skip: the load balancer can't be one machine either, so it needs its own way to scale and fail over. We'll build that in.
Functional Requirements
Core Requirements
- The load balancer should spread incoming traffic across a pool of backend servers behind a single address.
- It should detect unhealthy backends and stop sending them traffic, then bring them back once they recover.
- It should support both L4 (TCP/UDP connections) and L7 (HTTP requests, routed by host, path or header) load balancing.
- Operators should be able to add and remove backends and change routing rules without dropping existing connections.
Below the line (out of scope)
- Web application firewall rules and bot detection.
- Caching responses (that's a CDN or cache in front).
- Authentication, API keys and rate limits. Those belong in an API gateway; see /system-design/api-gateway.
Non-Functional Requirements
Ask what kind of traffic and how much. For our design: 1M HTTP requests per second at peak, 5M concurrent connections (many are long-lived WebSockets and HTTP/2 connections), and 100 Gbps of traffic in a region.
Core Requirements
- The load balancer should add minimal latency: microseconds at L4, under 1ms at L7 excluding TLS handshakes.
- It must be highly available, with no single point of failure, including the load balancer itself.
- It should scale horizontally as traffic grows.
- Existing connections should survive changes to the backend pool and to the load balancer fleet as far as possible.
Below the line (out of scope)
- Choosing between regions. We'll touch on global load balancing at the end, but focus on one region.
The numbers rule out one box. A well-tuned L7 proxy on a modern server handles on the order of tens of thousands to a hundred thousand HTTPS requests per second, depending on TLS, request size and configuration. TLS handshakes are the expensive part; reused connections are cheap. At 1M requests per second, that's 20 to 50 L7 proxy nodes, plus headroom. Connections cost memory too: an L7 proxy keeps buffers per connection, and at even 20KB each, 5M connections is 100GB spread across the fleet. On bandwidth, 100 Gbps is more than one machine's network card. So the load balancer is itself a distributed system, and something has to balance traffic across the load balancers.
If your design has one load balancer box in front of everything, your interviewer's first question will be "what happens when that box dies?" Plan for it from the start.