CodeOrigin All articles
Software Engineering

Every Copy Has an Owner: Designing Cache Invalidation as a Contract

CodeOrigin
Every Copy Has an Owner: Designing Cache Invalidation as a Contract

Photo: Florian Hirzinger - www.fh-ap.com, CC BY-SA 3.0, via Wikimedia Commons

A cache is easy to add and hard to own. The first request is fast, the second is faster, and six weeks later a support ticket reads: the dashboard is showing yesterday's price. At that point somebody opens a ticket called fix cache invalidation, which is roughly equivalent to opening a ticket called stop being wrong.

The reframe that makes the problem tractable is to stop treating the cache as a performance detail and start treating it as duplicated state with a contract. Every copy of data has an owner, a freshness promise, and a failure mode. Write those three things down and invalidation stops being a scavenger hunt.

Four Clauses Every Copy Must Declare

Each clause should be answerable in a sentence, by a named person.

  1. Owner. Which service is permitted to write this key? If two services can write the same key, you do not have a cache; you have two caches that happen to share an address.
  2. Freshness promise. Not fast — a number and a unit. At most 60 seconds stale, or stale until the writer publishes a new revision. A promise you cannot state is a promise you cannot test.
  3. Failure mode. When the source of truth is unreachable, does the copy serve stale data, serve nothing, or serve an error? All three are defensible answers. Serving stale data without saying so is not.
  4. Blast radius. What breaks if this key is wrong for an hour? If the honest answer is nothing anyone notices, the key probably does not deserve a cache. If the answer is the order total, the design needs to change, not the invalidation logic.

Most invalidation incidents are two people holding different versions of these clauses. The written contract is less about documentation than about surfacing the disagreement before it reaches production.

Versioned Keys Turn Invalidation Into Eviction

If a key can be addressed by version, invalidation becomes a change of address rather than a delete:

product:{id}:schema-v3
pricing:table:{epoch}

Bumping the schema version or the epoch makes every old copy unreachable in one operation, and the old entries age out on their own TTL. There is no sweep, no fan-out, no list of keys to find. The costs are real, though: you pay memory for the abandoned generation, and you must guarantee that no straggling writer can resurrect an old key. That guarantee is usually a rule that only one deploy generation may write, enforced by the writer reading the current epoch rather than carrying it in a cached configuration.

Write-Through, Write-Behind, and What Each Costs

Write-through puts the cache write on the same path as the database write. The tempting assumption is that this eliminates the staleness window. It does not, because the two writes are still not atomic. Two concurrent writers can commit to the database in one order and land in the cache in the other. The fix is not a lock but a guard: store the source row's revision number alongside the value, and only accept a cache write whose revision exceeds the stored one. A rejected write is a correct write.

The practical default for most teams is delete-on-write with a short TTL as a backstop. Deleting is idempotent, deleting the wrong thing twice is harmless, and a miss is merely slow. Treat a failed delete as a miss by marking the key dirty, not as a silent success.

Write-behind makes the cache authoritative for a window and flushes durably afterwards. It buys write throughput and pays for it in a data-loss window that must be enumerated in the contract. If the flush path drops, you have lost writes, not lost performance. Reserve this pattern for data whose loss is survivable and say so out loud in the design document.

Tests That Fail When the Copy Drifts

A cache without tests is a hope. Four tests cover most of the failure space:

These are not exotic. They are unglamorous, and they are the difference between a cache that is correct and one that is usually correct.

Observability: Knowing Before Users Do

Hit ratio is the metric everyone graphs and the one that lies most often. A cache returning confidently wrong answers can post a beautiful hit ratio. Watch these instead:

Alert on stale beyond promise and on drift above threshold, not on hit ratio. The first two describe a broken promise; the third describes a busy afternoon.

Closing the Contract

Caching is not a cleanup task you remember to do later. It is the act of publishing a second copy of your data, and copies stay correct for exactly one reason: someone wrote down who may write them, how long they may live, and what happens when the writer disappears. The TTL is a backstop, not the design. The design is the contract — and once it exists, invalidation stops being a mystery and becomes a checklist.

All Articles

Related Articles

The Join Is the Gate: Multi-Tenant Authorization from Relationships to Queries

The Join Is the Gate: Multi-Tenant Authorization from Relationships to Queries

The Ambient Lie: Configuration as an Explicit Process Boundary

The Ambient Lie: Configuration as an Explicit Process Boundary

Coexistence Is the Constraint: Migrating Schemas While Two Versions Serve Traffic

Coexistence Is the Constraint: Migrating Schemas While Two Versions Serve Traffic