Amazon CloudFront
Developer Guide (API Version 2013-05-12)
« PreviousNext »
View the PDF for this guide.Go to the AWS Discussion Forum for this product.Go to the Kindle Store to download this guide in Kindle format.Did this page help you?  Yes | No |  Tell us about it...

Specifying How Long Objects Stay in a CloudFront Edge Cache (Object Expiration)

You can control how long your objects stay in a CloudFront cache before CloudFront forwards another request to your origin. Reducing the duration allows you to serve dynamic content. Increasing the duration means your customers get better performance because your objects are more likely to be served directly from the edge cache. A longer duration also reduces the load on your origin.

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.

By default, each object automatically expires after 24 hours. To specify a different expiration time, configure your origin to add a value for either the Cache-Control max-age directive or the Expires header field to each object:

  • The Cache-Control max-age directive lets you specify how long (in seconds) you want the object to remain in the cache before CloudFront gets the object again from the origin server. The minimum expiration time CloudFront supports is 0 seconds and the maximum is in the year 2038.

  • The Expires header field lets you specify an expiration date and time using the format specified in RFC 2616, Hypertext Transfer Protocol -- HTTP/1.1 Section 3.3.1, Full Date, for example:

    Sat, 30 Jun 2012 23:59:59 GMT

    Important

    After the expiration date and time passes, CloudFront gets the object again from the origin server every time an edge location receives a request for the object.

We recommend that you use the Cache-Control max-age directive instead of the Expires header field to control object caching. If you specify values both for Cache-Control max-age and for Expires, CloudFront uses only the value of max-age.

You cannot use the HTTP Cache-Control or Pragma header fields in a GET request from an end user to force CloudFront to go back to the origin server for the object. CloudFront ignores those header fields from the end user.

For more information about the Cache-Control and Expires header fields, see the following sections in RFC 2616, Hypertext Transfer Protocol -- HTTP/1.1:

For an example of how to add Cache-Control and Expires header fields using the AWS SDK for PHP, see Upload an Object Using the AWS SDK for PHP in the Amazon Simple Storage Service Developer Guide. Some third-party tools are also able to add these fields.

Specifying the Minimum Time that CloudFront Caches Objects for Download Distributions

For download distributions, if you are adding Cache-Control or Expires headers to your objects, you can also specify the minimum amount of time that CloudFront keeps an object in the cache before forwarding another request to the origin. Here's how object headers and a minimum TTL control how long objects remain in the CloudFront cache:

 Minimum TTL = 0 (Default)Minimum TTL > 0

Origin adds Cache-Control max-age directive to objects

Objects are cached for the value of the Cache-Control max-age directive

Objects are cached for the greater of the value of the Cache-Control max-age directive or the value of the CloudFront Minimum TTL

Origin does not add Cache-Control max-age directive

Objects are cached for 24 hours.

Objects are cached for 24 hours.

Origin adds Expires header

Objects are cached until the date in the Expires header. After the date passes, CloudFront forwards every request to the origin.

Objects are cached until the date in the Expires header. After the date passes, objects are cached for the value of the CloudFront Minimum TTL.

Origin adds Cache-Control no-cache, no-store, and/or private directives to objects

CloudFront respects the headers

Objects are cached for the Minimum TTL

For information about how to change settings for download distributions using the CloudFront console, see Listing, Viewing, and Updating CloudFront Distributions. For information about how to change settings for download distributions using the CloudFront API, see PUT Config.

Adding Headers to Your Objects Using the Amazon S3 Console

To add a Cache-Control or Expires header field to Amazon S3 objects using the Amazon S3 console

  1. Sign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/.

  2. In the Amazon S3 console, in the Buckets pane, click the name of the bucket that contains the files.

  3. In the Objects and Folders pane, select the first object to which you want to add a header field.

  4. At the top of the Objects and Folders pane, click Actions and click Properties.

  5. In the Properties pane, click the Metadata tab.

  6. On the Metadata tab, click Add More Metadata.

  7. In the Key list, click Cache-Control or Expires, as applicable.

  8. In the Value field, enter the applicable value:

    • For a Cache-Control field, enter:

      max-age=number of seconds that you want objects to stay in a CloudFront edge cache

    • For an Expires field, enter a date and time in HTML format.

  9. Click Save.

  10. If you want to add a header field to additional objects, in the Objects and Folders pane, click the name of the next object, and repeat Steps 6 through 9.