How does caching work in CloudFront, and how do you control how long content stays cached?
Short answer
CloudFront caches content at edge locations based on cache behaviors you configure, including a time to live value that controls how long an object is kept before CloudFront checks the origin again for a fresh copy, and you can also control caching using HTTP headers like Cache…
Advertisement
AModel answer
CloudFront caches content at edge locations based on cache behaviors you configure, including a time to live value that controls how long an object is kept before CloudFront checks the origin again for a fresh copy, and you can also control caching using HTTP headers like Cache Control sent directly from your origin server.
Example
aws cloudfront create-invalidation --distribution-id ABC123 --paths '/images/*'
Real-world example
An e commerce site sets a long cache duration for rarely changing product images but a very short cache duration for its frequently updated pricing page, balancing performance with data freshness.
Likely follow-ups
- What is a cache invalidation and when should you use one?
- How do query strings affect CloudFront caching behavior?
Related topics