Skip to content

Lesson 7 of 9 · 27m

Invalidation Patterns That Survive Reality

Use TTLs, explicit deletes, versioning, events, and dependency mapping as complementary controls rather than pretending one strategy solves invalidation.

There are only two hard problems in computer science, and cache invalidation is the one that paging you at 2 a.m. The joke survives because invalidation is rarely a single operation. One write can affect detail pages, list pages, search indexes, permissions, aggregates, rendered fragments, and CDN copies. The correct answer is not 'use events.' It is layered invalidation with bounded staleness.

Start with a dependency map

For every cached representation, write what can invalidate it:

product detail key
  depends on: product row, price rule, inventory state, region

category listing key
  depends on: category row, product membership, product summaries, sort order

If you cannot enumerate dependencies, you cannot invalidate confidently. This is why broad composite pages are harder than small reusable fragments. Fragment caching increases composition work but narrows invalidation blast radius.

TTL is the floor, not the ceiling

Every cache entry needs some maximum lifetime unless it is truly immutable. TTL bounds damage when explicit invalidation fails. Choose it from business tolerance, then layer events or deletes for the common path.

redis.set(key, payload, ex=300 + random.randint(0, 30))

The jitter matters. A batch job that writes ten thousand keys at midnight should not cause ten thousand expirations at 00:05.

Explicit invalidation should be close to the write

The best invalidation signal is the transaction that knows what changed:

with db.transaction():
    product.update(price_cents=4900, version=product.version + 1)

redis.delete(f"catalog:v4:product:{product.id}:region:us")
redis.delete(f"catalog:v4:product:{product.id}:region:eu")

For a few direct keys, this is ideal. For broad derived sets, enumerating every key becomes fragile. That is where versioned dependencies or event consumers help.

Versioned dependencies avoid key enumeration

Instead of deleting every category page, bump a category version used in derived keys:

category_version = redis.incr(f"category:{category_id}:version")

Keys then include that version. Old entries become unreachable and expire naturally. This trades memory for operational safety. It also avoids fragile scans over production keyspaces. Never use KEYS * as an invalidation mechanism outside a tiny lab.

Events work when they are boring

A change-data-capture or domain-event pipeline can invalidate caches near the data layer:

{
  "type": "product.updated",
  "product_id": 1042,
  "version": 38,
  "changed_fields": ["price_cents"]
}

The consumer must be idempotent, ordered enough for your correctness needs, and observable. If events are dropped, TTL is the backstop. If events lag, users see stale state longer than intended. Measure lag like any other production dependency.

Purging HTTP caches is a different discipline

Application cache deletion does not purge browser or CDN copies. Use cache tags or surrogate keys where available, and design URLs so rollback is possible. Emergency purges should be rare, tested, and scoped. 'Purge everything' is sometimes necessary, but it converts an invalidation incident into an origin load incident.

Field notes

  • Deleting one key is not enough when the same fact appears in lists, aggregates, search, and fragments.
  • Version counters need tenant or object scoping; global counters destroy key reuse.
  • Tombstones can be necessary after deletion so stale replicas do not resurrect objects.
  • Test partial invalidation failure. The system should degrade to bounded staleness, not permanent contradiction.
  • Keep invalidation boring enough that the on-call engineer can explain it while half awake.

Was this lesson helpful?

Sign in to rate