The consistency challenge
A cache holds a copy of data that lives in a database. When the database changes, the cached copy becomes stale. Cache invalidation is the act of removing or refreshing that copy so readers do not see outdated data.
Invalidation strategies
- Delete on write removes the key when the source changes, so the next read reloads it fresh. This is simple and avoids caching a wrong value.
- Update on write rewrites the cache with the new value, faster for reads but risky if writes race and apply out of order.
- Event driven invalidation uses a change feed so other nodes and tiers drop their copies when data changes.
Hard problems
- A race between a slow read repopulating an old value and a delete can leave a stale entry.
- In a distributed cache, every replica and every tier must hear the invalidation.
- Most designs accept brief eventual consistency rather than pay for strict synchronization.
Key idea
Invalidation keeps caches honest by deleting or updating stale copies on change, but distribution and read write races make it famously one of the hardest problems.