Zero→Hero
0/17
Module 3 · Scale & Delivery · Unit 09

Scaling & Load Balancing

"Traffic just grew 5×. What do you change?" is a near-guaranteed prompt. This unit is the systematic answer: find the bottleneck, scale the right layer, balance the load.

⏱ 30 min Prereq: Units 02, 04, 06 You'll be able to: answer "handle 5× traffic" methodically

By the end of this unit

Vertical vs horizontal

Vertical (scale up)

  • Bigger machine: more CPU/RAM.
  • Zero code change; instant relief.
  • Hard ceiling + single point of failure + expensive.
vs

Horizontal (scale out)

  • More machines behind a load balancer.
  • Near-limitless, fault-tolerant, commodity hardware.
  • Requires statelessness, coordination, data partitioning.
The senior framing "I scale out, not up, for anything that must stay available — so I keep app servers stateless (session/state in Redis or a token), put them behind a load balancer, and autoscale on load. Vertical scaling is a quick stopgap with a ceiling."

The 5× method: find the bottleneck first

Don't scale everything — walk the request path and ask what saturates first. Then apply the matching fix:

LayerSaturation signFix
App tierCPU-bound, statelessAdd servers + load balancer; autoscale.
Read pathDB read CPU high, read-heavy ratioCache (Unit 06) + read replicas (Unit 04).
Write pathPrimary write-saturated; data too bigShard (Unit 04); batch; queue writes (Unit 08).
Slow workLong requests block threadsMove to async workers via a queue.
Static/mediaBandwidth-heavy assetsCDN + object storage (Units 10–11).
State is the enemy of horizontal scale If app servers hold session state in memory, you can't freely add/remove them. Externalize state (Redis, DB, signed tokens) so any server can handle any request — the precondition for autoscaling.

Read vs write optimization

Read-heavy (most apps)

  • Cache aggressively (multi-layer).
  • Read replicas; route reads off the primary.
  • Denormalize / precompute hot views (e.g., precomputed feeds).
  • CDN for static and cacheable responses.

Write-heavy (logging, IoT, chat)

  • Shard to spread write load.
  • Batch & buffer through a queue.
  • Write-optimized stores (LSM-tree: Cassandra).
  • Async/eventual where correctness allows.

Load balancing

A load balancer spreads requests across servers and removes unhealthy ones via health checks. L4 (transport) balances by IP/port — fast and protocol-agnostic. L7 (application) reads HTTP and can route by path/host/header — smarter, slightly costlier. Common algorithms:

The LB itself must not be a single point of failure — run it redundantly (active-active or active-passive) behind a floating IP / DNS.

Quick check
Traffic grows 5×. Your DB primary is at 90% CPU but it's almost all reads, and the dataset fits on one node. First move?
Cache + replicas. It's a read bottleneck on data that still fits one node, so offload reads first — cheaper and simpler than sharding. Reserve sharding for write saturation or size limits. Match the fix to the saturating layer.
Quick check
Why must app servers be stateless to autoscale cleanly?
Any server handles any request. If session state lives in one server's memory, scaling in/out or a server dying breaks users mid-session. Externalizing state (Redis/DB/signed token) lets the LB send a request anywhere — the foundation of elastic scaling.

Practice before you move on

Take your photo-feed design at 1× and narrate exactly what changes at 10×: which layer saturates first, the order you'd apply fixes (cache → replicas → shard → CDN → async), and where load balancers sit. Keep it to a tight 2-minute story.

Finished this 30-min unit?