liveBook · Manning

Cache invalidation

As explained in Fast by Default

A cache invalidation strategy defines when and how cached data is marked stale or removed after underlying data changes.

Cache invalidation decides when cached values stop representing their underlying data. It is a performance and correctness trade-off: caching shields services from repeated reads and traffic spikes, but an outdated value can persist after a write. The basic failure pattern is deceptively quiet: a cache works at small scale, then stale data, unbounded memory use, or mismatched derived values become difficult to diagnose as traffic and data grow. section 4.5.3 section 5.3.1

Treat expiration as a safety net, not merely a performance setting. A TTL bounds the time a failed explicit invalidation can leave data stale. Set it according to the acceptable inconsistency window: predictable data can use time-based expiration, while highly changeable or consistency-sensitive data may need a short TTL, explicit invalidation, or no cache at all. Size limits and eviction policies are equally necessary so a cache remains bounded as datasets grow. section 4.5.3 section 5.3.1

Choose an approach based on how data changes and where cached copies live:

  • Use time-based expiration for data with predictable updates; it refreshes stale entries eventually even when an invalidation is missed. section 5.3.1 section 4.5.3
  • Use event-driven invalidation when all writes are controlled and can publish invalidation events after source data changes. section 5.3.1
  • Prefer no cache when consistency matters more than latency, rather than accepting an unacceptable stale-data window. section 4.5.3
  • Use longer cache times with explicit invalidation for content with known update patterns; use short cache times or no caching for unpredictable changes. section 5.3.2
  • Use versioned URLs for immutable asset versions, deploying a new filename instead of changing content in place. section 5.3.2

Distributed and edge caches make invalidation an architectural concern. A profile update may require removing entries across dozens of servers; one failed purge can show different users different versions. At edge scale, a single URL may exist at hundreds of locations. Design updates around those propagation limits, and explicitly decide whether bounded staleness is acceptable rather than pursuing perfect consistency by default. section 4.5.3 section 5.3.2

Read-fill race during a write

A cache can be refilled on a read while an invalidation from a concurrent write is in flight. If those operations arrive out of order, an old value can be stored after the invalidation and remain stale indefinitely. Rare interleavings, transient failures, and error-handling paths make this class of bug hard to reproduce, which is why increasingly elaborate consistency logic can create its own risk. section 5.3.1

Operate invalidation as part of scale readiness. Verify that every cache has expiration and size limits, determine how the application behaves if the cache fails, and document consistency trade-offs for distributed caching. Test common database queries against projected data volumes as well: a cache should degrade gracefully without causing database load to collapse the application when entries expire, are purged, or become unavailable. section 4.5.6 section 4.5.3

Breaking-news edge purge

Dynamic HTML can be cached at a CDN and purged when major news changes. The Guardian uses Fastly instant purges to remove outdated edge content in 150ms across locations, replacing it without overwhelming the origin during unpredictable traffic spikes. This suits urgent content updates; it does not imply that every personalized API response should be cached for the same duration. section 5.3.2

How Fast by Default covers it

How deep it goes
Explains TTLs, event-driven invalidation, distributed propagation, edge purges, and read-fill races without prescribing a universal implementation.
Examples
Uses profile updates across servers, concurrent cache fills and writes, breaking-news purges, and versioned asset filenames.
What you can do after
Apply time-based expiration, event-driven invalidation, cache bypass, or versioned URLs according to update patterns and acceptable staleness.
How it connects
Connects caching to TTLs, eviction policies, eventual consistency, Redis Cluster, Memcached, CDNs, and versioned URLs.

Also explained in

Compare how 4 books explain cache invalidation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Fast by Default. The section links point to the parts of the book it draws on.