ConceptDifficulty: Beginner2 min

Cache Invalidation: TTL, Delete on Write and Write-Through

Every cached value is a bet on when the original will change.

Cache invalidation is how a cached copy is kept from drifting too far from the original. There are three common ways: let the copy expire after a fixed time, delete it whenever the original is written, or update it in the same step as the original. Each allows a different amount of staleness, the time a reader can see an old value.

Three ways to keep a cached copy fresh
StrategyWhen the copy is refreshedHow stale a reader can see it
TTL (time to live)When it expires, on the next readUp to the full TTL
Delete on writeOn the first read after a writeClose to none; a brief race is possible
Write-throughIn the same step as the writeClose to none; the write is slower

TTL sets a lifetime on each entry, such as 300 seconds. It needs no link between the code that writes the data and the cache, which makes it the simplest, and it guarantees that no copy is older than the TTL.

Delete on write removes the entry whenever the code changes the original, so the next read misses and fetches the new value. It keeps copies fresh, but every place that writes the data must also delete the entry; one forgotten path leaves a stale copy until it expires.

Write-through writes the new value to the database and the cache together, so the cache is never behind. Each write costs two stores instead of one, and values that are written but never read still fill the cache.

A TTL alone

Use it when

  • A value may be a little out of date, such as a weather forecast or a list of trending posts
  • The code that writes the data cannot reach the cache

Avoid it when

  • A reader must see a change at once, such as a price or a stock level
Delete on write or write-through

Use it when

  • A reader must see a change as soon as it is made
  • The writes go through code you control

Avoid it when

  • Writes come from many places you cannot all change

Teams commonly pair them: delete on write for freshness, with a TTL as a backstop for the write path somebody forgets.

So choose the strategy by how long a reader may see an old value: minutes allow a TTL, and none at all needs the write to reach the cache.