Skip to content

Week 2 — Load Balancers & Caching

A load balancer distributes traffic across multiple servers. Without it, one server becomes the bottleneck and a single point of failure.

Layer 4 (TCP/UDP)Layer 7 (HTTP)
SeesIP + portFull HTTP request
RoutingBy IP/portBy path, header, cookie
PerformanceFasterSlower but smarter
ExampleAWS NLBNginx, HAProxy, AWS ALB
AlgorithmHowBest for
Round Robin1→2→3→1→2→3Uniform request weight
Weighted Round RobinServer A gets 3x traffic of BMixed server capacity
Least ConnectionsRoute to server with fewest active sessionsLong-running requests (e.g. Odoo)
IP HashHash(client IP) → same server alwaysSession stickiness

Cache layers (closest to user first):

Browser cache → CDN → App cache (Redis) → DB query cache → DB buffer pool
  • LRU (Least Recently Used) — evict the least recently accessed item. Most common.
  • LFU (Least Frequently Used) — evict the least accessed over time.
  • TTL — expire after a fixed duration regardless of access.

In practice: LRU + TTL together.

StrategyWriteReadStale riskComplexity
Cache-asideApp writes DB, invalidates cacheApp reads cache, misses → DBMediumLow
Write-throughApp writes cache + DB togetherAlways freshLowMedium
Write-behindApp writes cache, async flush to DBFreshLow (risk: data loss)High
Read-throughCache populates itself on missAuto-populatedMediumMedium

Most practical: cache-aside. Explicit control, easy to reason about.

When cache expires and 1000 concurrent requests all miss and hit the DB simultaneously.

Solutions:

  • Mutex lock — only one request fetches, others wait
  • Background refresh — refresh before expiry, never let it expire cold
  • Probabilistic early expiry — randomly refresh slightly before TTL

Design caching for a product catalogue:

  • 100k products, updated rarely
  • 50k concurrent users browsing
  • What do you cache? For how long? What invalidates it?
  • When does Least Connections beat Round Robin?
  • Why is cache invalidation “one of the two hardest problems in computer science”?
  • What’s the difference between a CDN cache and a Redis cache?