How to store frequently changing static files in Cloudfront?

499 Views Asked by At

I have some static image files that we use for our campaigns. The problem is that the marketing team changes the files very frequently and they take time to update in cloudfront cache.
Is cloudfront the best place to store such files? If yes, how do I go about handling this and make sure updated files are served. If instead cloudfront is not the best solution then what service should I use?
Origin to cloudfront is an s3 server.

2

There are 2 best solutions below

0
On

It depends how often these files are updated and whether the other benefits of CloudFront make it worth it.

If the files are updated on average, say, once an hour or more, and you have thousands or millions of users scattered around the world, then CloudFront is still beneficial, since those users will see reduced load times due to the cache. Either use object versioning (see here) or just set a low TTL, lower than the amount of time you're happy to allow files to be out of date for - going with the updated-once-an-hour example, perhaps you could set a TTL of 15-30 minutes.

If all your users are in a similar location, then just use the S3 bucket directly in that location. You'll save on CloudFront costs and caching didn't align well with your use case anyway.

You can also use cache invalidations, but these cost extra and if you're having to do this too often then you should decrease the TTL or move off CloudFront.

0
On

The recent (from August 2020) AWS blog titled Why is CloudFront serving outdated content from Amazon S3? discusses two solution:

  1. Invalidate the S3 objects: You can invalidate an S3 object to remove it from the CloudFront distribution's cache. After the object is removed from the cache, the next request retrieves the object directly from Amazon S3.
  2. Use object versioning: If you update content frequently, we recommend that you use object versioning to clear the CloudFront distribution's cache. For frequent cache refreshes, using object versioning might cost less than using invalidations.