Search code examples
imageamazon-web-servicescdnamazon-cloudfront

Cloudfront does not reliably store images over a period of days


I have a webserver which is dynamically generating images for an e-commerce site, and I have set up Amazon Cloudfront in front of this server to cache image requests to reduce load on my own server.

I find that on any given day, images (once loaded a single time that day) are served from cloudfront. e.g:

enter image description here

The problem is that the image is being regenerated (Cloudfront is missing my image and needs to request it from my server) each day, cloudfront seems to ignore the 'max_age' I am defining on my own server and choosing a default of 24 hours. This is the response from the server 'behind' cloudfront:

enter image description here

Are there any reasons why cloudfront would be ignoring my 'max-age' value based on the responses I'm sending? To add extra infuriation, it does hold on to some images for more than 24 hours, but never more than a couple of days.


Solution

  • This might be an artifact of how Amazon CloudFront operates regarding object expiration, see Specifying How Long Objects Stay in a CloudFront Edge Cache (Expiration):

    Typically, CloudFront serves an object from an edge location until the object expires. After it expires, the next time the edge location gets an end-user request for the object, CloudFront forwards the request to the origin server to verify that the cache contains the latest version of the object:

    • If CloudFront already has the latest version, the origin returns only a 304 status code (not modified).

    • If CloudFront does not have the latest version, the origin returns a 200 status code (OK) and the latest version of the object.

    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. [emphasis mine]

    Infrequent object access would also explain why it does hold on to some images for more than 24 hours, but never more than a couple of days. This should be evident by the lack of entries in the CloudFront Access Logs, so in case you aren't positive that those files are frequently requested, checking the logs could yield a definite answer.