How to Add Caching Headers to Your Objects Using Amazon S3

How to Add Caching Headers to Your Objects Using Amazon S3

AWS CloudFront is a content distribution service offered by AWS to serve low latency content with high data speed. CloudFront caches the object to its edge location. By default any object stays in the edge location for 24 hours after which CloudFront sends the request to its caching origin to fetch the object again. If the user has more dynamic data that requires frequent updates, it is recommended to lower the caching period. If the object does not change for a longer period and is requested frequently, it is advisable to increase the caching period. This is because the data transfer cost between S3 and CloudFront will be reduced. In addition, the latency will also improve as the load on the origin will be reduced.

The present guide demonstrates how to set cache-control and the expiration headers to an S3 object.

The user can also specify the object expiration time. CloudFront serves the object from its edge location until it expires.  Once the object is expired, CloudFront will send the request to the origin server to check for the updated version of the object.  The origin will send the updated version if it is modified or else send the status code indicating that the object has not been updated.

As mentioned above, by default the object expires in 24 hours unless specified. To configure a different expiration time there are two headers available, whereby the user can set it at the object level.

1. Create a bucket with AWS S3. Make the bucket publicly accessible with the access policy. Upload the media file to be distributed using CloudFront to the bucket and make it public.

2. Select the object metadata and add the following two parameters:

a. Cache-control: It specifies how long an object should stay in CloudFront before it sends a request again to the origin server. The parameter accepts a value in milliseconds. The parameter will be set as the key pair value. The key is cache-control and the value is max-age=<time in seconds>. In the example given below, the user has set the expiration at 1 year (24*365*60*60 = 31536000). The minimum value can be 0 and the maximum value can be up to Year 2038.

b. Expires: It allows the user to specify the expiration time period in a Full date format, such as
“Sat, 31 Jan 2022 23:59:59 GM”. Once the object is expired, CloudFront will fetch the object again from the origin.

3. The user can specify both or one of the values. If both the values (Cache-control and Expires) are specified, AWS considers only the Cache-control max-age value. Thus, it is advisable to use Cache-control instead of Expires.

4. When creating a download distribution for AWS CloudFront, the caching configuration allows to configure a parameter called TTL to specify the minimum time an object should stay in the cache.

5. The minimum value for TTL is 0 seconds. AWS recommends that if the user has specified Cache-Control, then “Use Origin Cache Headers” should be selected in the Cache behavior setting. If the user has specified the Cache-control or the Expires header and the specified value of TTL, AWS follows the rule as specified in the table given below.

 

If Minimum TTL = 0 (Default)

If Minimum TTL > 0 (as explained in step #4)

If Origin has added Cache-Control max-age directive to objects as explained step#2.a Objects will be cached for the value specified in the Cache-Control max-age. If TTL is > max age than objects will be cached for the value of the CloudFront Minimum TTL or else it will be cached as per the max-age value.
If Origin does not have Cache-Control max-age directive configured Objects will be cached for 24 hours. Objects will be cached for 24 hours.
If Origin has added Expires directive to objects as explained step#2.b Objects will be cached as per the date specified in the Expires header. After the date is passed, CloudFront will forward every request to the origin. Objects will be cached as per the date specified in the Expires header. After the date is passed, objects will be cached till the value of the CloudFront Minimum TTL.
If Origin has added Cache-Control no-cache, no-store, and/or private directives to objects CloudFront will not cache objects as it will follow the headers directives. Objects will be cached for the value of the CloudFront Minimum TTL.

Keywords: Amazon Web Services, AWS, Amazon AWS Console, AWS S3, Amazon CloudFront, AWS CloudFront, CloudFront, AWS EC2, AWS S3, Amazon S3

You must be to post a comment.

* As a bonus, you'll receive our weekly newsletter!

Hitchhiker's Guide to The Cloud

Newvem's eBook for Cloud Operations