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
Version the URL, not the cache
/app.v42.jsand/app.v43.jsare different resources. No invalidation needed.Tag by deployment
Tag every asset with
release:42. Purge by tag, not by URL.Write-then-invalidate, not invalidate-then-write
Invalidate after the write commits. Otherwise a concurrent read may repopulate with the old value.
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
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.jsis a new resource; the old one is never requested again.
Connections
Fielding, R., Nottingham, M. & Reschke, J. (2022). RFC 9111: HTTP Caching. https://www.rfc-editor.org/rfc/rfc9111.html
Vogels, W. (2008). Eventually Consistent. https://doi.org/10.1145/1466443.1466448
Kleppmann, M. (2017). Designing Data-Intensive Applications. https://dataintensive.net/
Etymology
Origin: borrowed
in(prefix): Latin, "not"valid(adjective): from Latin validus, "strong"
Semantic network
● broader ● narrower ● related
Used in the garden
- Changelog (4×)
- Architecting API Dominance: Design a REST API That Scales From Day One (2×)
- The Zero-Downtime WordPress Migration Blueprint for High-Stakes Sites (2×)
Sources
{"Fielding, R., Nottingham, M. & Reschke, J. (2022). RFC 9111":"HTTP Caching. https:\/\/www.rfc-editor.org\/rfc\/rfc9111.html"}
Vogels, W. (2008). Eventually Consistent. https://doi.org/10.1145/1466443.1466448
Kleppmann, M. (2017). Designing Data-Intensive Applications. https://dataintensive.net/