# Caching and CDNs

> Use caches at every layer, choose cache-aside, write-through or write-back, handle eviction and invalidation, and serve static content via CDNs. Free notes,...

Canonical: https://www.internhack.in/roadmaps/tracks/cs-fundamentals/caching-and-cdn
Last updated: 2026-11-28

Use caches at every layer, choose cache-aside, write-through or write-back, handle eviction and invalidation, and serve static content via CDNs.

Intermediate · about 5 hours · difficulty 3/5

Learn first: [Load Balancers, Proxies and API Gateways](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/load-balancing-and-proxies)

Caching stores the results of expensive work closer to where they are needed. It is the most powerful and most error-prone tool in system design.

### Where caches live

Browser cache, **CDN** edge servers, reverse proxy cache, application in-memory cache, distributed caches like **Redis** or Memcached, and the database's own buffer pool.

### Caching strategies

- **Cache-aside (lazy loading)**: the app checks the cache, on a miss reads the DB and fills the cache. The most common pattern.
- **Read-through**: the cache itself loads from the DB on a miss.
- **Write-through**: write to cache and DB together. Consistent, but slower writes.
- **Write-back (write-behind)**: write to cache, flush to DB later. Fast, but risk of data loss.
- **Write-around**: write to DB only; the cache fills on reads.

### Eviction and expiry

- **LRU** (least recently used, the same data structure you built in DSA), **LFU**, FIFO.
- **TTL** bounds staleness; choose it from how stale the data may safely be.

### The hard parts

- **Invalidation**: when data changes, delete or update the cached copy. Deleting on write is usually safer than updating.
- **Thundering herd / cache stampede**: a popular key expires and thousands of requests hit the DB at once. Fix with request coalescing, locks or jittered TTLs.
- **Cache penetration**: repeated requests for keys that do not exist; cache the "not found" or use a Bloom filter.
- **Hot keys**: one key gets most of the traffic; replicate it or add a local cache.

### CDNs

CDNs cache static assets (images, JS, video) at edge locations near users, cutting latency and origin load. Push vs pull CDNs, and cache-busting with versioned file names.

Good enough to move on: you can add a cache to a design, justify the strategy and TTL, and explain how you would prevent a stampede on a trending key.

## Key concepts

- **Cache layers from browser to DB**
- **Cache-aside and read-through**
- **Write-through, write-back, write-around**
- **Eviction policies and TTL**
- **Cache invalidation**
- **Stampede, penetration and hot keys**
- **CDNs and edge caching**
- **Redis basics**

## Mini project

Add Redis cache-aside caching to a slow API endpoint in one of your projects, measure p50 and p95 latency before and after with a load test, then implement invalidation on update and a simple lock to prevent stampedes when a hot key expires.

## Check yourself

When a user updates their profile, why is deleting the cached entry usually safer than writing the new value into the cache?

## Free resources

- [The System Design Primer: Cache](https://github.com/donnemartin/system-design-primer) (GitHub)
- [Redis documentation](https://redis.io/docs/latest/) (Redis)
- [Cloudflare Learning Center: What is a CDN?](https://www.cloudflare.com/learning/cdn/what-is-a-cdn/) (Cloudflare)

Previous: [Load Balancers, Proxies and API Gateways](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/load-balancing-and-proxies) · Next: [Scaling Databases: Replication, Partitioning and Sharding](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/database-scaling)

## More in System Design Foundations

- [Scalability, Latency and Back-of-the-Envelope Estimates](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/scalability-basics)
- [Load Balancers, Proxies and API Gateways](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/load-balancing-and-proxies)
- [Caching and CDNs](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/caching-and-cdn)
- [Scaling Databases: Replication, Partitioning and Sharding](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/database-scaling)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [CS Fundamentals and Placement Prep Roadmap](https://www.internhack.in/roadmaps/tracks/cs-fundamentals)
