Web serving & caching
Caching & CDNs
Caching is the cheapest performance and resilience win in most systems, and the source of some of the strangest bugs: users seeing old pages, one user seeing another's data, or a database falling over the moment a cache restarts. Interviewers ask about it in system design rounds and in troubleshooting questions. This topic covers the layers of caching, the HTTP headers that control browsers and CDNs, how CDNs such as CloudFront work, application caching with Redis or Valkey, and the failure modes you need to design against.
After this topic you can
- Set Cache-Control headers correctly for assets, pages and private data
- Design CloudFront behaviours, cache keys and versioned deployments
- Use Redis caching patterns and prevent stampedes, stale data and cache leaks
Lessons
-
1Why and where to cache
- The layers of caching
- Hit ratio, freshness and what to cache
-
2HTTP caching headers
- Cache-Control
- Validation and 304 Not Modified
- Cache keys and Vary
-
3CDNs
- How a CDN works
- CloudFront in practice
- Invalidation versus versioned assets
-
4Application caching with Redis and Valkey
- Caching patterns
- Memory, eviction and TTLs
- Running it on AWS
-
5Cache failure modes
- Stampedes, avalanches and penetration
- Correctness and security failures
-
6Interview questions and model answers
- "Explain no-cache vs no-store."
- "How do you deploy a new front-end version without users seeing stale files?"
- "A user reports seeing someone else's account page. What happened and how do you fix it?"
- "What is a cache stampede and how do you prevent it?"
- "Cache-aside vs write-through?"
- "How does CloudFront decide whether two requests are the same?"
- "Your Redis cache restarted and the database fell over. What would you change?"
- "Why put a CDN in front of an API at all?"