System Design · Lesson 3 of 8 · Updated ·
Caching and CDNs — Make Systems Fast and Cheap
A cache keeps a copy of frequently used data somewhere faster than the original. Reading from Redis takes about 1 ms; a complex database query can take 50–100 ms. Caching is usually the single biggest speed-up in a system.
1. Caches are everywhere
| Layer | Example |
|---|---|
| Browser | images and CSS cached on your phone |
| CDN | static files served from a city near you |
| Application | Redis / Memcached in front of the database |
| Database | its own buffer cache of hot pages |
2. Cache-aside (the most common pattern)
get(key):
value = cache.get(key)
if value is not None: # hit
return value
value = db.query(...) # miss
cache.set(key, value, ttl=300)
return value
Cache hit ratio
If 90% of reads hit the cache, the database handles only 10% of reads — 10× less load.
3. Keeping the cache correct (invalidation)
"There are only two hard things in computer science: cache invalidation and naming things."
- TTL (time to live): entries expire automatically (e.g. after 5 minutes). Simple; data may be a little stale.
- Delete on write: when data changes, update the DB and delete the cache key; the next read reloads it.
- Choose staleness per data type: a course catalogue can be 10 minutes old; a bank balance can't.
4. Write strategies
| Strategy | How | Trade-off |
|---|---|---|
| Write-through | write to cache and DB together | consistent, slower writes |
| Write-back (write-behind) | write to cache, flush to DB later | very fast, risk of losing data |
| Write-around | write to DB only; cache fills on read | avoids caching data nobody reads |
5. Eviction: when the cache is full
| Policy | Removes |
|---|---|
| LRU — least recently used | the item not used for the longest time (most common) |
| LFU — least frequently used | the item used least often |
| FIFO | the oldest item |
(Same idea as page replacement in the Operating Systems lesson.)
6. CDN (Content Delivery Network)
A CDN (Cloudflare, Akamai, CloudFront) copies your static files — images, videos, CSS, JS — to servers in many cities. A student in Patna downloads them from a nearby server instead of your server in Mumbai.
Benefits: lower latency, far less load on your servers, protection from traffic spikes and attacks.
7. What NOT to cache
- Data that changes on every request.
- Highly personal or sensitive data in shared caches (without care).
- Anything where stale data causes real harm (payments, stock counts at checkout).
Common mistakes
- Caching without a TTL or invalidation plan → users see old data for hours.
- Cache stampede: a popular key expires and thousands of requests hit the DB at once — use locks or staggered TTLs.
- Treating the cache as the source of truth (it can be wiped any time).
Quick recap
- Cache = fast copy; Redis ~1 ms vs DB ~10–100 ms.
- Cache-aside: check cache → miss → DB → store with TTL.
- Invalidate with TTL or delete-on-write; evict with LRU.
- CDNs serve static files close to users.