Skip to content

Cloudfront cache duration #285

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
smknstd opened this issue May 3, 2021 · 4 comments
Closed

Cloudfront cache duration #285

smknstd opened this issue May 3, 2021 · 4 comments
Labels

Comments

@smknstd
Copy link

smknstd commented May 3, 2021

Where can I set how much time processed images should be saved in cloudfront's cache ?

Thank you for your help.

@G-Lenz
Copy link
Contributor

G-Lenz commented May 5, 2021

Hi @smknstd
If you are using the latest version, there should be a cache policy attached to the CloudFront distribution. You can set the TTL by editing the TTL settings in the cache policy.

@smknstd
Copy link
Author

smknstd commented May 5, 2021

Yes ! These are default values :

Screenshot from 2021-05-05 19-38-22

But honestly it's not easy to understand how these work. Here is my hit/miss ratio from the cloudfront's "cache statistics" tab:

Screenshot from 2021-05-05 19-47-40

Can you confirm some of my questions :

  • Cloudfront sends this header cache-control: max-age=31536000,public, is it the "Maximum TTL" value ?
  • Do processed images stay for 1 day on edge location because of the "Default TTL" value of 86400 ?
  • Is there a TTL on cloudfront's origin as well or do processed images stay there as long as the original file doesn't change on s3 ?

Thanks in advance

@G-Lenz
Copy link
Contributor

G-Lenz commented May 5, 2021

If the origin includes the cache-control header, it will cache the object for the lesser of cache-control: max-age= and Maximum TTL on the policy, unless the header is less than the minimum TTL on the policy, in which case it will use the minimum TTL on the policy. CloudFront does not send the header, it will only pass them through if they exist.

The reason you are seeing the cache-control: max-age=31536000,public is because if an object does not contain a cache-control header, the Serverless Image Handler solution adds that header via a lambda function. CloudFront will then cache the object for the lesser of the max-age or the Maximum TTL value. The browser however will cache the image for the max-age. In this case, since the header and the TTL are the same, it will cache it on CloudFront for that 31536000 seconds or 365 days. If you lowered the Maximum TTL to 86400 for example, CloudFront would then cache the image for 1 day, since the Maximum TTL is lower than the header (however the browser will still have it cached for 365 days due to the header).

There is no TTL on the CloudFront origin, however you can add cache-control headers to your objects in S3. This will bypass the solution adding the cache-control: max-age=31536000,public header and instead use the existing cache-control header on the object. The same rules still apply as to how long it is cached to CloudFront.

You can see a breakdown of how objects are cached in CloudFront in the CloudFront Developer Guide: Managing Cache Expiration

@smknstd
Copy link
Author

smknstd commented May 7, 2021

wow this is great explanation ! I didn't expect so much details. Thanx a lot, it is really helpful.

@smknstd smknstd closed this as completed May 7, 2021
@smknstd smknstd changed the title Edge location cache duration Cloudfront cache duration May 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants