patternModeratepending
Cache invalidation strategies — when to expire, when to purge
Viewed 0 times
cache invalidationTTLwrite-throughcache-asidestale cacheversioned keys
nodejspythonlinux
Problem
Application serves stale data from cache. Users complain they don't see their updates. Manual cache clearing is needed after every deployment or data change.
Solution
Choose the right strategy: (1) TTL-based: set expiration time. Simple but stale until expiry. Good for data that changes infrequently. (2) Write-through: update cache when updating the database. Consistent but more complex writes. (3) Cache-aside (lazy loading): read from cache, miss goes to DB, populate cache. Most common for read-heavy workloads. (4) Event-driven invalidation: publish an event when data changes, subscribers invalidate their cache. Best for distributed systems. (5) Versioned keys: include a version or hash in the cache key — new version = new key, old cache naturally expires. (6) Never cache user-specific data in shared caches without proper key scoping.
Why
Cache invalidation is famously one of the two hard problems in CS. The difficulty is maintaining consistency between the cache and the source of truth across concurrent operations and distributed systems.
Revisions (0)
No revisions yet.