Lesson 233 of 296
Load balancers, scaling and stateless services
What you will learn
- Vertical vs horizontal scaling
- What a load balancer does
- Load balancing algorithms
- Layer 4 vs layer 7
- Health checks
- Stateless services and where state goes
- Auto scaling
On this page
Vertical vs horizontal scaling
| Vertical (scale up) | Horizontal (scale out) | |
|---|---|---|
| How | A bigger machine: more CPU, RAM | More machines sharing the work |
| Pros | Simple; no code changes | Almost unlimited; survives single machine failures |
| Cons | Hard upper limit; expensive; still a single point of failure | Needs load balancing and stateless servers |
Start vertical (it is simple and modern machines are big); design so you can scale horizontally.
Load balancers
A load balancer (LB) receives incoming traffic and distributes it across several servers. Benefits:
- Scalability: add servers behind it.
- Availability: it stops sending traffic to unhealthy servers.
- Single entry point: TLS termination, routing, security rules in one place.
Examples: AWS Application Load Balancer and Network Load Balancer, nginx (Lesson 248), HAProxy, Envoy, Cloudflare. The load balancer itself must not be a single point of failure: managed cloud load balancers are redundant across availability zones.
Algorithms
| Algorithm | How | Good for |
|---|---|---|
| Round robin | Each server in turn | Similar servers, similar requests |
| Weighted round robin | More requests to bigger servers | Mixed server sizes |
| Least connections | Server with the fewest active connections | Requests of very different lengths, WebSockets |
| Least response time | Fastest-responding server | Mixed performance |
| IP hash / consistent hashing | Same client or key → same server | Stickiness, caches (Lesson 235) |
Layer 4 vs layer 7
| Layer 4 (transport) | Layer 7 (application) | |
|---|---|---|
| Sees | IP addresses and ports (TCP/UDP) | Full HTTP: URL, headers, cookies |
| Can do | Fast forwarding of connections | Route /api to one service and /images to another, TLS termination, header rules, rate limits |
| Example | AWS NLB | AWS ALB, nginx |
Health checks
The load balancer regularly calls each server (for example GET /readyz, Lesson 193). Servers that fail are removed from rotation until they recover. This is how deployments and crashes avoid user-facing errors.
Stateless services
A server is stateless when it keeps no user data in its own memory between requests. Any request can go to any server. This is what makes horizontal scaling and safe restarts possible.
Where state goes instead:
| State | Store it in |
|---|---|
| Sessions | Redis or the database (Lesson 200), or a signed token |
| Uploaded files | Object storage (S3), not the server's disk |
| Caches shared by all servers | Redis |
| Background work | A queue (Lesson 236) |
Sticky sessions (the LB always sends a user to the same server) are a workaround for stateful servers; they make scaling and failover worse, so avoid them unless required (some WebSocket setups).
Auto scaling
The platform adds servers when load rises (CPU above 60%, queue length, requests per target) and removes them when it falls: AWS Auto Scaling groups, ECS service scaling, Kubernetes Horizontal Pod Autoscaler. Requirements: stateless servers, fast start-up, graceful shutdown, and headroom, because new servers take time to start.
How to say it in an interview
"I start with vertical scaling for simplicity but design for horizontal scaling, which needs stateless application servers: sessions in Redis or tokens, files in object storage, shared caches in Redis and background work in queues. A load balancer spreads traffic with round robin or least connections, removes unhealthy servers using readiness checks, and at layer 7 can route by path and terminate TLS. With stateless servers I can auto scale on CPU or request metrics and deploy without downtime, and I avoid sticky sessions unless a protocol requires them."
Key words
| Word | Meaning |
|---|---|
| Vertical scaling | Using a bigger machine |
| Horizontal scaling | Using more machines |
| Load balancer | Distributes traffic across servers |
| Round robin / least connections | Load balancing algorithms |
| Layer 4 / layer 7 | Transport-level / HTTP-level load balancing |
| Stateless service | Keeps no user state in memory between requests |
| Sticky session | A user always routed to the same server |
| Auto scaling | Automatically adding and removing servers |
Now prove it
This lesson is complete when you pass the quiz and all three practice levels.