Start with the idea
A TTL trades staleness for simplicity, explicit invalidation trades simplicity for correctness, and write-through trades write latency for both. The failure modes are the interesting part: a stampede when a hot key expires, and unbounded growth without an eviction policy. Naming which staleness your product can tolerate is the real answer.
A cache is a second copy of data, so every design must answer when that copy is valid. A time-to-live is easy to operate but permits bounded staleness. Explicit invalidation can reduce staleness but must account for every writer and failure path. Write-through updates cache and source together from the application's point of view, but partial failure and ordering still need a policy. The right choice depends on whether a stale value is harmless, confusing, or financially dangerous.
Suppose a product page is read thousands of times but price changes rarely.
Keep learning, for free
The rest of this lesson is waiting for you.
Create a free account to read the complete explanation, see the infographic, explore the code, and try the exercises. All 742 lessons and algorithm steps are included.
No payment details. Your account opens the complete Academy.