Cache Invalidation: Making Updates Appear
Find which cache is holding your old content and use versioned URLs, purges and short lifetimes so changes show up reliably.
Key takeaways
- Several independent caches can hold your content, and clearing one does nothing to the others.
- Putting a content hash in asset filenames makes updates immediate without any purging.
- You can purge caches you control, such as a CDN or reverse proxy, but you can't reach into visitors' browsers.
- Upload new assets before the pages that reference them, and keep the old ones for a while.
- Compare response headers from different points to find which layer is serving stale content.
When you've published a change and still see the old version, some cache between your server and the screen is holding a copy it believes is good. To fix it, find which layer that is, then either wait for the copy to expire, tell the cache to discard it, or change the address so the old copy no longer applies.
The reliable long-term answer is to design for updates before you cache anything: new filenames for new assets, careful lifetimes for pages and a purge step in your deployment.
The layers that can hold a copy
A request passes down from the top until some layer can answer it. A stale answer anywhere hides every fresh thing below it. DNS caching is a separate matter that affects which server you reach; see DNS caching and time to live.
Versioned URLs: avoid the problem
If a file's address changes whenever its contents change, no cache can serve the wrong one, because no cache has ever seen the new address.
The strongest form puts a hash in the filename:
/assets/styles.8d2f41.css
/assets/app.c07b9e.js Build tools can generate these names and rewrite references in your HTML. You can then send Cache-Control: max-age=31536000, immutable without worry. You can compute a file hash yourself with a file hash generator to see the principle: change one character and the result is completely different.
A query string such as styles.css?v=18 also works with most caches. It's simpler to add by hand, but some intermediaries can be configured to ignore query strings, and during a multi-server rollout the old server will happily return old content under the new query. Hashed filenames avoid both.
HTML pages, feeds and many API addresses must stay stable, since people link to them. For those you need lifetimes and purging.
Lifetimes you can live with
Ask how long an outdated copy would be acceptable. For a news front page, perhaps a minute. For a help article, perhaps an hour. For prices at checkout, not at all.
Short shared lifetimes are an unglamorous but robust form of invalidation: whatever goes wrong with your purge system, the content corrects itself.
stale-while-revalidate lets a cache answer immediately from a slightly old copy while it fetches a new one. Visitors stay fast, and one request pays for the refresh. The directives are described in browser caching and Cache-Control.
Matching the method to the content
Different content calls for different tools, and most sites use several at once.
| Content | Practical approach |
|---|---|
| Scripts, styles, fonts | Hashed filenames, year-long lifetime |
| Images that get replaced | New filename or version on each upload |
| Articles and product pages | Short shared lifetime plus purge on edit |
| Home page and listings | Very short lifetime, or purge by tag |
Fixed names such as robots.txt or a site icon | Modest lifetime, purge when changed |
| Account pages | Don't share them in a cache at all |
The awkward cases are files whose address is dictated by someone else: a script that other sites embed, a feed that readers poll, a document whose link was printed on packaging. Give those short lifetimes with validators, so clients check often and receive an inexpensive 304 Not Modified while nothing has changed. The mechanics are in ETags and conditional requests.
A common pattern for embeddable scripts is a tiny, briefly cached loader at the stable address that requests a long-cached, versioned file. Updating the loader switches everyone to the new version quickly without giving up caching for the bulk of the code.
Purging shared caches
CDNs and many reverse proxies let you remove stored items on demand. Common methods include:
| Method | Removes | Watch out for |
|---|---|---|
| Purge by URL | Specific addresses | Variants by query, encoding or language |
| Purge by tag or key | Everything labelled with a tag | Requires your responses to carry labels |
| Purge by prefix or pattern | A section of the site | Availability differs between products |
| Purge everything | The whole cache | A rush of misses hits your origin |
Tag-based purging suits content sites. If every page that displays product 42 is labelled product-42, one call after an edit refreshes the detail page, listings and search results together. Header names and API calls are specific to each provider.
A soft purge marks items stale instead of deleting them, so they can still be served while fresh ones are fetched, or if the origin is down.
Automate it. Have your publishing system or deployment script call the purge API, and check that the call succeeded.
Knowing what to purge
The hard part is usually not the purge call but listing everything affected. Editing one headline might change the article, the home page, a category listing, an author page, the feed and the sitemap. Write these relationships down in the publishing code, where they can be tested, instead of relying on an editor to remember.
When the relationships become too tangled to track, shorten lifetimes on the listings and purge only the main item. A listing that's a minute behind is usually acceptable, and the system stays understandable.
The browser is out of reach
Once a browser has stored a response with a long lifetime, you can't send a message to discard it. It won't even contact you until the copy goes stale.
Your options are limited:
- Change the URL that your HTML references.
- Wait for expiry.
- If the browser does make a request to your site, send the
Clear-Site-Data: "cache"header, which asks it to wipe the cache for your origin. Browser support is uneven and it's a blunt instrument.
This is why long lifetimes belong only on versioned addresses. If a visitor needs help right now, a forced reload or the steps in clearing cache and cookies will do it for that one device.
A service worker adds its own storage, which ignores your purge calls entirely. Its update logic has to be part of your release plan.
Deploying without broken pages
Updates involve several files, and visitors arrive in the middle of them. A safe order:
If you publish HTML first, someone can request a new script that doesn't exist yet, and a shared cache may even store the resulting 404. If you delete old assets immediately, people with a page already open break when it loads another piece.
Finding the guilty layer
Work from the source outwards.
- Check the content directly on the application server.
- Request through the proxy.
- Request the public URL and read the headers.
curl -sI https://www.example.com/news/
curl -sI "https://www.example.com/news/?check=1" If the second command, with an unused query string, shows new content while the first shows old, a cache keyed on the URL is likely responsible. That trick fails if your cache ignores queries. Look at Age, which grows while a shared copy is reused, and at any hit or miss header your provider adds.
To test your own server while bypassing DNS and the CDN, curl can connect a hostname to a chosen address:
curl -sI --resolve www.example.com:443:203.0.113.10 https://www.example.com/news/ If your origin only accepts connections from the CDN, run this from an allowed machine.
Application and database caches
Inside your application, the same ideas apply under different names. A cached query result can expire after a set time, be deleted when the underlying record is written, or be stored under a key that includes a version number. The versioned key mirrors hashed filenames: bump the version and old entries are simply never read again, then fall out of memory on their own.
Order the steps carefully. Write to the database first, then delete or update the cache entry. If reads go to replicas that lag behind, a request arriving just after the delete can refill the cache with the previous value. A short expiry on every entry is a sensible safety net, so that any missed invalidation heals by itself.
What can go wrong
- Variants left behind. You purged one address but not its mobile, compressed or language versions.
- Stampedes. A full purge sends all traffic to the origin at once. Purge narrowly, or use request collapsing so one fetch serves many waiting requests.
- Race conditions. A purge runs before database replicas catch up, and the cache refills with old data.
- Negative caching. A temporary error or redirect gets stored. Permanent redirects can be remembered by browsers for a long time, so test with temporary ones first.
- Hidden application caches. Object caches and compiled templates need their own expiry or reset.
Common questions
Why do I see the new version while a colleague sees the old one?
You may be reaching different edge locations, or one browser has a fresh stored copy. Compare headers from both machines.
Is turning caching off a reasonable fix?
As a temporary diagnostic, yes. As a policy, it gives up speed and makes your origin carry every request. A well configured CDN with versioned assets rarely needs it.