CHAPTER 07
Beginner
Caching Strategies and CDN
Updated: May 18, 2026
5 min read
# CHAPTER 7
Caching Strategies and CDN
1. Chapter Introduction
If there is a "cheat code" in System Design, it is Caching. Hitting a relational database on disk takes milliseconds (sometimes seconds). Hitting a cache in RAM takes microseconds. If you do not include a caching layer in your interview architecture, you will fail the scalability portion of the screen. This chapter explores the mechanics of distributed caches (Redis/Memcached), the notoriously difficult problem of Cache Invalidation, and how Content Delivery Networks (CDNs) scale static assets globally.2. What is a Cache?
A cache is a temporary storage area that stores the result of an expensive operation (like a complex database query or a heavy computation) so that subsequent requests can be served instantly.- Databases store data on Disk (SSD/HDD) -> Persistent but slow.
- Caches store data in Memory (RAM) -> Ephemeral but lightning fast.
3. Where to Cache?
Caching can happen at almost every layer of the architecture:- 1. Client/Browser Cache: The browser stores logos and CSS files locally so it doesn't have to download them again.
- 2. CDN (Content Delivery Network): Geographically distributed servers caching static media globally.
- 3. Web Server Cache: Reverse proxies (Nginx) caching HTML pages.
- 4. Application/Database Cache (Redis/Memcached): Caching the raw JSON data returned from slow database queries.
4. Distributed Caches: Redis vs. Memcached
In interviews, you will typically use a distributed Key-Value store as your application cache.- Memcached: Simple, multi-threaded, purely in-memory. Great for simple string caching.
- Redis: The industry standard. Single-threaded, but supports complex data structures (Lists, Sets, Hashes) and offers persistence (saving snapshots to disk). *When in doubt, say Redis.*
5. Cache Reading Strategies
How does the application interact with the cache and the database? Cache-Aside (Lazy Loading):- 1. The app asks the Cache for the data.
- 2. Cache Hit: Data is found and returned instantly.
- 3. Cache Miss: Data is not found. The app queries the Database, receives the data, saves a copy in the Cache, and returns it to the user.
6. Cache Writing Strategies
When data changes, how do you keep the cache updated?- 1. Write-Through: App writes data to the Cache AND the Database simultaneously.
- 2. Write-Around: App writes directly to the Database, bypassing the cache.
- 3. Write-Back: App writes ONLY to the Cache (fast). The cache asynchronously syncs to the database later.
7. Cache Invalidation (The Hardest Problem)
*“There are only two hard things in Computer Science: cache invalidation and naming things.” - Phil Karlton* If a user updates their profile name in the DB, but the old name is still in the Cache, the system is serving "stale data." Solutions:- 1. TTL (Time To Live): Assign an expiration time to the cached item (e.g., 5 minutes). It automatically deletes itself.
-
2.
Active Invalidation: When the App updates the DB, it explicitly sends a
DELETEcommand to the Cache for that specific key.
8. CDN (Content Delivery Network)
A CDN is a network of proxy servers distributed globally, designed to cache *Static Content* (Images, Videos, HTML, JavaScript) physically closer to the user. *Analogy:* If Netflix's only server is in New York, a user in Tokyo will experience massive latency trying to stream a 4K movie. A CDN places a copy of that movie on a server *inside Tokyo*. The user streams it locally with zero buffering. (Examples: Cloudflare, AWS CloudFront).9. Real-World Scenario: The Viral Tweet
*Scenario:* A user with 10 million followers tweets. Every time a follower opens their feed, the system executes a complex SQLJOIN to assemble the tweet, user profile, and like count. 10 million SQL queries execute simultaneously. The database melts.
*The Fix (Redis):* When the celebrity tweets, the backend actively pushes the fully assembled JSON payload of that tweet into Redis (Write-Through). When the 10 million followers load their feed, the app pulls the payload directly from RAM in 1 millisecond. Zero queries hit the database. The system survives.
10. Mini Project: Cache a User Profile
Write the pseudo-code for agetUserProfile(userId) API using the Cache-Aside strategy:
javascript
11. Common Mistakes
- Caching everything: RAM is expensive. Do not cache data that is rarely accessed.
- Ignoring the Thundering Herd Problem: If a popular cached item expires (TTL ends), 10,000 requests might hit the database simultaneously to regenerate it before the new cache is set. (Fix: Use mutex locks).
12. Best Practices
- Eviction Policies: What happens when Redis runs out of RAM? Configure an eviction policy like LRU (Least Recently Used), which automatically deletes the data that hasn't been requested in the longest time to make room for new data.
13. MCQs
Question 1
Why is caching considered essential for highly scalable system designs?
Question 2
What is the fundamental difference between a Database and a Distributed Cache like Redis?
Question 3
Under the "Cache-Aside" (Lazy Loading) strategy, what happens when a "Cache Miss" occurs?
Question 4
What is the danger of using a "Write-Back" caching strategy (writing only to the cache and syncing to the DB later)?
Question 5
What is the problem of "Cache Invalidation"?
Question 6
What does assigning a TTL (Time To Live) to a cached item achieve?
Question 7
What is a CDN (Content Delivery Network)?
Question 8
When configuring a Redis cache, what does the "LRU" (Least Recently Used) eviction policy do?
Question 9
In a system design interview, why is Redis typically chosen over Memcached?
Question 10
What is the "Thundering Herd" problem in caching?
14. Interview Questions
- Q: "We have a news website. The homepage queries the top 10 articles from the database. Traffic is spiking and the DB is at 90% CPU. Walk me through exactly how you would implement a caching layer to fix this."
15. FAQs
- Q: Should I cache API responses or database queries?