Cache Coherence Across Layers
What you'll learn: How to keep multiple cache layers in sync when the same data lives in different places.
In the previous lessons, you learned that modern systems use multiple cache layers—browser caches, application-level caches, distributed caches like Redis, database query caches, CDNs, and even CPU caches. But here's the problem: what happens when the original data changes?
The Core Problem
Imagine you update a user's profile in the database. Now that same profile might be cached in:
- Your Redis distributed cache
- Your application's in-memory cache
- A CDN serving the profile image
- The database's own buffer pool
If you don't manage updates properly, users see stale (outdated) data. One user might see their old username while another sees the new one, depending on which cache layer serves their request.
Managing Cache Coherence
Propagation strategies determine how updates flow through cache layers:
- Invalidation: When data changes, mark cached copies as invalid (delete them). The next request fetches fresh data.
- Write-through: Update the cache immediately when updating the database.
- Time-based expiry (TTL): Accept eventual consistency—caches expire after a set time, guaranteeing freshness within that window.
The analogy: Think of a company org chart displayed on multiple office bulletin boards. When someone gets promoted, you can either (1) tear down all the old charts immediately, (2) replace them all right away, or (3) let them expire naturally and replace them during your weekly update routine.
Coordination Challenges
The more cache layers you have, the harder coordination becomes. You need to decide which layers to invalidate and in what order to avoid race conditions.
Key Takeaway: Cache coherence requires choosing an update propagation strategy (invalidation, write-through, or TTL) and carefully coordinating changes across all cache layers to prevent serving stale data.