Cache Invalidation Strategies
Techniques for determining when cached data is stale and must be refreshed, including time-based expiry, event-based invalidation, and tag-based dependency tracking.
Description
Cache invalidation is famously one of the two hard problems in computer science. It encompasses the strategies and mechanisms for determining when cached data no longer reflects the source of truth and must be refreshed, evicted, or revalidated. Getting invalidation wrong means users see stale data (under-invalidation) or the cache provides no benefit because data is refetched too aggressively (over-invalidation). The right strategy depends on data volatility, consistency requirements, and the cost of cache misses.
Common invalidation strategies include: time-based expiry (TTL), where cached entries expire after a fixed duration—simple but imprecise; event-based invalidation, where mutations trigger explicit cache purges for affected entries—precise but requires tracking all dependencies; tag-based invalidation, where cache entries are tagged with logical categories and mutations invalidate all entries with matching tags (used by RTK Query, Next.js revalidateTag); and version-based invalidation, where a version number or ETag determines freshness. Stale-while-revalidate (SWR) is a hybrid approach that serves stale data immediately while revalidating in the background.
In practice, applications use layered caching strategies: browser HTTP caches use Cache-Control headers with max-age and stale-while-revalidate directives; CDN caches use surrogate keys (similar to tags) for targeted purging; application-level caches (Redis, Memcached) use TTL plus explicit invalidation on writes; and client-side data caches (React Query, Apollo) use query keys with configurable staleTime and automatic background refetching. The challenge is coordinating invalidation across all layers: a mutation must invalidate the client cache, the CDN edge cache, and any server-side caches simultaneously to prevent any layer from serving stale data.
Prompt Snippet
Implement a multi-layer cache invalidation strategy: configure React Query with staleTime of 30 seconds for volatile data and 5 minutes for reference data, using query key factories (e.g., todoKeys.list(filters), todoKeys.detail(id)) for granular invalidation. On the server, use Redis with a TTL of 60 seconds for API responses, with explicit EVAL-based pattern deletion (SCAN + UNLINK) when a write operation invalidates related keys. Set CDN cache-control to 's-maxage=300, stale-while-revalidate=60' with Surrogate-Key headers for Fastly/Cloudflare tag-based purging triggered by a post-mutation webhook. Document the invalidation contract per entity in the API spec.
Tags
Related Terms
Stale-While-Revalidate
A caching strategy that immediately returns cached (potentially stale) data to the caller while asynchronously revalidating the cache in the background.
Optimistic Updates
A UI pattern where the interface immediately reflects a user's action as if it succeeded, then reconciles with the server response asynchronously—rolling back on failure.
CQRS (Command Query Responsibility Segregation)
An architectural pattern that uses separate models for reading data (queries) and writing data (commands), allowing each to be optimized independently.
Event-Driven Architecture
An architectural style where the flow of the program is determined by events—state changes that are produced, detected, consumed, and reacted to by loosely coupled services.