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.
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
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 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
Try it yourself
Learn this first
Learn next
- Distributed cachingOne cache spread over several nodes: who holds which key, and what can go wrong. Intermediate
- Race conditionsTwo threads update one counter and an update vanishes, unless a lock steps in. Intermediate
- Consistency models (strong vs eventual)What a read may return after a write when the data lives on three machines. Intermediate
Spotted a mistake on this page?