Step 1 · The pattern
A copy with a timer
Readers ask the cache first. When the TTL runs out the key is dropped, the next reader fetches the row from the database and stores it again. Nothing is writing, so every copy is correct.
- current data
- request
In short
Invalidate from the change log: how it works and how it breaks
A cache in front of a database holds copies, and a copy is wrong from the moment its row changes. A TTL limits how long: with nothing else, readers can see old data for the whole TTL. Shortening it helps little and costs cache hits, because every expiry is another database read. The usual next step is to delete the key in the code that writes. That only works when the writer knows which keys it touched. A conditional update changes rows nobody listed, and a script or another service may write without going through your code at all. The dependable signal is the database's own log of committed changes. A small consumer tails that log and invalidates or refreshes the key for every changed row. It sees every writer, never sees a rolled-back transaction, and shrinks staleness from the TTL to about a second.
How this comes up in interviews
"Users see out-of-date data from your cache after certain updates, even though the write code deletes the key. How would you fix it?"
Then they ask
- Why not shorten the TTL instead?
- Does invalidating from the change log make the cache strongly consistent? What can still go wrong?
- The consumer that tails the log falls behind or stops. What do readers see, and how would you know?
A strong answer covers
- A delete in write code only covers writes that pass through that code and can name their keys.
- Bulk updates by a condition, scripts and other services change rows without deleting any key.
- The database's log of committed changes sees every writer and never shows a rolled-back transaction.
- A consumer tails the log and invalidates or refreshes the key for each changed row, cutting staleness from the TTL to about a second.
- It is not strong consistency: keep a version check and a TTL as a backstop.
Points to cover, not a script. Say them in your own words and tie them to a system you have worked on.
On one page: the track cheat sheet.
Sources: Uber Engineering: How Uber serves over 40 million reads per second using an integrated cache ↗ · MySQL reference manual: the binary log ↗ · Report a problem
Diagrams are our own simulations: the timings, rates and counts in them are illustrative, not measurements. Captions are original wording.