Handbooks / System Design / Chapter 3
Scalability and Load Balancing
7 pages · ~5 min✓ Reviewed
Builds on Networking, DNS and CDNs. Next up: API Design and Gateways.
Scaling Mental Model
1. Vertical vs Horizontal
| Vertical (scale up) | Horizontal (scale out) | |
|---|---|---|
| Change | Bigger machine: more CPU/RAM | More machines behind a LB |
| Capacity | Limited, hard ceiling | Elastic, near-linear |
| Upside | Simple, no code change | Resilient, fault tolerant |
| Downside | SPOF, downtime on resize | Complex: needs stateless design, data consistency, networking |
2. Example: 4-vCPU API at 90% CPU
verticalhorizontal
Stateless Services
1. What Stateless Means
- Stateless: any instance can serve any request
- No per-user state in instance memory or local disk
- Sessions → Redis, files → S3, data → database
- Or carry state in a signed token (JWT)
2. Stateless vs Stateful
| Stateless | Stateful |
|---|
Load Balancers: L4 vs L7
1. What a Load Balancer Is
- One entry point: a VIP or DNS name
- Spreads traffic across healthy backends
- Hides backend addresses
2. L4 vs L7
| L4 (transport) | L7 (application) | |
|---|---|---|
| Reads | IP + TCP/UDP port | HTTP host, path, headers, cookies |
Balancing Algorithms and Health Checks
1. Picking a Server
| Algorithm | Picks | Best when |
|---|---|---|
| Round robin | Next server in order | Equal servers, similar request cost |
| Weighted RR | Bigger servers get more requests | Mixed server sizes |
| Least connections | Fewest active connections | Uneven or long-lived requests |
Latency, Throughput and Estimation
1. Latency vs Throughput
| Latency | Throughput | |
|---|---|---|
| Measures | Time for one request | Requests or bytes per second |
| Unit | ms: report p50/p95/p99 | QPS / RPS, bytes/s |
- concurrency = throughput x latency (Little's law)
Scaling Cheat Sheet
1. Vertical vs horizontal
| Vertical | Horizontal | |
|---|---|---|
| Change | Bigger box | More boxes |
| Good | Simple | Elastic |
| Catch | Capped, SPOF | Needs stateless |
2. Stateless and L4 vs L7
Check yourself
An API serves 200 RPS at 50 ms latency. You add instances and it now serves 400 RPS at the same latency. How many requests are in flight?
- 400 × 0.05 = 20 (concurrency = throughput × latency)
- Adding instances raised throughput, not per-request latency
Behind a round-robin LB, users are logged out at random. Sessions sit in each instance's memory. Why, and what is the fix?
- The next request lands on an instance with no copy of the session
- Move sessions to a shared store like Redis; sticky sessions only hide the bug