Chapter 15: Performance and cost

Cache invalidation

Also called Stale read, TTL

Getting rid of a stored copy when the real answer changes. Do it too late and people read old data, which is called a stale read. A time limit on each copy, or TTL, is the blunt way: it is simple, and wrong until it runs out.

Tests rarely catch this, because they read a value once and never check again after a write. So an agent's cache can pass everything and still show someone the old price for an hour. Whoever adds a cache has to say what happens on every write path, including the ones they didn't read.

Say it to your agent

“Add a test: read the price, update it, read it again, and assert the second read returns the new price. List every code path that writes to prices, and show me each one clearing the cache. Keep a TTL of 5 minutes as a backstop.”

The workshop

Name a word, or describe what you mean in everyday language.