Cache invalidation and TTLs
How a cached copy stops being served: it ages out after a time to live, or your code removes it when the source changes. Each choice sets how stale a reader can get and what a miss costs.
What it is
Invalidation is the moment a cached copy stops counting as current: it is dropped, replaced, or marked stale so the next request checks the source before trusting it. Set aside eviction, where a full cache drops entries to make room, and a restart or flush that empties it; for freshness the two basic mechanisms are these. With a time to live (TTL), each entry carries a lifetime, and once that lifetime has passed the entry is treated as gone: the next read misses and loads the current value from the system of record. With explicit invalidation, the code that changes the data also removes or replaces the copy, so the staleness window shrinks to the moments around the write instead of running until an arbitrary clock tick. In the cache-aside pattern, an update writes the data store and then invalidates the matching cache item, and the next read reloads it; between the write and that reload a reader can miss or briefly see the old value. The pattern offers no consistency promise beyond that: a change made by another process shows up in the cache when the item is next loaded, not before.
How a TTL behaves in HTTP. A stored response is fresh while its age, the seconds since the origin generated it, is below max-age, and stale after that. A shared cache that has held a response for a day forwards it with Age: 86400, so the browser subtracts that from max-age and keeps it fresh for the remaining time. A stale response isn't thrown away: the cache may revalidate it with a conditional request (If-None-Match or If-Modified-Since), and a 304 Not Modified answer with no body makes it fresh again. no-cache lets a cache store the response but asks it to revalidate before reusing it, with history navigation such as the back button as a documented exception; no-store means don't store it at all. And a no-store header on a new response does not remove a copy already stored for that URL.
How a TTL behaves in Redis. The EXPIRE command, in Redis since 1.0.0 (its NX, XX, GT and LT options since 7.0.0), sets a timeout on a key, and after that many seconds the key is deleted. Redis reloads nothing: in a cache-aside design, the next caller that misses reads the database and stores a fresh copy. Three details catch people out. Overwriting a key with SET clears its timeout, while INCR, LPUSH and HSET, which alter the value in place, leave the timeout untouched. Calling EXPIRE again on a key that already has a timeout replaces it with the new value, which is how a sliding session window is built. And EXPIRE with a time that is zero or in the past deletes the key at once.
Purging a CDN by tag. With Cloudflare, the origin adds a Cache-Tag response header listing tags for the content, such as Cache-Tag: product-42,catalog. The edge records the tags against the cached object and strips the header before the visitor sees it. A purge by tag, from the dashboard or the API, forces a cache MISS on everything carrying that tag, so a product update can clear its page, its thumbnails and the category listing in one call without enumerating URLs. A purge sends the next requests to the origin, so a broad tag on a hot path is a self-inflicted traffic spike.
The stampede, and leases. When a hot entry expires under heavy load, many requests miss at once and each recomputes the value: a page that takes 3 seconds to render at 10 requests per second means about 30 concurrent renders the moment it expires. Three families of fix exist: a lock so one process recomputes while the others wait, return not-found, or serve the stale copy; an external process that refreshes the entry before it expires; and probabilistic early expiration, where each process independently decides to refresh a little before the deadline, with a higher chance the closer it gets. Facebook's 2013 memcache paper, as walked through in a set of MIT 6.824 lecture notes, names a related problem, the stale set: a web server stores an out-of-date value after a delete has already happened, and the cache stays wrong until the key is next written. Their fix is a lease: a miss hands the reader a token for that key, a delete on the key invalidates it, and a set carrying a stale token is ignored. The same servers hand out a lease for a key at most once every 10 seconds, so the rest of a herd retries shortly instead of all reaching the database.
When to use it
Lean on a TTL when the data changes on a schedule or by a process your code doesn't control (a nightly import, another team's service writing the same table), when the key space is large and dynamic so there is no list of keys to delete, and when a reader seeing a value a few minutes old does no harm. The TTL then does two jobs: it bounds staleness and it reclaims memory for entries nobody reads again.
Invalidate explicitly when the change comes through your own write path and the person who made it will look for it next: a profile edit, a price change, a document save. Write the system of record first and then delete the cached key, which narrows the window in which a reader can refill the cache with the old row. Keep a TTL on the entry anyway, long enough that it rarely fires, so that a delete lost to a crash or a network blip has a bounded cost instead of a permanent one. For HTTP responses, prefer a tag or a versioned URL over per-URL purges when one change touches many pages.
Serve stale while refreshing when the origin is slow or fragile and a slightly old answer beats a slow one: the first reader past the deadline triggers a reload, the rest keep getting the stale copy until it lands. This is the stale-fallback option for stampedes, and it fits leaderboards, feeds and dashboards. Our advice: say out loud how old the stale copy may get, because that number is the real trade you are making.
When not to
Don't let a cache decide a read that must be exact, such as a balance before a debit or a seat before a booking: a cached copy may be stale between the write and the next reload, and the pattern makes no promise about that gap. Send the deciding read to the system of record, and cache the display copy.
Don't reach for a very short TTL as a substitute for invalidation. Azure's guidance warns that premature expiry makes applications keep refetching and re-adding the same data; a busy key gets reloaded again and again, with the stampede risk on hot keys, while readers still see stale data for a few seconds. If freshness after a write matters, delete on write or use write-through; if it doesn't, a longer TTL is cheaper.
Don't plan to purge what sits in the user's browser. A private cache is tied to one client, so a CDN or server-side purge doesn't reach copies already stored there, and changing the response headers doesn't remove them either. For assets, change the URL when the content changes (a hash or version in the file name) and give the old one a long max-age; for an HTML page, keep max-age short or use no-cache so the browser revalidates. Also skip explicit invalidation when writes are frequent and reads rare: each write pays for a delete that few reads benefit from, and the cache adds little.
Compared
| Mechanism | When the copy leaves | Staleness bound | When the weaker option is right |
|---|---|---|---|
TTL, nothing else | When its age passes the lifetime; the next miss reloads it | The TTL | Changes come from outside your code, keys are many and dynamic, or minutes of staleness are harmless |
Delete on write (cache-aside) | Right after the database write | Near zero, unless a reader writes an old value back after the delete; then it stays stale until a TTL, eviction or the next write clears it | Your code owns the write path and the writer will read it back soon |
Purge by tag (CDN) | When the purge call lands at the edge | Until the purge takes effect at the edge, then an origin spike as edges refill | One change touches many URLs across a shared cache |
Versioned key or URL | Old copies are not asked for again; they age out unused | None for new requests | Static assets whose content changes rarely and whose URL you control |
Revalidate (ETag, 304) | The copy stays; the cache asks the origin whether it changed | One round trip | Bodies are large and change rarely, so a 304 is far cheaper than a refetch |
Serve stale while one reloads | After the reload lands | The reload time | A slow origin where an old answer beats a slow one, and a stampede would hurt |
In a picture
Step 1 of 3
A TTL bounds staleness
A read checks the cache. On a miss it loads from the database and stores the value with a lifetime. When the lifetime passes, the entry is dropped; nothing reloads it until the next read misses.