When the cache is wrong
After this: Spot a stale answer, delete the cached copy on a write, and keep a time to live as a safety net.
On this page · 5 sections · 3 checks
The pen costs 10. The cache holds that price with a time to live of 60 s. At 10 s the shop raises the price to 12, and the database is updated. What do the next customers see?
A copy that is out of date
The cache does not know the price changed. The database was written, but nobody touched the copy in the cache. So the next reads hit, and return 10.
A cached copy that no longer matches the database is . Work through four reads of p1 and one write, if the write leaves the cache alone:
- At 0 s: miss. The app reads 10 from the database and stores it until 60 s.
- At 10 s: the price becomes 12 in the database. The cache still holds 10.
- At 20 s: hit, returns 10. Stale.
- At 40 s: hit, returns 10. Stale again.
- At 70 s: the copy expired at 60 s, so this read misses and fetches 12.
After the write the reads return 10, 10, 12: 2 stale reads. The TTL did end the problem, but only after 50 seconds of wrong prices.
Delete the copy on every write
The fix is to tell the cache. Whenever the app writes a new value to the database, it also deletes that key from the cache. Removing cached copies that may be out of date is . The same five events, with the write deleting p1 from the cache:
- At 0 s: miss, store 10.
- At 10 s: the price becomes 12, and
p1is deleted from the cache. - At 20 s: miss, because the copy is gone. The app reads 12 and stores it.
- At 40 s and 70 s: hits, returning 12.
After the write the reads return 12, 12, 12: 0 stale reads. The delete costs a miss on the next read, at 20 s. In this run that miss takes the place of the one at 70 s: the new copy is stored at 20 s and lives until 80 s. So both runs end with 2 misses. Step through both. The Policy switch reruns the same events with the other rule.
- Cache-aside: 5 requests · Write leaves the cacheRun it with the policy set to leave the cache alone. The price changes to 12 at 10 s. Watch what the reads at 20 s and 40 s return.
- Cache-aside: 5 requests · Write deletes the key
Press Run it, then step through and watch the requests move.
In code, the write function does two things: write the database, then delete the key from the cache.
The first read misses and caches 10. set_pricesetPrice writes 12 to the database and deletes p1 from the cache, so the second read misses and returns 12. Remove the delete line and run it again: the second read hits the old copy and prints 10.
Why delete instead of update?
The app could write 12 into the cache instead of deleting the key. That looks better, because the next read would hit. But suppose two app servers change the same price close together: one to 11, then the other to 12. The database ends with 12. Their two cache updates travel separately, though, and can arrive in the other order: 12 first, then 11. The cache now holds 11, the older price, and keeps it until something removes it. A delete has no value to get wrong: the next read fetches whatever the database holds at that moment.
Keep a TTL as a safety net
Deleting on write is right almost every time, but not every time. The app can crash after writing the database and before deleting the key. Or a slow read can put an old value back:
- A read misses and fetches 10 from the database, but is slow to store it.
- Meanwhile a write sets 12 and deletes the key (there is nothing to delete yet).
- The slow read now stores 10 in the cache. It is stale, and no write is coming to delete it.
A TTL puts a limit on how long such a copy can last. So real systems use both: delete on write, and a TTL as well.
Check yourself
The cache holds p1 = 10, stored at 0 s with a time to live of 60 s. At 10 s the price changes to 12 in the database, and the write leaves the cache alone. What does a read of p1 at 20 s return?
When the price changes, the app could delete the cached copy, or write the new price into the cache. Why is deleting the usual choice?
Every write already deletes the cached copy. Why keep a time to live on the entries as well?
- Say what a stale answer is: a hit on a cached copy that no longer matches the database.
- Delete the cached copy on every write (invalidation), so that the next read fetches the new value: 12, 12, 12 instead of 10, 10, 12.
- Say why deleting is safer than updating the cached copy: a delete has no value to get wrong when two writes race.
- Keep a time to live as a safety net: it limits how long a copy that a missed delete or a slow read left behind can last.
- Name write-through, write-back and CDNs as other places the same ideas appear.