Scaling & Load Balancers
System Design · Scaling & Data · Basics · 14 min
Bigger machine vs more machines, and how traffic gets shared.
A greeter at a row of checkout lanes. Round robin points shoppers to lanes 1, 2, 3, 1, 2, 3 no matter how long each queue is; a smarter greeter glances at the queues first. Every few seconds the greeter also checks each cashier is awake, and closes a lane whose cashier stopped answering.
The idea in everyday words
One cashier, long line. Option A: hire a super-fast cashier (vertical scaling, a bigger server). Option B: open 10 checkout lanes (horizontal scaling, more servers). The load balancer is the store greeter who points each customer to a free lane.
Vertical vs Horizontal
| Vertical (scale up) | Horizontal (scale out) | |
|---|---|---|
| How | Bigger CPU/RAM | More servers |
| Limit | Hardware max | Almost unlimited |
| Failure | Single point of failure | One dies, others continue |
| Complexity | Simple | Needs load balancer, stateless servers |
Load balancer strategies
- Round robin – 1, 2, 3, 1, 2, 3…
- Least connections – send to the least busy server
- IP hash – same user always goes to the same server
- Health checks remove dead servers automatically.
Servers should be stateless (store sessions in Redis/DB), so any server can handle any request.
Step by step
- Request arrives:
User hits the load balancer (e.g. AWS ALB / Nginx).
- Health check:
LB knows servers A, B, C are healthy.
- Pick server:
Round robin → server B.
- Server B dies:
Health check fails → LB stops sending traffic to B.
- Auto-scaling:
Traffic spikes → cloud adds servers D and E automatically.
Code example
servers = ["A", "B", "C"]
i = 0
def next_server():
global i
s = servers[i % len(servers)]
i += 1
return s
for req in range(7):
print("request", req, "->", next_server())Interview tips
Say 'I'll put the app servers behind a load balancer and keep them stateless.'
Cloud names: AWS ALB/ELB, GCP Cloud Load Balancing, Azure Load Balancer / App Gateway, open source Nginx/HAProxy/Envoy.
Mention auto-scaling groups based on CPU or request count.
Challenge: Round Robin Router
Given n servers (numbered 0..n-1) and k requests, return a list of which server handles each request using round robin.
Example: round_robin(3, 5) → [0, 1, 2, 0, 1]
Solve it in the browser with tests, hints and an AI coach.
Practice problems
- What is an Application Load Balancer?
- Health checks for ALB target groups
- What is Amazon EC2 Auto Scaling?
More in Scaling & Data
- Caching (Redis & CDN)
- Databases: SQL, NoSQL, Sharding
- Choosing a Database on AWS
- DynamoDB Data Modeling
- Consistent Hashing
Previous: Serverless on AWS · Next: Caching (Redis & CDN)