Topic 085 / Core
Cache invalidation and TTL
Chapter 6.1 · Caching Strategies
What and why
A cache entry can become wrong the instant the thing it copied changes anywhere, and TTL alone can't detect that it happened — it only bounds how long the wrong value survives. Invalidation generalizes cache-aside's delete-on-write (→ 83): whenever the source changes, remove or refresh every cache entry derived from it. TTL (time-to-live) is the safety net under all of it — an upper bound on how long any stale entry, however it got stale, is allowed to live. Builds on: → 84 (write-through and write-back).
Diagram
| Strategy | Stale window bound | Cost | Breaks when |
|---|---|---|---|
| TTL only | bounded by the TTL | cheap — no code per writer | a write lands just before expiry; stale until the TTL runs out |
| Explicit on write | near zero, same write path | code at every writer | a writer forgets to invalidate, or writes outside the known path |
| Event-driven (CDC → 119) | near zero, decoupled from writer code | infrastructure (a change stream) | lag in the event pipeline becomes the staleness window |
How it works
- Pick a TTL as the outer bound: if nothing else works, the entry is wrong for at most the TTL.
- On every write path that changes the source, invalidate (delete or update) every cache entry derived from it — cache-aside's write path (→ 83), generalized to every key a record feeds, not just one.
- For data changed by code you don't own (another service, a batch job, a direct edit), rely on event-driven invalidation (→ 119) or accept TTL alone and size it accordingly.
- Two failure modes to design against: missing invalidation (a writer forgets) and over-invalidation (deleting too broadly, risking a stampede, → 88).
- Shorter TTL means fresher data but a lower hit ratio and more database load; longer TTL is the opposite — the choice is a direct trade against hit ratio (→ 82).
Practical example
(numbers illustrative). A product's price changes about once a day; reads happen continuously at 10,000/day, with TTL = 5 minutes as the safety net. If an explicit invalidation is missed on one code path (say, a bulk import script), the TTL bounds the exposure to at most 10,000 × (5 ÷ 1,440) ≈ 35 reads seeing the stale price before the TTL forces a refresh — versus up to all 10,000 reads for the rest of the day with no TTL at all.
When to use it
- Use when: TTL as the baseline, always; add explicit invalidation when writes are known and few, and staleness has real cost.
- Avoid relying on TTL alone when: staleness has real cost (pricing, availability) and the write path is known — explicit invalidation is cheap there.
- Failure scenarios: invalidation storms deleting many keys at once and triggering a stampede (→ 88); invalidating before the database write commits, leaving the exact refill race topic 83 describes; forgetting to invalidate on soft-deletes or bulk operations.
- Simpler alternative: no cache for a record type whose staleness tolerance is near zero and whose traffic doesn't justify the invalidation engineering — read it straight from the database.
Practice
(process/pattern kind, 20 min, lab: Postgres + Redis).
- Add explicit invalidation to a write path from topic 84's exercise; confirm a read immediately after a write never returns the old value.
- Remove the invalidation call (simulate the "forgotten path" bug) with TTL = 5 s; count how many reads see the stale value in that window, then repeat with TTL = 60 s and compare the counts.
- Classify three scenarios — a price change, a user display-name change, a nightly batch recompute — as TTL-only, explicit, or event-driven, and justify each.
Done when the TTL-only staleness window is measured directly and matches the configured TTL, not assumed.
Shared lab setupKey takeaway
TTL is the safety net every cache needs regardless of how good explicit invalidation is; explicit invalidation keeps staleness near zero on the paths you control. Done when you can: for any write path, name the matching invalidation strategy and its maximum staleness. Used next in → 86 (eviction, the other way entries leave a cache), → 88 (stampede, invalidation's sharp edge).
References
None — this topic makes no external claims.
Source directory