Skills101

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

LayerExample
Browserimages and CSS cached on your phone
CDNstatic files served from a city near you
ApplicationRedis / Memcached in front of the database
Databaseits own buffer cache of hot pages

2. Cache-aside (the most common pattern)

The app asks the cache first; on a hit it returns immediately; on a miss it reads the database and stores the result in the cache with a TTL

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

StrategyHowTrade-off
Write-throughwrite to cache and DB togetherconsistent, slower writes
Write-back (write-behind)write to cache, flush to DB latervery fast, risk of losing data
Write-aroundwrite to DB only; cache fills on readavoids caching data nobody reads

5. Eviction: when the cache is full

PolicyRemoves
LRU — least recently usedthe item not used for the longest time (most common)
LFU — least frequently usedthe item used least often
FIFOthe 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.

Practice what you learned

📝 Quiz: Caching and CDNs5 questions · instant answers & explanations · +10 XP · counts towards your free certificateTake the quiz →