Search code examples
amazon-cloudfront

Handling regularly updated files on Amazon S3 and CloudFront


If I am regularly overwriting files on S3, CloudFront caches them when I serve them in the browser. I was looking into Invalidation but there's a limit on how many validations at once it seems which wont work for a multi-teant website.

The only solution I can see is when overwriting a file, delete the old file and change the name of the new file (I was just going to add the TIMESTAMP to the end) so that CloudFront doesn't pull the cached version.

Would this be the most advised way to handle this sort of thing?


Solution

  • Here's the link to the specifics of the invalidation limits: http://aws.amazon.com/cloudfront/faqs/#Is_there_a_limit_to_the_number_of_invalidation_requests

    You can only have 3 invalidation requests in progress at any given time. If you exceed this limit, further invalidation requests will receive an error response until one of the earlier requests completes.

    The AWS recommendation is:

    it is recommended that you either implement a versioning system for your files and/or set a short expiration period.

    The short expiration period is straightforward, but has obvious downsides. To implement a versioning system, you could track the file version in a database, and append the version as a querystring parameter:

    file.jpg?v=2

    This way you can set a long expiration period, and the file only invalidates when you specifically upload the new file and update the version.

    As noted below in the comments, the default behavior is that CloudFront ignores the query string when building up the cache key, so be sure to enable Forward Query Strings under the Default Cache Behavior settings to use this versioning approach.