Cache Invalidation
One of the two hard things in computer science: knowing when to throw away cached data.
1The Menu Board Analogy
Cache Invalidation: The process of removing or updating cached data when the source data changes, ensuring users don't see stale information.
2Invalidation Strategies
Time-Based (TTL)
Data expires after a set time. Simple but can serve stale data until expiry.
Event-Based
Invalidate when data changes. Requires application to know about cache.
Write-Through
Write to cache and database together. Cache always has fresh data.
Write-Behind
Write to cache immediately, sync to DB asynchronously.
3Common Patterns
Versioned Keys
user:123:v5 - increment version on update. Old versions naturally expire.
Tag-Based
Tag related cache entries. Invalidate all entries with tag 'product:123'.
Pub/Sub
Publish invalidation events. All cache nodes subscribe and clear.
Cache-Aside + TTL
Combine lazy loading with short TTL for eventual consistency.
4Challenges
5Key Takeaways
?Quiz
1. Cache expires, 1000 requests hit DB at once. This is called:
2. Best way to avoid stale data in cache after update?