Question

I'm using S3 to store a bunch of images and CloudFront to cache them. The problem I'm getting is that my images are not being cached correctly.

If I check the headers for an image through the CloudFront link, it will show a MISS on the first try and HIT for all subsequent tries. This is normal, but if I close my browser, go away for a few hours and try it again it will show a MISS. Therefore indicating that it's only getting cached for short period.

Here's a sample image on the cdn: http://d711ds9nsj32q.cloudfront.net/static/8e1f8567e229e24e0782ed_1e994b9dc56ba092ecd2_5.jpg

A weird behavior is that if you try to sniff the headers with web-sniffer.net you'll see a MISS every single time. Even though the cache control is public, max-age=315360000 and it's set to expire after 10 years.

What is the proper way to cache something for 10 years on cloudfront? Are there any extra headers I'm missing?

Edit: For comparison here's the same image on imgur. They use cloudflare but the behavior I see there is correct. Once it caches, I never see a miss again from any of my computers or nearby locations. Both my headers and theirs are similar except for a few (unimportant) differences. Any help is appreciated.

Was it helpful?

Solution

It could be that you have minimum TTL set to something non-default and it's caching it for that time period instead of whatever's in your Cache-Control header. I think that's unlikely both because you probably already thought of it and because it appears the distribution respects Cache-Control: no-cache (and it wouldn't if minimum TTL was non-default).

More likely, cloudfront is just evicting your items after a while:

If an object in an edge location isn't frequently requested, CloudFront might evict the object—remove the object before its expiration date—to make room for objects that are more popular.

- from this article

As for web-sniffer.net, you'll notice that it sends a Cache-Control: no-cache header, which will make cloudfront fetch from origin.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top