Understanding Cache Behavior in CloudFront

Amazon CloudFront is a content delivery network (CDN) that caches content at edge locations to improve response times for users. However, when you update your backend origin (such as an S3 bucket or an EC2 instance), CloudFront does not automatically update its cache. Instead, it continues serving cached content until the Time-to-Live (TTL) expires.

While TTL-based expiration works in many cases, sometimes you want the latest content to be available immediately. This is where cache invalidation comes into play.


What is CloudFront Cache Invalidation?

Cache invalidation allows you to forcefully refresh specific files or entire paths in CloudFront before the TTL expires. This ensures that users always get the most up-to-date content without waiting for the cache to expire naturally.

Key Points About Cache Invalidation:

  1. Manually Triggered: You must explicitly request an invalidation for specific files or patterns.
  2. Removes Cached Content: The specified files are removed from all edge locations, forcing CloudFront to fetch fresh content from the origin.
  3. Speeds Up Content Updates: Users immediately see the latest version of the files.

Example of Cache Invalidation in CloudFront

Scenario

Steps to Invalidate Cache

To force CloudFront to fetch fresh files, you need to invalidate specific paths: