Sub topic 11
Publishing updates: invalidations versus versioned file names
This chapter assumes you have a working distribution with a private S3 origin and that you know how Cache-Control and TTLs control freshness. Once CloudFront has cached an object, changing it in S3 does not change what viewers receive until the cached copy expires or is removed. You have two ways to publish an update.
The problem: stale copies at the edge
Each edge location keeps its own copy of an object until the TTL runs out. If you overwrite index.html in S3, edges that already hold the old copy keep serving it. Browsers may also keep their own copy, based on the same Cache-Control header.
Option 1: invalidations
An invalidation tells CloudFront to drop cached copies of the paths you list. The next request for each path is a cache miss and fetches the current object from S3.
aws cloudfront create-invalidation --distribution-id EDFDVBD6EXAMPLE --paths "/index.html" "/css/*"Points to know:
- Paths are case-sensitive and start with
/. A trailing*matches everything under that prefix. - Invalidations are not instant. They usually finish within a minute or two, and you can check progress with
aws cloudfront get-invalidation. - Pricing is per path. Each account gets a monthly allowance of free paths, then each further path is charged. A wildcard counts as one path, however many objects it matches.
- An invalidation only clears CloudFront. It does not clear copies already stored in a viewer's browser.
Option 2: versioned file names
Instead of removing old copies, give every new build a new name. Put a content hash or version in the file name, such as app.3f9a1c.js. A new name is a new cache key, so no edge has it cached. Old names stay valid for anyone still using an old page.
Because a versioned file never changes, you can cache it for a very long time:
aws s3 cp ./dist s3://amzn-s3-demo-bucket/assets/ --recursive --cache-control "public,max-age=31536000,immutable"This sets a one-year lifetime. immutable tells browsers not to revalidate the file.
The entry point still needs care
Something must point to the new names. That is usually index.html, which keeps the same name on every release. Give it a short lifetime, or invalidate it on each deploy. A common pattern is:
- Upload the new hashed assets first.
- Upload the new
index.htmlthat references them. - Invalidate
/index.htmlonly.
Uploading assets first matters. If the HTML goes up before the assets, a viewer can load a page that points to files that are not in S3 yet.
Comparing the two
| Aspect | Invalidation | Versioned file names |
|---|---|---|
| Takes effect | Within minutes, per edge | Immediately for the new name |
| Browser caches | Not cleared | Bypassed by the new URL |
| Cost | Per path beyond the free allowance | None |
| Rollback | Re-upload, then invalidate again | Point the HTML back at the old name |
| Needs a build step | No | Yes, to generate hashed names |
Common errors
- Invalidating after overwriting but still seeing old content. The browser cache still holds the old file. Test in a private window, or check the
X-Cacheresponse header on the CloudFront response. - Invalidating
/*on every deploy. This works, but it empties the whole cache and sends every viewer back to S3 until it refills. - Long TTL on
index.html. Viewers keep an old page that references old assets, so updates seem to never arrive. - Deleting old versioned files immediately. Pages already open in a browser may still request them and get errors. Keep old assets for a while.
Exercise
Your site has index.html and /assets/app.3f9a1c.js. You release a new build that produces /assets/app.8d27be.js. Which files do you upload first, and which path should you invalidate?
Show answer
Upload app.8d27be.js first, then the new index.html that references it. Invalidate only /index.html. The new JavaScript file has a new name, so it was never cached and needs no invalidation. Keep the old app.3f9a1c.js in the bucket for a while so pages that are already open still work.