Part 4 of 6 · 3 min read
Update the entry, or delete it
What to do with a cached copy when the data behind it changes.
Think of a notice board with a printed menu. When the soup changes, someone can pin up a new menu, or take the old one down and wait for the next customer to ask. Pinning a new menu seems quicker, but two people changing the menu at once can leave the wrong one up.
When data changes in the database under cache-aside, the cached copy is now wrong. There are two choices:
- Update the cache entry with the new value.
- Invalidate (delete) the cache entry, so the next read misses and loads the fresh value.
Cache invalidation
Cache invalidation removes or marks as out of date a cached copy because the source has changed.
Example: after a price update in the database, the application runs cache.delete("price:91"). The next read reloads the new price.
Deleting is usually safer. It never writes a value computed by one thread over a value computed by another. A worked example of the problem with updating:
Two writers change the same key. Writer A sets the price to 10, and writer B sets it to 12. Both write the database first, then the cache.
- A writes 10 to the database.
- B writes 12 to the database.
- B writes 12 to the cache.
- A writes 10 to the cache (it was delayed).
- The database holds 12 and the cache holds 10. They stay different until the entry expires.
With delete, both writers end by deleting the key. The next read loads 12 from the database. The delay of writer A cannot leave a wrong value behind.
Deleting has its own race, which is rarer. A reader misses and reads the old value from the database. Before the reader stores it, a writer updates the database and deletes the key. The reader then stores the old value. Steps:
- Reader: cache miss. Reads price 10 from the database.
- Writer: sets the price to 12 in the database.
- Writer: deletes the cache key (nothing to delete).
- Reader: stores 10 in the cache.
- The cache holds 10 until the entry expires. The database holds 12.
The chance is small, since the reader's database read and cache write would have to straddle the writer's two steps. A time to live puts a ceiling on how long the wrong value stays. This is one reason to always set an expiry (module 3). The book "System Design Interview" notes that the cache and the data store are not changed in a single transaction, which is the root of such races, and that keeping them consistent across regions is hard.
The order of steps for a write under cache-aside:
- Write the database.
- Delete the cache key.
If the delete fails (the cache is down), the old value stays until the TTL ends. A retry queue for failed deletes narrows the window. Deleting first and then writing the database is worse: a reader can load the old value between the two steps and store it.
| Choice | Strength | Weakness |
|---|---|---|
| Update on write | Next read hits at once | Races leave wrong values; extra work for keys never read |
| Delete on write | Simple and safe | Next read misses and pays the database cost |
Check yourself:
- Why can a delayed writer leave the wrong value in the cache when entries are updated, but not when they are deleted?
- In what order should the database write and the cache delete happen?