Load Balancer¶
What it does¶
A load balancer distributes incoming client requests across computing resources (application servers, databases, etc.), then returns each resource's response to the appropriate client.
Load balancers are effective at:
- Preventing requests from going to unhealthy servers.
- Preventing overloading of resources.
- Helping eliminate a single point of failure.
Can be implemented with hardware (expensive) or software (e.g., HAProxy, NGINX).
Additional benefits¶
-
SSL termination — decrypt incoming requests and encrypt responses so backend servers don't do this expensive work, and you don't need to install X.509 certificates on each server.
-
Session persistence — issue cookies to route a client's requests to the same instance when the web app doesn't track sessions itself.
Routing strategies¶
A load balancer can route based on:
- Random
- Least loaded
- Session / cookies
- Round robin or weighted round robin
- Layer 4 (transport layer)
- Layer 7 (application layer)
Layer 4 vs Layer 7¶
Layer 4 (transport layer)¶
- Looks at source/destination IP addresses and ports (not packet contents).
- Forwards packets using Network Address Translation (NAT).
- Less flexible, but faster and cheaper to compute.
Layer 7 (application layer)¶
- Looks at header/message/cookie contents to make routing decisions.
-
Terminates the connection, reads the message, decides, then opens a connection to the selected server.
-
Example: direct video traffic to video servers, and billing traffic to security-hardened servers.
| Layer 4 | Layer 7 | |
|---|---|---|
| Inspects | IP + ports | Headers, messages, cookies |
| Speed | Faster | Slower |
| Flexibility | Low | High |
Horizontal scaling¶
Load balancers enable horizontal scaling (scaling out with more commodity machines) vs vertical scaling (scaling up one bigger, pricier server). Horizontal scaling is more cost-efficient and gives higher availability.
Disadvantages of horizontal scaling¶
- Introduces complexity; requires cloning servers.
- Servers should be stateless — no user data like sessions or profile pics.
-
Store sessions in a central data store (DB or persistent cache like Redis/Memcached).
-
Downstream servers (caches, DBs) must handle more simultaneous connections as upstream scales out.
Disadvantages of load balancers¶
- Can become a performance bottleneck if under-resourced or misconfigured.
- Adds complexity.
- A single load balancer is itself a single point of failure — so you add multiple (active-passive or active-active), which adds further complexity.
Key takeaways¶
-
Load balancers solve distribution and availability problems, not correctness.
-
Stateless servers + externalized sessions are prerequisites for clean horizontal scaling.
-
L4 vs L7 is a speed-vs-intelligence trade-off.
Scaling the Load Balancer Itself¶
A single load balancer is a bottleneck and single point of failure. To design a distributed load balancer, you must horizontally scale the load-balancing layer itself and coordinate multiple LB instances.
Core ideas¶
- Multiple LB nodes behind a virtual IP (VIP) or anycast address. DNS resolves the LB name to multiple IPs (or anycast routes clients to the nearest LB node).
- Stateless LB nodes so any node can handle any request. Persistent state (e.g., session stickiness tables) is stored in a fast distributed store like Redis or etcd, not locally.
- Consistent hashing to distribute traffic across backend servers while minimizing rehashing when backend servers are added or removed. This also helps LBs agree on which backend should receive a given request without sharing all state.
- Coordination via a consensus service (ZooKeeper, etcd) for cluster membership, leader election (if needed), and health status of backend servers. Each LB node subscribes to updates and maintains a consistent view.
- Data plane vs control plane separation: the data plane (fast packet forwarding, often in kernel or eBPF) is distributed across LB nodes; the control plane (configuration, health checks, routing policies) is managed centrally and pushed to all nodes.
- Health checks at scale: each LB node periodically probes backends and shares results with peers (or via control plane). If a node dies, others take over its traffic (via ECMP, anycast failover, or DNS repointing).
- Avoiding split-brain: for active-active LB pairs, use consistent hashing and shared state to ensure only one node acts on a given request; for active-passive, use a leader-elect algorithm (e.g., via etcd lease) so only the leader owns the VIP.
How this answers the question¶
When asked to design a distributed load balancer, this section explains how to make the LB itself scalable and highly available: stateless nodes, consistent hashing, shared coordination, and control/data plane split. It addresses the key challenges: avoiding a single point of failure, distributing load evenly, maintaining session affinity when needed, and performing health checks across a large backend fleet. Use this to describe a concrete architecture (e.g., anycast + ECMP to reach multiple L4 LB nodes, which then route to L7 proxies using consistent hashing).
flowchart LR
DNS[DNS] --> LB1[LB 1 - active]
DNS --> LB2[LB 2 - active]
LB1 --> S1[Server 1]
LB1 --> S2[Server 2]
LB2 --> S2
LB2 --> S3[Server 3]
LB1 -.health check.-> S1
LB1 -.health check.-> S2
Common Interview Questions¶
Design the application tier of a web service
Why it's asked here: the L4/L7 choice and session handling are core LB decisions.
Key points to discuss:
- Session persistence (sticky sessions) vs stateless + a central session store.
- SSL termination at the LB.
- Health checks to stop routing to unhealthy nodes.
- How the LB can become a bottleneck and how to scale it out.
flowchart LR
C[Client] --> L4[Layer 4 LB]
L4 -->|SSL termination| L7[Layer 7 proxy]
L7 --> A[App 1]
L7 --> B[App 2]
A --> S[(Session store)]