Why doesn't Amazon CloudFront cache files in the amount of time that I specified?

3 minute read
0

I want to specify the amount of time that my Amazon CloudFront distribution caches objects, so I added Cache-Control or Expires headers on the origin. However, CloudFront doesn't cache objects for the amount of time that I specified, or CloudFront doesn't cache at all.

Resolution

Confirm that the directives that you set on the Cache-Control or Expires headers don't conflict with each other. It's a best practice to use the Cache-Control max-age directive, instead of the Expires header. If you specify values for both, then CloudFront uses only the value that you set for Cache-Control max-age.

Verify that the Minimum TTL, Default TTL, and Maximum TTL that you set on the CloudFront distribution don't conflict with the Cache-Control or Expires headers.

If these cache duration settings conflict with each other, then CloudFront might not cache objects for the set time, or CloudFront might not cache at all. Avoid these examples of cache duration settings that conflict with each other:

  • The Maximum TTL is set to 5 minutes (300 seconds) and the Cache-Control max-age header is set to 1 hour (3600 seconds). In this case, CloudFront caches the object for 5 minutes instead of 1 hour.
  • The Cache-Control max-age header is set to 3 hours and the Expires header is set to 1 month. In this case, CloudFront caches the object for 3 hours instead of 1 month.
  • The Default, Minimum, and Maximum TTLs are set to 0 seconds. In this case, CloudFront always verifies that it has the most recent content from the origin.
  • The Minimum TTL is set to 60 seconds, the Default TTL is set to 300 seconds, and the Maximum TTL is set to 3600 seconds. If the origin doesn't provide cache headers, such as Cache-Control max-age or Expires, then CloudFront caches the object for the Default TTL duration.

Important: If you update an origin asset with a cache header, then CloudFront reflects the change only after CloudFront makes a new request to your origin. CloudFront makes a new request to your origin when the cached asset expires in the edge locations. Then, your distribution receives a new request for the asset. To force CloudFront to reflect the update before the cached asset is set to expire, invalidate the cache.

Related information

Object caching

Managing how long content stays in the cache (expiration)

Controlling how long CloudFront caches errors

AWS OFFICIAL
AWS OFFICIALUpdated 6 months ago