# Load Balancers and Stateless Services — System Design Interview Prep

Source: https://www.skillbyai.com/en/system-design-interview/b-lb

> Scale out the web tier.

## L4 versus L7, health checks, statelessness

A **load balancer** distributes requests across servers and removes unhealthy ones using health checks. Layer 4 balancers route by IP and port (fast, protocol-agnostic); layer 7 balancers understand HTTP and can route by path or header, terminate TLS and retry. Algorithms include round robin, least connections and hashing for affinity. Keep application servers **stateless** (sessions in a shared store or tokens) so any server can handle any request and you can scale horizontally.

## Spread load, avoid repeated work

Load balancers spread traffic, consistent hashing spreads data, and caches avoid repeated work.

![Three ideas: load balancing, consistent hashing, caching.](assets/figures/system-design-interview/section-3-map.svg) — Figure 3.1 — Load balancing, hashing and caching.

## A restaurant host

The host seats guests at whichever waiter has capacity and stops seating at a table whose waiter has gone home sick.

## Make the balancer redundant too

Use managed balancers or active-passive pairs so the load balancer is not itself a single point of failure.

**Quiz:** Why keep application servers stateless?

- [x] So any server can handle any request and servers can be added or removed freely
- [ ] To make the code shorter
- [ ] Because databases require it
- [ ] To avoid needing a load balancer

*Answer:* So any server can handle any request and servers can be added or removed freely. Statelessness enables horizontal scaling.
