Sub topic 9
Controlling freshness with TTLs and Cache-Control
This chapter assumes you know how a distribution, a cache behavior, and a cache policy fit together, and that you can read HTTP response headers. Here you learn how long CloudFront keeps an object before it asks S3 again.
Freshness in one sentence
An object in an edge cache is fresh until its time to live (TTL) runs out. While it is fresh, CloudFront serves it without contacting S3. After it expires, CloudFront revalidates or refetches it from the origin.
Where the TTL comes from
CloudFront combines two sources: the Cache-Control header that S3 returns with the object, and three TTL values in the cache policy.
- Minimum TTL is the shortest time CloudFront caches an object, even if the origin asks for less.
- Maximum TTL is the longest time CloudFront caches an object, even if the origin asks for more.
- Default TTL applies only when the origin sends no
Cache-Control: max-ageorExpiresheader.
The managed CachingOptimized policy uses a minimum of 1 second, a default of 86,400 seconds (24 hours), and a maximum of 31,536,000 seconds (one year).
| Origin header | Result |
|---|---|
| No `Cache-Control` or `Expires` | Default TTL is used |
| `max-age=300`, within min and max | 300 seconds |
| `max-age` below minimum TTL | Minimum TTL |
| `max-age` above maximum TTL | Maximum TTL |
| `no-cache`, `no-store`, or `private` | Cached only if minimum TTL is above 0. If the minimum is 0, CloudFront follows the header. |
s-maxage also applies to CloudFront. It overrides max-age for shared caches such as CDNs, so you can give browsers and the edge different lifetimes.
flowchart TDThe diagram below shows the decision.
Setting Cache-Control on S3 objects
S3 stores Cache-Control as object metadata and returns it on every response. Set it at upload time. This command gives fingerprinted asset files a one-year lifetime:
aws s3 cp ./dist s3://amzn-s3-demo-bucket/assets/ --recursive --cache-control "public,max-age=31536000,immutable"immutable tells browsers not to revalidate the file while it is fresh. It is safe only when the file name changes whenever the content changes.
Choosing values by content type
Match the lifetime to how often the file changes.
| Content | Suggested header | Reason |
|---|---|---|
| Fingerprinted JS, CSS, images (`app.3f9c1a.js`) | `public,max-age=31536000,immutable` | The name changes with the content, so it never goes stale. |
| `index.html` | `no-cache` or `max-age=60` | It points to the current asset names and must update quickly. |
| User-facing documents that change rarely | `max-age=3600` | A short window limits staleness. |
Note that no-cache does not mean "do not store." It means "store, but revalidate before reuse." CloudFront then sends a conditional request to S3, and S3 answers 304 Not Modified if the object is unchanged.
Common mistakes
- Long TTL on
index.html. Viewers keep seeing the old page and its old asset names. Use a short TTL or publish with an invalidation. - Forgetting the browser cache. CloudFront removes a cached copy on invalidation, but browsers keep theirs until
max-ageends. - Uploading without metadata. Objects with no
Cache-Controlget the default TTL of 24 hours under CachingOptimized, which may be longer than you want for HTML. - Expecting a lower TTL to shorten cached objects. A new TTL applies to objects fetched after the change. Objects already cached keep their old expiry until they expire or are invalidated.
Exercise
Your cache policy has minimum TTL 0, default TTL 86400, and maximum TTL 3600. An S3 object has Cache-Control: max-age=7200. How long does CloudFront cache it, and why?
Show answer
One hour (3600 seconds). The origin's 7200 seconds exceeds the maximum TTL, so CloudFront clamps the value down to the maximum.
Exercise
You upload index.html with Cache-Control: public,max-age=31536000,immutable by mistake. Why is this a problem for a site whose scripts are named app.<hash>.js?
Show answer
index.html is the file that references the current script names. With a one-year lifetime, edge caches and browsers keep serving the old page, so viewers never learn about the new hashed file names until the copy expires or you invalidate it. Re-upload it with a short lifetime such as no-cache, and invalidate /index.html.