Cache Invalidation: TTL vs Event-Driven
On this page
Learning Objective
Explain why cache invalidation is genuinely hard rather than merely fiddly, compare TTL-based expiry, explicit invalidation, and event-driven invalidation on correctness and operational cost, and design a key naming scheme that makes invalidation tractable instead of guesswork.
Concept Explanation
Phil Karlton line about there being only two hard things in computer science, cache invalidation and naming things, is quoted so often that the actual reason it is true tends to get lost. Invalidation is hard because a cache is a second copy of data whose authoritative version lives elsewhere, and keeping two copies in agreement across a network, with concurrency, partial failure, and no global clock, is the distributed consensus problem wearing a lightweight disguise. Every invalidation strategy is a decision about which part of that impossibility to accept: accept staleness for a bounded time, accept the risk of a missed invalidation message, or accept the cost and latency of coordination.
TTL-based expiry accepts bounded staleness. Every entry carries an expiry and is discarded when it is reached, so no invalidation message ever needs to be sent and no component needs to know which cache entries derive from which records. It is stateless, it self-heals after any failure including a missed invalidation, and it requires nothing from the write path at all. Its weakness is that the TTL is a single knob controlling two things you would rather control separately: correctness, which wants it short, and efficiency, which wants it long. It is also unconditional, so entries are discarded and recomputed on schedule even when the underlying data has not changed, which is pure waste, and synchronized expiry across many keys created at the same time produces the mass-expiry stampede covered in the next lesson.
Explicit invalidation accepts delivery risk in exchange for freshness. The code performing a write also deletes or updates the affected cache entries, so a change is reflected essentially immediately. The difficulty is not conceptual but combinatorial: the writer must know every cache key derived from the record being changed. A single product row may back a product detail entry, several category listing entries, a search result page, a sitemap fragment, and a recommendation block, and each of those may exist at several layers. Every new cached view is a new invalidation obligation on every write path that touches its inputs, and the failure mode is silent, because a forgotten invalidation produces stale data rather than an error. In distributed deployments this is compounded by the fact that a write on one node must invalidate caches held by every other node, which requires a broadcast that can be lost.
Event-driven invalidation accepts infrastructure complexity in exchange for decoupling. Rather than the writer knowing about caches, the data store emits a change stream, through change data capture, a replication log reader, or an outbox table drained to a message bus, and cache invalidators subscribe to it. Now the write path knows nothing about caching, new consumers can be added without modifying any writer, and because the stream is derived from the committed log rather than from application code, it cannot silently miss a write that bypassed the application layer, such as a manual database fix or a batch job. The cost is a real pipeline to operate, with ordering, replay, and lag as ongoing concerns, and invalidation that is fast but not instantaneous, since propagation lag is now a visible property of the system.
Two design techniques make invalidation dramatically more tractable regardless of strategy. The first is key versioning, sometimes called generational caching: instead of deleting many derived keys, embed a version or a modification timestamp in the key itself, so incrementing a single version counter atomically orphans every key derived from it and the old entries simply age out under memory pressure. This converts an N-key delete fan-out into a one-key increment, and it is the standard technique for invalidating a collection when any member changes. The second is deliberate key naming that encodes the dependency structure, so that the set of keys affected by a change is derivable rather than remembered. Most invalidation bugs are really key naming bugs, which is why the two hard things in the joke turn out to be the same thing.
| Strategy | Freshness | Failure Mode | Write Path Cost | Operational Cost |
|---|---|---|---|---|
| TTL expiry | Stale for up to the TTL | Self-healing; a missed anything corrects on expiry | None | Very low |
| Explicit invalidation | Near-immediate | Silent staleness when a key is forgotten or a broadcast is lost | Writer must know every derived key | Low infrastructure, high maintenance |
| Event-driven (CDC or outbox) | Near-immediate, bounded by pipeline lag | Consumer lag or pipeline outage delays invalidation | None; the writer is unaware | A pipeline to run, with ordering and replay |
| Key versioning | Immediate for everything derived from the version | Old entries linger until evicted, consuming memory | One atomic increment | Low |
Worked Example: Replacing a Fan-Out Delete With a Version Bump
The scenario is a product whose data appears in a dozen derived views. The first version is what most systems start with and it degrades with every feature added. The second makes the invalidation cost constant regardless of how many derived views exist.
# --- NAIVE: the writer must know every derived key, forever ---
update_product(pid, fields):
db.update("products", pid, fields)
cache.delete(f"product:{pid}")
cache.delete(f"product:{pid}:reviews")
for cat in categories_of(pid):
cache.delete(f"category:{cat}:listing:page1")
cache.delete(f"category:{cat}:listing:page2") # ...and page N?
cache.delete(f"search:results:{normalize(fields.name)}")
cdn.purge(f"/p/{pid}")
# Every new cached view added anywhere in the codebase is a new line
# that someone must remember to add HERE. Forgetting raises no error.
# --- BETTER: generational keys. One increment orphans everything. ---
# Keys embed the version of every entity they depend on.
def product_key(pid):
v = cache.get(f"ver:product:{pid}") or 1
return f"product:{pid}:v{v}"
def category_listing_key(cat, page):
cv = cache.get(f"ver:category:{cat}") or 1
return f"category:{cat}:v{cv}:listing:p{page}"
update_product(pid, fields):
db.update("products", pid, fields)
cache.incr(f"ver:product:{pid}") # orphans ALL product:pid:* keys
for cat in categories_of(pid):
cache.incr(f"ver:category:{cat}") # orphans ALL that category's pages
# Orphaned entries are never read again and are evicted by LRU pressure.
# Cost is O(number of entities), not O(number of derived views).
# --- EVENT-DRIVEN: the writer stops knowing about caches entirely ---
# The write only touches the database, inside one transaction with an outbox.
update_product(pid, fields):
with db.transaction():
db.update("products", pid, fields)
db.insert("outbox", {"entity": "product", "id": pid, "op": "update"})
# A separate relay drains the outbox to a topic; invalidators subscribe.
on_event(evt):
cache.incr(f"ver:{evt.entity}:{evt.id}")
cdn.purge_by_tag(f"{evt.entity}-{evt.id}")
# Adding a new cache layer later means adding a subscriber, and changing
# no write path at all. A manual SQL fix still emits nothing, which is why
# real CDC reading the replication log is stronger than an outbox table.Real-World Examples
- Fastly and Cloudflare both expose surrogate keys, also called cache tags, precisely because per-URL purging does not scale for content whose dependency graph is wide: an origin attaches one or more tags to a response, and a single purge-by-tag call invalidates every cached object carrying that tag across the entire edge network. This is the same idea as generational key versioning expressed at the CDN layer, and it exists because publishers found that enumerating every affected URL when one article or product changes is not maintainable.
- Ruby on Rails ships Russian doll caching built on key-based expiration, where a cache key includes the record updated_at timestamp so that touching a record makes every key derived from it unreachable rather than requiring a delete, and nesting fragments means a change to a child automatically invalidates its parents through the touch chain. The Rails documentation is explicit that the old entries are not deleted at all and are simply evicted by the cache when memory pressure demands it, which is the memory-for-simplicity trade that key versioning always makes.
Review Questions & Answers
A cache outage is loud and self-limiting: latency rises, the database sees more load, alerts fire, and the data being served remains correct throughout. A missed invalidation is silent and produces incorrect output. The system is fast, healthy by every operational metric, and confidently serving a value that no longer matches the source of truth, potentially for as long as the TTL allows or indefinitely if there is no TTL. Nothing in the monitoring stack is positioned to detect it, because correctness of cached content is not something a cache can check. It is usually discovered by a user reporting that a change did not take effect, which means the detection latency is measured in whatever the gap is between the bug and someone caring enough to report it.
Instead of naming a cache entry by what it contains, you name it by what it depends on plus the current version of that dependency, so the key for a product listing becomes something that embeds the category current version number. Invalidating means incrementing that one version counter, after which every key built from the old version number is simply never generated again by any reader, so those entries become unreachable without anyone deleting them. The cost is memory: the orphaned entries continue to occupy cache space until the eviction policy reclaims them, so a high-churn version counter can leave a meaningful amount of dead data resident. That is generally an excellent trade, because memory is cheap and elastic while the correctness risk of an enumerated delete list is neither, but it does mean the cache must be running an eviction policy rather than relying on explicit deletion to bound its size.
Any write that did not go through the application. Manual SQL executed during an incident, a data migration script, a batch job writing directly to the database, a replication-level repair, or a second service with its own write path all change the authoritative data without executing the application invalidation code, so caches keep serving the old value with no indication anything happened. Change data capture reads the database replication log, which records every committed change regardless of origin, so it observes all of them. An outbox table is a partial improvement because it is transactional with the write, but it still only captures writes made by code that knows to insert into the outbox, so it shares the blind spot for out-of-band changes.
They have bought a genuine and underrated property: an upper bound on staleness that holds automatically under every failure mode, including missed messages, pipeline outages, forgotten delete calls, and out-of-band database edits. That self-healing quality is why TTLs belong in essentially every caching design regardless of what else is used. What they have not bought is freshness for anything that must be correct sooner than 60 seconds, efficiency, since every key is recomputed on schedule whether or not the underlying data changed, or protection from the stampede that occurs when many keys created together expire together and their misses arrive simultaneously at the origin. A short uniform TTL also tends to be chosen by picking the smallest value the database can survive, which couples a correctness parameter to a capacity limit and quietly becomes a scaling ceiling.