# Load Balancing

> Distribute traffic across servers with L4 and L7 load balancers, choose balancing algorithms, and use health checks to route around failures. Free notes, key...

Canonical: https://www.internhack.in/roadmaps/tracks/system-design/load-balancing
Last updated: 2026-11-28

Distribute traffic across servers with L4 and L7 load balancers, choose balancing algorithms, and use health checks to route around failures.

Intermediate · about 3 hours · difficulty 2/5

Learn first: [HTTP, Connections and Real-Time Protocols](https://www.internhack.in/roadmaps/tracks/system-design/http-for-system-design)

A **load balancer (LB)** spreads incoming requests across a pool of servers so no single machine is overwhelmed, and it removes unhealthy servers from rotation. It is the component that turns "one server" into "a fleet".

### L4 vs L7

- **Layer 4** balancers route by IP and port without reading the request. Very fast, protocol agnostic. Example: AWS NLB.
- **Layer 7** balancers understand HTTP. They can route by path or header (/api to one pool, /images to another), terminate TLS, add headers and retry. Examples: Nginx, HAProxy, Envoy, AWS ALB.

### Algorithms

- **Round robin** and **weighted round robin**: simple and fair when requests cost about the same.
- **Least connections** / **least outstanding requests**: better when request costs vary.
- **IP hash / consistent hashing**: the same client or key goes to the same server. Useful for caches and sticky sessions.
- **Power of two choices**: pick two random servers, send to the less loaded one. Cheap and surprisingly good.

### Health checks and failure

Active checks (hit /health every few seconds) and passive checks (watch for errors) decide who stays in the pool. Make health checks meaningful but cheap: a check that queries the database can take down the whole fleet when the database is slow.

### Avoiding the LB as a single point of failure

Run LBs in pairs (active-passive with a floating IP, or active-active behind DNS or anycast). Managed cloud LBs handle this for you.

### Sticky sessions

Pinning a user to one server makes stateful apps work but hurts even distribution and failover. Prefer stateless servers with session data in Redis.

### Pitfalls

- Deep health checks that cascade failures.
- Forgetting that WebSocket connections are long-lived, so round robin at connect time can leave servers unbalanced.

You are ready to move on when you can choose L4 or L7 and a balancing algorithm for a REST API, a WebSocket chat service and a cache cluster.

## Key concepts

- **L4 vs L7 load balancing**
- **Round robin and weighted**
- **Least connections**
- **Consistent hashing based routing**
- **Health checks**
- **Sticky sessions**
- **LB redundancy**
- **Power of two choices**

## Mini project

Run three copies of a small API behind Nginx or HAProxy in Docker. Try round robin and least connections, kill one container, and record how quickly the balancer stops sending it traffic.

## Check yourself

Why can a health check that queries the database make an outage worse during a database slowdown?

## Free resources

- [What is load balancing?](https://www.cloudflare.com/learning/performance/what-is-load-balancing/) (Cloudflare)
- [NGINX documentation](https://nginx.org/en/docs/) (NGINX)
- [Envoy proxy documentation](https://www.envoyproxy.io/docs) (Envoy)
- [The System Design Primer: load balancer](https://github.com/donnemartin/system-design-primer) (GitHub)

Previous: [Content Delivery Networks (CDNs)](https://www.internhack.in/roadmaps/tracks/system-design/cdn) · Next: [Reverse Proxies and API Gateways](https://www.internhack.in/roadmaps/tracks/system-design/proxies-and-api-gateways)

## More in DNS, CDNs, Load Balancers and Gateways

- [DNS in System Design](https://www.internhack.in/roadmaps/tracks/system-design/dns)
- [Content Delivery Networks (CDNs)](https://www.internhack.in/roadmaps/tracks/system-design/cdn)
- [Load Balancing](https://www.internhack.in/roadmaps/tracks/system-design/load-balancing)
- [Reverse Proxies and API Gateways](https://www.internhack.in/roadmaps/tracks/system-design/proxies-and-api-gateways)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [System Design Roadmap](https://www.internhack.in/roadmaps/tracks/system-design)
