Skip to content

Cache invalidation

Cache invalidation is the process of marking cached data as stale so subsequent reads fetch fresh content from the source of truth, preventing users from seeing outdated information.

Definition

What it is

Every cache eventually disagrees with its source. The source updates, the cache does not know, and the next reader gets a stale answer. Invalidation is the mechanism that reconciles the two. It can happen explicitly (the writer tells the cache "delete key X"), implicitly (a TTL expires and the entry is dropped), or lazily (the entry is marked stale but only refreshed on next read).

Scope note. "Invalidation" is a general verb that applies beyond caching: sessions are invalidated at logout, tokens at revocation, plans on policy breach, permissions on role change. This entry covers only cache invalidation — the mechanism that reconciles cached data with its source of truth.

Phil Karlton's famous quote — "there are only two hard things in computer science: cache invalidation and naming things" — survives because it is accurate. The mechanism is trivial to implement and impossibly hard to get right at scale.

Why it matters

A user updates their profile, the cache still serves the old one for hours. A product price changes, the catalog shows the previous price. A security token is revoked, the cache still grants access. None of these fail loudly. They fail quietly, in production, with no stack trace to follow.

Strategies

Strategy

Freshness

Write cost

Complexity

Best for

TTL

Stale up to N seconds

Zero

Low

Public content

Write-through

Immediate

High (sync)

Medium

Financial data

Write-behind

Near-immediate

High (async)

High

High throughput

Explicit

Immediate

Depends on fan-out

High

User-specific

Version-based

Immediate

Low

Medium

Static assets

The lifecycle of a cached value

sequenceDiagram
  participant U as User
  participant C as Cache
  participant D as Database
  U->>C: GET /profile
  C-->>U: stale
  Note over U: User updates profile
  U->>D: PUT /profile
  D->>C: invalidate /profile
  U->>C: GET /profile
  C->>D: fetch
  D-->>C: fresh
  C-->>U: fresh

How to invalidate well

Invalidation that does not lie

  1. Version the URL, not the cache

    /app.v42.js and /app.v43.js are different resources. No invalidation needed.

  2. Tag by deployment

    Tag every asset with release:42. Purge by tag, not by URL.

  3. Write-then-invalidate, not invalidate-then-write

    Invalidate after the write commits. Otherwise a concurrent read may repopulate with the old value.

  4. Accept some staleness

    If the data is not critical, TTL of 30–60 s is better than complex invalidation logic.

Common failures

Origin

Cache invalidation as a formal problem entered computing with CPU caches and memory hierarchies in the 1960s. The multi-processor systems of the 1980s (MESI protocol, 1983) formalized it as a coherence problem. The web re-encountered the same problem in the 1990s with HTTP caches and proxies.

There are only two hard things in Computer Science: cache invalidation and naming things.

— Phil Karlton, attributed, 1996
1960s · CPU cache invalidation in memory hierarchies
1983 · MESI protocol formalizes multicore coherence
1999 · HTTP/1.1 (RFC 2616) formalizes HTTP cache invalidation
2008 · Vogels "Eventually Consistent" (ACM Queue)
2022 · RFC 9111 consolidates HTTP cache invalidation

Evidence

FAQ

Cache invalidation vs general invalidation?

This entry covers cache specifically. Sessions, tokens, plans and permissions have their own invalidation mechanisms.

TTL vs explicit invalidation?

TTL is simple and safe; explicit is precise and complex. Use TTL for public content, explicit for user-specific data.

What is a thundering herd?

When a popular cache key expires, N requests try to refill it at once, hitting the origin simultaneously. Fix with locks or jitter.

Can I avoid invalidation entirely?

Yes, for static assets: version the URL. /app.v43.js is a new resource; the old one is never requested again.

Connections

Etymology

Origin: borrowed

  • in (prefix): Latin, "not"
  • valid (adjective): from Latin validus, "strong"

Semantic network

Cache invaliperformanccachingCacheTTLThunderingCDNStale data

● broader ● narrower ● related

Broader

  • performance
  • caching

Related

Used in the garden

Sources