Skip to main content
System Design
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. 1. Client/Browser Cache: The browser stores logos and CSS files locally so it doesn't have to download them again.
  1. 2. CDN (Content Delivery Network): Geographically distributed servers caching static media globally.
  1. 3. Web Server Cache: Reverse proxies (Nginx) caching HTML pages.
  1. 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. 1. The app asks the Cache for the data.
  1. 2. Cache Hit: Data is found and returned instantly.
  1. 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.
*Pros:* Only requested data is cached (saves RAM). *Cons:* The first user to request the data experiences a delay (Cache Miss penalty).

6. Cache Writing Strategies

When data changes, how do you keep the cache updated?
  1. 1. Write-Through: App writes data to the Cache AND the Database simultaneously.
*Pros:* Cache is always consistent. *Cons:* Slower writes.
  1. 2. Write-Around: App writes directly to the Database, bypassing the cache.
*Pros:* Fast writes. *Cons:* Leads to Cache Misses on the next read.
  1. 3. Write-Back: App writes ONLY to the Cache (fast). The cache asynchronously syncs to the database later.
*Pros:* Extremely fast writes. *Cons:* Massive risk of data loss if the cache crashes before syncing.

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. 1. TTL (Time To Live): Assign an expiration time to the cached item (e.g., 5 minutes). It automatically deletes itself.
  1. 2. Active Invalidation: When the App updates the DB, it explicitly sends a DELETE command 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 SQL JOIN 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 a getUserProfile(userId) API using the Cache-Aside strategy:
javascript
123456789101112
function getUserProfile(userId) {
   // 1. Check Cache
   let user = Redis.get("user:" + userId);
   if (user != null) {
      return user; // Cache Hit
   }
   // 2. Cache Miss: Query DB
   user = Database.query("SELECT * FROM Users WHERE id = " + userId);
   // 3. Save to Cache with 1 hour TTL
   Redis.set("user:" + userId, user, TTL=3600);
   return user;
}

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?
A: Both are valid, but caching the final API JSON response (Application-level caching) is often easier and provides the fastest response time for the client.

16. Summary

Caching is the ultimate weapon against latency and database overload. By placing a distributed cache like Redis between the application and the database, you can serve data in microseconds. However, you must carefully design your eviction policies (LRU) and cache invalidation strategies (TTL, Active Invalidation) to ensure users do not see stale data. Finally, leverage CDNs to move heavy static assets to the geographical edge, closest to the user.

17. Next Chapter Recommendation

Our servers are balanced and our data is cached. Now we must define how the Client actually asks the Server for data. In Chapter 8: API Design and Communication Protocols, we will explore REST, GraphQL, WebSockets, and how to design beautiful endpoints.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·