Invalidation races
On This Lesson 4 topics
This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.
In this lesson
find a race that leaves stale data and fix it.
Planned topics
- Race: reader A misses and reads the old row; writer B updates the row and deletes the key; A then writes the old value to the cache. Stale until TTL.
- Fix 1: delete after write (database first), plus a short TTL.
- Fix 2: versioning: store the row version with the value; only overwrite if the version is newer.
- Fix 3: delayed double delete: delete again after a short pause (reduces but does not eliminate the race).
- "It is rare" is not an argument: at scale rare happens daily.
Draft outline details
Demonstration
| Step | Reader A | Writer B |
|---|---|---|
| 1 | cache miss, reads price 10 | |
| 2 | updates to 12, deletes key | |
| 3 | writes 10 to cache |
Fix: A writes only if its version (1) is newer than what is cached; the cache stores version 2 or nothing.
Check your understanding
Why does a plain delete not prevent this race?
Show answer
The late reader refills the cache with the old value after the delete.
Practice
Apply this lesson with the module's lab, exercises and quiz.
Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.