Performance glossaryCache invalidation is removing or refreshing cached copies of content when the original changes, so visitors are not served out-of-date pages.
Cache invalidation is how a cache learns that its stored copy is out of date. Every cache keeps copies of content so it does not have to rebuild or refetch it, and every copy goes stale the moment the original changes. Invalidation decides when a copy is thrown away or refreshed, so visitors get the new version.
It is famously awkward. Phil Karlton's quip, "There are only two hard things in Computer Science: cache invalidation and naming things," has lasted because it rings true. Invalidate too eagerly and the cache stops helping; too lazily and visitors see old prices, old menus, or yesterday's news.
The main methods
Expiry (time to live)
Each cached item gets a lifetime, its TTL, and is refetched or revalidated once that time is up. HTTP caches read the lifetime from the Cache-Control header, as in Cache-Control: max-age=3600. Expiry is simple and needs no coordination, but content can be stale for up to the full TTL.
Purging
The cache is told directly to delete an item, a group of items, or everything. WordPress caching plugins purge a post's cached page, along with related pages such as the home page and archives, when the post is published or updated. CDNs offer purging by URL or in bulk through their dashboards and APIs.
Revalidation
The cache keeps its copy but checks with the origin before using it, sending the copy's ETag or Last-Modified value. If nothing has changed, the server answers 304 Not Modified without resending the content. The stale-while-revalidate directive lets a cache serve the old copy while it fetches a fresh one in the background.
Versioned file names
Instead of invalidating a file, the new version gets a new URL, a technique called cache busting. WordPress does this for scripts and stylesheets by adding a ?ver= query string, and build tools put a content hash in the file name, such as app.3f9c2a.js. Versioned files can safely be cached for a year, because any change arrives at a new address.
Tags (surrogate keys)
Some caches, including Varnish and several CDNs, can label responses with tags and invalidate everything with a given tag at once: for example, every page that shows a particular product.
Cache invalidation on a WordPress site
A typical WordPress site has several cache layers, and each one has to be invalidated separately:
- Object cache (Redis or Memcached), which stores database results. WordPress core clears most of its own entries when data changes.
- Page cache, from a plugin or the host, which stores finished HTML.
- CDN cache at the edge.
- Browser cache on each visitor's device, which can only be controlled in advance, through headers and versioned URLs.
When a change "isn't showing", the usual cause is a layer that was missed: the plugin cleared its page cache but the CDN still holds the old HTML, or a stylesheet was edited without its version number changing. Clearing the layers from the inside out (object cache, page cache, CDN, then a hard refresh in the browser) settles it.
Common mistakes
- Letting browsers cache HTML for a long time. Browser caches cannot be purged remotely, so HTML should have a short lifetime or none, while versioned assets can have a long one.
- Purging the entire CDN after every small edit, which sends all traffic back to the origin at once.
- Forgetting the pages that list the changed content, such as category archives, the home page, and feeds.
- Caching personalized pages, such as a WooCommerce cart or account page. No invalidation strategy makes that safe; those pages should bypass the cache entirely.
For how the layers work, see caching; for edge caching, see CDN.
Source: MDN: HTTP caching.