0

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks for the help. I don't think the eviction is taking place though because I'm testing with relatively new files. It would seem that CF has to wait at least some time (months?) before it does something like that. Also: If you try this image from Squre on web-sniffer you'll see that it's a HIT everytime. It seems that my headers and theirs are similar, and both on CloudFront.
You're right, I'm trying it on my own files served through cf and it's registering a hit. Could you post the URL of the source object?
Alex, here's the source url. I can give you more info if you need. When I upload the files, I'm setting 'Cache-Control' and 'Expires' to 10 years. Thanks.
I set up another default distribution in front of you bucket and managed to get web-sniffer.net to show cache hits every time (after a couple of misses). This leads me to suspect that whatever's happening is happening because of your distribution settings. Do you have anything non-default in your configuration?
Very interesting. I'm not doing anything outside of the default. I can go ahead and setup another one. I'll leave everything as default and post it here.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.