Invalidation is the staleness trade
Every cache policy is a bet on how wrong a read may be, and the bet has to be written down.
No cache policy keeps the cache and the database in sync, because a write touches two stores and one of them lags. Each policy picks a stale window, a blast radius, and who pays for the refill. Invalidate-on-write sells a miss, update-first sells a silent error, and cache-first sells data loss if the cache dies. Choose by how bad a wrong read is, how long it lasts, and how many readers it reaches.
Invalidate-on-write deletes the key after the database accepts the write. The next read finds nothing, loads the new value from the database, and repopulates the cache with a correct copy. Ignoring the cache instead leaves the old entry in place until its TTL runs out, so the stale window equals the remaining TTL, not the full TTL, since the clock started when the entry was populated. Update-first writes the cache, then the database, which keeps reads fast but makes the cache the first thing to change. Cache-first goes further: it acknowledges after writing the cache and flushes to the database later, so the database stays behind until the flush.
The abstraction hides the gap between the two writes. If the cache accepts the value and the database write fails, or the process dies before the second write, the cache holds a value no reader can distinguish from a correct one. Requests keep returning it with full confidence, and nothing alerts. The failure stays silent until someone compares the cache against the database. Invalidation trades that for a miss, which is slower but never wrong. It also hides the herd: deleting a hot key sends every client waiting on it to the database in the same instant, so one write becomes a burst equal to the number of waiting clients. Jitter, one refill request while the rest wait or take the old value, and serving stale all exist to absorb that burst.
Decide per key class before writing code. Multiply three things: how bad a wrong read is, how long it persists, and how many readers it reaches. For authentication, money, and inventory, update the cache or invalidate and add a short TTL, and never use cache-first. For feeds, counts, and recommendations, accept a TTL window. For hot keys, prefer update over delete, because deleting forces the herd. For cold keys, invalidate, since the miss is cheap. Use cache-first only for write-heavy counters with a write-ahead log or replication behind it. Write the stale window into the contract, because it is the only part users can observe.
- misses per invalidation
- 1 per key
- herd burst
- equal to waiting clients
- deferred write window
- milliseconds
- stale window without invalidation
- remaining TTL
Each invalidation forces one refill, and the reader who triggers it waits on the database.
Every client queued behind a deleted hot key reaches the database at once, so the queue sets the burst size.
Write-back acknowledges before the database has the value, so database readers see the old value for that long.
The longest an entry stays wrong when no invalidation arrives, counted from when it was last populated.
We put a search cache in front of the fare endpoints on SNCF Connect because the morning a sales window opens does not fit in the booking database. The first design was the obvious one: key on the search parameters, store the whole result set, expire after sixty seconds. It held the traffic, and it broke the product in a way that took a quarter to notice.
The wall is that a fare is a quote, not a row. Availability moves when someone else takes the last seat, and prices move when a quota is exhausted, so a cached result set is a promise about inventory the cache cannot keep. The failure arrived as bookings refused at payment after the customer had seen a price, and as support tickets quoting fares that no longer existed.
We tried the fixes in the obvious order. Shorter TTLs made the cache useless on exactly the mornings it was built for. Invalidating on fare change needed a reverse index from fare to every query that could contain it, which is unbounded. Versioning fares and caching the version still let a customer act on a version that had just disappeared.
What worked was to stop caching the answer. The search cache holds candidate fares and marks the response as a quote with an id and a validity window. The booking endpoint revalidates that quote against inventory, and when price or availability moved it returns a price change instead of charging. The question was never how long to cache, it was what the product may promise without checking.