← Lessons

quiz vs the machine

Platinum1800

System Design

Cache Consistency With Invalidation

Keeping cached copies from drifting from the source by removing them on change.

5 min read · advanced · beat Platinum to climb

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.

Check yourself

Answer to earn rating on the learn ladder.

1. Why is delete on write often preferred over update on write?

2. What race can leave a stale entry after invalidation?

3. What consistency level do most cache designs accept?