A fast copy that can go stale

Think of it when something is slow, and someone says “just save the answer”.

In everyday life

A note on the fridge that says “milk: 2 bottles”. Reading the note is faster than opening the fridge, but once someone drinks the milk, the note is wrong until somebody updates it.

One case

A shop’s home page says “Only 3 left”. To load fast, it saves the stock count once an hour. At 14:10 the last tote bag sells. Until 15:00 the page still says “Only 3 left”, and two customers buy a tote the shop does not have.

For 55 minutes the page showed a copy that was wrong
The case, cell by cell1 cell = 5 minutes
Stock table, the real number
3100000000000
Home page, the saved copy
3333333333330
Minutes the copy was wrong
55
Totes sold that did not exist
2
The home page saves the stock count once an hour.

Every case

Browsers, phone apps and servers all keep fast copies like this one. Each copy trades freshness for speed. The useful question is never “is there a cache?” It is “how old can this copy get before it hurts someone?”

The word

Engineers call this

cacheA saved copy of an answer that is slow to get. Reading the copy is fast, but it can fall behind the real data.

A cache is a copy: fast, and sometimes wrong. Decide its time to liveHow long a saved copy is trusted before it is fetched again. Engineers write it TTL., and decide what happens when it is staleOut of date: the copy no longer matches the real data..

Where this idea leads

Skills that encode it

  • principle-prove-it-works pstack“Read the actual value, not a cached or derived representation” before you trust it.