Cdn-Cache-Control A new way to control caching

AC   

Have you ever been confused configuring caching on a website? Public resources are being cached, however the server configuration is preventing caching of an otherwise cacheable resource?

Controlling caching without a service like Peakhour requires messing around with the cache-control headers. There are a wide array of fields and directives in this header, each informing downstream clients (eg proxies, shared caches, and end browsers) how to handle caching for a particular resource.

Correctly setting this header can be complicated and also brittle, depending on how a web server may be configured.

Enter CDN-Cache-Control

CDN-Cache-Control is a recent proposed header specified in https://datatracker.ietf.org/doc/html/draft-cdn-control-header-01

The draft RFC states:

This specification defines a convention for HTTP response header
fields that allow directives controlling caching to be targeted at
specific caches or classes of caches.  It also defines one such
header field, targeted at Content Delivery Network (CDN) caches.

What does this mean? A practical example is in order. Imagine you have a Magento application, pages are cacheable by a cache but not by a browser. A cache-control header could look like:

Cache-Control: max-age=0, s-max-age=600

This would tell a browser cache that the resource is not cacheable, but shared caches may cache this response for 600 seconds.

What if the application doesn't want a shared cache to cache the resource, as the application automatically sends purges to the cache to control changed?

This facility isn't available using a cache control header as the above header enables any cache to cache the resource for the specified time. The application could then send:

Cache-Control: max-age=0, s-max-age=60
CDN-Cache-Control: max-age=3600

Indicating that the browser can't cache the page, the shared cache can cache the page for 60s but the CDN can for one hour avoiding our problem.

Targeted CDN-Cache-Control

What if you need to target a specific CDN only? This can be achieved by using the providers targeted cdn-cache-control-header.

For example, to target Peakhour you can use Peakhour-CDN-Cache-Control to instruct only Peakhour to cache the resource and all other CDN's in the request won't honour this header.

Header format

Examples

For example, the following header fields would instruct a CDN cache to consider the response fresh for 600 seconds, other shared caches for 120 seconds and any remaining caches for 60 seconds:

Cache-Control: max-age=60, s-maxage=120
CDN-Cache-Control: max-age=600

These header fields would instruct a CDN cache to consider the response fresh for 600 seconds, while all other caches would be prevented from storing it:

Cache-Control: no-store
CDN-Cache-Control: max-age=600

Because CDN-Cache-Control is not present, this header field would prevent all caches from storing the response:

Cache-Control: no-store

Whereas these would prevent all caches except for CDN caches from storing the response:

Cache-Control: no-store
CDN-Cache-Control: none

(note that 'none' is not a registered cache directive; it is here to avoid sending a header field with an empty value, because such a header might not be preserved in all cases): max-age=60

Website Performance Learning

© PEAKHOUR.IO PTY LTD 2024   ABN 76 619 930 826    All rights reserved.