VEL ORENDISVel Orendis
The Observatory / CachingQuest: The Stale Cache
0 StandingLV 10 XP

The Stale Cache

Something is wrong with the prices. Watch one copy age.

Lumen Market's shoppers keep paying old prices. Find out why, and stop it.

A cache keeps a copy of an answer close to the reader, so most reads skip the database. The copy starts ageing the moment it is stored.1

At Lumen Market, the price of a litre of oat milk changes every six minutes, and shoppers ask for it once a minute. The cache keeps each copy for its time to live, the TTL, and then deletes it.2 If the price changes before the copy expires, shoppers pay the old price.

Watch one copy age while the price moves.

From the references

  1. 1. The System Design Primer, Cache: When to update the cache (cache-aside, write-through). Donne Martin, CC BY 4.0, commit ae9bbd7. Read and rewritten in our words.
  2. 2. Valkey documentation, commands/expire.md: Keys with an expire; How Valkey reclaims expired keys. Valkey project, CC BY-SA 4.0, commit 3298309. Read and rewritten in our words.

Written for this lesson

Unnumbered explanations, the simulation and every number it shows were written for this lesson.

One cached priceA shopper reads a price through the cache; the database holds the real price. Parts: Shopper (person or client), Cache (cache), Database (database). Shopper sends a request to Cache and waits for the answer. Cache sends a request to Database and waits for the answer. Cache is fresh, matching the source.Shopperreads once a minuteCachecopy 3.40, 0 min oldDatabaseprice 3.40

Minute 0. A shopper misses the cache, so the price comes from the database: 3.40. The cache keeps a copy.

0

When you have watched the copy go stale, open the live cache.