Backend / Cache invalidation

Cache invalidation

How a cached copy falls out of date, and three ways to get rid of it: a timer, a delete on every write, and versioned keys. Seven steps, one price change.

Intermediate7 steps~7 min
Reader — asks for the price of product 7
Cache — holds a copy under the key price:7
Database — holds the original, the truth
Write — a command that changes the data
// THE SHORT VERSION

In 30 seconds

A cache holds copies. When the original changes, the copy does not know, and it keeps giving the old answer: it is stale. Invalidation means removing or replacing stale copies.

There are three common ways: let each copy expire after a set time, delete the copy whenever the data is written, or put a version in the key so a change creates a new entry.

It is hard because timing matters and because one fact is often copied into many places, some of which the server cannot reach.

Cache invalidation, step by step

STEPS01 / 07

The saying that cache invalidation is one of the two hard things in computer science is attributed to Phil Karlton. The race in step 6 is one of several; careful systems add locks, versions or short TTLs.

STEP 01 / 07 A copy and its original
THE MATH
cache: price:7 = $20 database: price:7 = $20
  1. Step 1: A copy and its original. A cache holds a copy of something the database knows, here the price of product 7. Readers are answered from the copy because it is fast. While the two agree, nobody can tell the difference.
  2. Step 2: A write makes the copy stale. The shop raises the price. The write goes to the database, and nothing tells the cache. The copy is now stale: it still says $20, and every reader keeps getting the old price. Cache invalidation is the job of getting rid of copies like this one.
  3. Step 3: Fix 1: let copies expire. The simplest answer is a time limit. Every copy is stored with a time to live, here 60 seconds, and is thrown away when it runs out. The next read reloads the new price. Nothing has to know about writes, but readers can see the old price for up to a minute.
  4. Step 4: Fix 2: delete on write. To avoid the wait, the code that changes the data also removes the copy. It writes the new price to the database, then deletes the key from the cache. The next read misses, reloads from the database and gets $25 straight away.
  5. Step 5: Fix 3: versioned keys. A third way never changes a copy. The key includes a version number, which each write increases. Readers look up the current version and ask for that key, so the old entry is never requested again and expires unused. Websites do this with files named like app.3f9a.css, cached for a year.
  6. Step 6: Hard part 1: timing. Delete-on-write can still go wrong when two things overlap. Reader A misses and fetches $20. Before A stores it, a writer changes the price and deletes the key. Then A stores its old $20. The cache is stale again, and no delete is coming to fix it.
  7. Step 7: Hard part 2: many copies. One fact rarely lives in one cache entry. The price is also inside a cached page, a search result, a CDN copy and browsers the server cannot reach. Every write has to find all of them, and a missed one stays wrong. So most systems delete what they can and keep a TTL as a safety net.
VALUES SO FAR
cache$20copy
database$20original
database····changed
TTL····timer
on write····explicit
key····versioned
race····bug
copies····spread

Spotted a mistake on this page?