Amazon CloudFront cache invalidation strategy for fresh content and cost-effectiveness

I'm trying to get my head around what approach should I use to force update the CDN of user profile photos on a website where CloudFront is the CDN serving profile photos and S3 is the main file storage.

I need to make sure that the user profile photos are updated as soon as the user updates the photos of their profiles. I see three options that I can make to update profile photos and to ensure that website users receive the latest image as soon as the profile photos are revised. Are these approaches better than others in terms of providing fresh content and maximizing long-term cost-effectiveness? Are there better approaches to delivering fresh content and maximizing long-term cost-effectiveness?

  • Print a single request to an S3 object to save the file with its original name and issue one Amazon CloudFront invalidation request. Amazon CloudFront allows up to 1000 free invalidation requests per month, which is a bit like the downside.
  • Run one request from the S3delete object to delete the original photos, then one request to place the S3delete object to save the new photo with a unique name for the new photo file. These will be two S3 file update requests and will not require a CDN CloudFront invalidation request. CloudFront will then serve the latest files as soon as they are updated, provided that the image URLs are automatically set to the new file names.
  • Issue one request to the S3 object to save the file with its original file name, and then on the client side add the version code to the CDN URLs (i.e. /img/profilepic.jpg?x=timestamp) or something in this direction. I'm not sure how effective this strategy is in terms of invalidating cached CloudFront objects.

thanks

+6
source share
1 answer

Invalidation of CloudFront may take some time, and it is recommended as a last resort to remove content that needs to be removed (for example, copyright infringement).

The best approach is URLs with versions. For profile images, I would use a unique identifier (such as a GUID). Whenever a user uploads a new photo, replace that URL (and delete the old photo if you want).

When you update your database with a new user profile identifier, CloudFront pulls out a new image and the change will be immediate.

+4
source

Source: https://habr.com/ru/post/905461/


All Articles