Cache Invalidation Strategies
Learn how to keep cached data fresh and consistent using TTLs, write-through, write-behind, and event-driven invalidation in Redis.
Why Invalidation Matters
Caching speeds up reads, but a cache that serves stale data can be worse than no cache at all. Cache invalidation is the discipline of removing or refreshing entries when the underlying source of truth changes.
It is famously hard: there are only two hard things in computer science, and one of them is cache invalidation.
TTL-Based Expiry
The simplest strategy is time-to-live. You accept that data may be stale for at most N seconds.
SET key value EX 60expires after 60 secondsEXPIRE key 60sets a TTL on an existing keyTTL keyshows remaining seconds
SET user:42:profile "...json..." EX 60
TTL user:42:profile
EXPIRE user:42:profile 120All lessons in this course
- Advanced Cache Patterns
- Session Management with Redis
- Rate Limiting and Anti-Patterns
- Cache Invalidation Strategies