ArchitectureSystem Design

Caching Strategies: Redis, CDN, and Resilience

TT
TopicTrick Team
Caching Strategies: Redis, CDN, and Resilience

Caching Strategies: Redis, CDN, and Resilience


1. The Cache Hierarchy

  1. Browser Cache: Saves the CSS and Images on the user's laptop.
  2. CDN (Content Delivery Network): Saves the files on a server physically close to the user (Module 182).
  3. Application Cache (Redis): Saves database results in the server's RAM.
  4. Database Cache: The internal engine of SQL using its own memory.

2. Redis: The "Speed King" of Data

Redis is a "Key-Value" store that lives in RAM.

  • A database read from a hard drive takes $10$ms.
  • A Redis read takes $0.1$ms.
  • In 2026, we use Redis for Sessions, Real-time Leaderboards, and "Materialized Views" (Module 141) of our most popular data.

3. Invalidation: The "Stale Data" Nightmare

If you cache a user's balance, and then they spend $$10$, your cache is now Wrong (Stale).

  • Write-Through: You update the database AND the cache at the same time. (Safe but slow).
  • Write-Behind (Write-Back): You update the cache and tell the database to update later. (Fast but dangerous—if the server crashes, you lose the data!).
  • TTL (Time To Live): You tell the cache: "Delete this data automatically after 10 minutes." This is the "Safety Valve" of most modern apps.

4. CDNs: Edge Computing

A CDN (like Cloudflare) doesn't just store files; it runs code.

  • In 2026, you can use "Edge Functions" (Module 174) to check a user's password or resize an image at the network edge, before the request ever reaches your main server.
  • This results in a website that feels "Instant," regardless of whether the user is in New York or New Zealand.

Frequently Asked Questions

Which Redis alternative should I use? In 2026, Dragonfly and KeyDB are high-performance alternatives to Redis that use modern multi-threading. If you reach "Google-scale," these can save you thousands of dollars in server costs.

Can I cache EVERYTHING? NO. Caching adds complexity. If you cache data that changes every second (like a stock price), the effort to keep the cache updated will actually make your system slower. Only cache data that is "Read many times, but changed few times."


Key Takeaway

Caching is the "Performance Multiplier." By mastering the hierarchy from Browser to Redis and the discipline of Cache Invalidation, you gain the ability to handle millions of users on a single server cluster. You graduate from "Building a site" to "Architecting a Global Network."

Read next: Database Sharding and Replication: Infinite Storage →


Part of the Software Architecture Hub — engineering the speed.