Sub topic 11
Publishing updates: invalidations versus versioned file names
When you change a file in S3, CloudFront may keep serving the old copy from its edge locations until the cached copy expires. This chapter covers the two ways to get new content to viewers: invalidating cached paths, and giving changed files new names. It also covers how to combine them in a deploy.
This chapter assumes you know the following:
- Basic S3 concepts: buckets, objects, and keys.
- How HTTP caching works, including
Cache-Controlandmax-age. - How to run AWS CLI commands.
- How a CloudFront distribution, cache behavior, and cache policy fit together, as covered in earlier chapters.
Why updates do not appear immediately
An edge location stores a copy of an object for as long as its TTL (time to live) allows. The TTL comes from your cache policy and the Cache-Control header that S3 returns with the object. Overwriting the object in S3 does not tell any edge location about the change. Each edge keeps serving its cached copy until that copy expires, or until you remove it.
There are two layers of caching to think about:
- CloudFront edge caches. You can control these with invalidations.
- Browser caches. You cannot invalidate these. A browser that was told
max-age=31536000will not ask CloudFront again for a year, no matter what you do in CloudFront.
That second layer is the main reason versioned file names matter. An invalidation only helps with the first layer.
Option 1: Invalidations
An invalidation tells CloudFront to treat matching cached objects as expired. The next request for that path goes back to S3 for a fresh copy.
aws cloudfront create-invalidation --distribution-id EDFDVBD6EXAMPLE --paths "/index.html" "/css/*"The command returns an invalidation ID and a status of InProgress. It usually completes within a few minutes. To wait for it:
aws cloudfront wait invalidation-completed --distribution-id EDFDVBD6EXAMPLE --id I2J0I21EXAMPLEPath rules
- Paths must start with
/. - A path matches the URL path exactly, so
/index.htmldoes not match/. If viewers request/and CloudFront serves the default root object, invalidate/or/index.htmlaccording to how the viewer URL is written. Invalidating both is the safe choice. - A wildcard
*at the end matches everything under that prefix, for example/css/*./*matches everything. - Paths are case sensitive.
- Query strings are part of the cache key only if your cache policy forwards them. If a query string is in the cache key, the invalidation path must include it, or you must use a wildcard such as
/page*.
Cost and limits
AWS gives you a monthly allowance of free invalidation paths per account. Paths beyond that allowance are charged per path. The wildcard /* counts as one path, so a single /* is often cheaper than listing hundreds of files. Check the current CloudFront pricing page for the exact allowance and rate, because these numbers change.
There is also a limit on how many invalidations can be in progress at the same time, and on how many wildcard invalidations can be active. A deploy script that fires an invalidation on every commit can hit these limits.
Problems with invalidations
- They do nothing for browser caches.
- They are not instant, so there is a window where some edges serve old content and others serve new content.
- They add cost and a rate limit when used for every file.
- Invalidating a path removes the cached copy, so the next request for each object at each edge is a cache miss. After a
/*invalidation your origin sees a burst of requests, and the cache hit ratio drops until the cache warms again.
Option 2: Versioned file names
With versioning, you never change the content of an existing URL. When a file changes, it gets a new name, and the pages that reference it are updated to use the new name. Because the URL is new, no cache anywhere has a copy, so it is fetched fresh.
There are two common styles:
| Style | Example | Notes |
|---|---|---|
| Content hash in the file name | `/assets/app.3f9a1c.js` | Most build tools (Vite, webpack, esbuild) produce this. The name changes only when the content changes. |
| Version in the path | `/v42/app.js` | Easy to reason about, but every file under the new prefix is a new URL, even if it did not change. |
A content hash is the better default. Unchanged files keep the same name, stay cached, and cost nothing to redeploy.
Because a versioned URL never changes its content, you can tell every cache to keep it for a very long time. Upload these files with a long Cache-Control lifetime:
aws s3 cp ./dist s3://amzn-s3-demo-bucket/assets/ --recursive --cache-control "public,max-age=31536000,immutable"The three parts of this header do different jobs:
publicallows shared caches such as CloudFront to store the object.max-age=31536000sets a one-year lifetime.immutabletells browsers not to revalidate the file even on a reload.
The TTL that CloudFront actually uses is also bounded by the minimum and maximum TTL in your cache policy. The managed CachingOptimized policy has a maximum TTL of one year, so this header is honored in full.
The one file that cannot be versioned
Versioning moves the problem rather than removing it. The HTML page that references app.3f9a1c.js must itself be fetched from a stable URL such as / or /index.html, because users bookmark and type that address. So that file still needs a different strategy: a short TTL, revalidation, or an invalidation.
Upload it with a short lifetime and mandatory revalidation:
aws s3 cp ./dist/index.html s3://amzn-s3-demo-bucket/index.html --cache-control "public,max-age=60,must-revalidate" --content-type "text/html"With this header, edges and browsers keep the page for at most 60 seconds. After that they revalidate with S3 using the object's ETag, and S3 answers 304 Not Modified when nothing changed, which is cheap.
Combining both: the standard deploy pattern
Most production static sites use both techniques. Each handles the files it is suited to.
- Build the site. The build tool produces hashed asset names and an
index.htmlthat references them. - Upload the hashed assets first, with the one-year
Cache-Controlheader. - Upload
index.htmland other unversioned files last, with a shortCache-Control. - Invalidate only the unversioned paths, for example
/index.htmland/.
The order matters. If you upload index.html before the new assets exist, a viewer can load a page that points to files that return 403 or 404. Uploading assets first means the new page never references a missing file.
Keep old versioned assets in the bucket for a while after a deploy. A viewer who loaded the previous index.html a minute ago may still request the old app.<oldhash>.js. If you delete it immediately, that viewer gets an error. Removing assets older than a few days with a lifecycle rule is a reasonable compromise.
Choosing between them
| Situation | Better choice |
|---|---|
| Build output with hashed names (JS, CSS, images) | Versioned file names |
| `index.html` and other stable entry URLs | Short TTL, plus invalidation if you need it at once |
| Fixing a wrong file that is already live at a fixed URL | Invalidation |
| Content that changes many times a day at one URL | Short TTL, not invalidations |
| Content that must be removed from caches urgently | Invalidation, and also delete or replace the object in S3 |
Common errors
- Invalidated, but users still see old content. The browser cache is the usual cause. Check the
Cache-Controlheader that was served. A longmax-ageon a non-versioned URL cannot be fixed with an invalidation, and the only remedies are to wait for it to expire or to change the URL. - Invalidation path does not match. Paths are case sensitive and exact. Confirm the viewer URL, including whether it has a trailing slash or a query string in the cache key.
- Edges disagree for a few minutes. This is expected while an invalidation is in progress. Wait for the
Completedstatus. - Assets cached forever but not hashed. If you set
immutableon a file whose name does not change, you have created a file you cannot update for viewers who already have it. Apply the long header only to versioned names. - Large invalidation bills. Listing every changed file individually counts each as a path. Use a wildcard prefix, or switch to versioned names so you need to invalidate only a few paths.
- Missing
Content-Typeafter upload.aws s3 cpguesses the type from the extension. If you upload through another tool, check thatindex.htmlis served astext/html.
Exercise
A deploy uploads app.9c2e.js with max-age=31536000,immutable and uploads index.html with max-age=60. Which files need an invalidation, and why does the upload order matter?
Show answer
Only index.html (and / if viewers request the root URL) needs an invalidation, because it keeps the same URL while its content changes. app.9c2e.js has a new name, so no cache has a copy. The assets must be uploaded before index.html. Otherwise a viewer could get the new page, which references app.9c2e.js, before that file exists in S3, and the request would fail.
Exercise
A team sets Cache-Control: max-age=31536000 on /logo.png and later replaces the file at the same key. They invalidate /logo.png in CloudFront, but some users still see the old logo. Explain why, and give two ways to fix it going forward.
Show answer
The invalidation clears only the CloudFront edge caches. Browsers that already downloaded the logo keep it for up to a year and do not contact CloudFront. Going forward, either serve the logo under a versioned name such as /logo.4b7d.png and update the references to it, or give unversioned files a short max-age with must-revalidate so browsers check back often.