About cache control headers
You can use cache control headers in your origin's responses to set policies that determine the maximum amount of time that content may be cached by Fastly. Fastly looks for caching information in each of these headers as described in our documentation on cache freshness in order of preference:
Surrogate-Control:
Cache-Control: s-maxage
Cache-Control: max-age
Expires:
Surrogate headers
Surrogate headers provide a specific cache policy for CDN caches like Fastly. Surrogate-Control
accepts many of the same values as Cache-Control
, plus some other more esoteric ones.
One use of this technique is to provide conservative cache interactions to the browser (for example, Cache-Control: no-cache
). This causes the browser to re-validate with Fastly on every request for the content, ensuring users get the freshest possible content. Simultaneously, your origin can send a Surrogate-Control
header with a longer max-age
that lets Fastly handle most of the browser traffic, only passing requests to your origin when Fastly's cache expires.
With Fastly, one of the most useful Surrogate headers is Surrogate-Key
. When Fastly processes a request and sees a Surrogate-Key
header in your origin's response, it uses the space-separated value as a list of tags to associate with the request URL in the cache. Combined with Fastly's Purge API, an entire collection of URLs can be expired from the cache in one API call (and typically happens in around 1ms).
Cache-Control
directives
Cache-Control
response directives, as defined by section 5.2.2 of RFC 9111, include:
Cache-Control: public
- Any cache can store a copy of the content. You don't need to add thepublic
directive to a response as most content is cacheable by default.Cache-Control: private
- Don't store. This is intended for user-specific content.Cache-Control: no-cache
- Re-validate before serving this content. This is ignored by Fastly.Cache-Control: no-store
- Don't ever cache this content in a web browser. This is ignored by Fastly.Cache-Control: max-age=[seconds]
- Caches can store this content for n seconds.Cache-Control: s-maxage=[seconds]
- Same as max-age but applies specifically to CDN caches like Fastly.
Only the max-age
, s-maxage
, and private
Cache-Control
directives will influence Fastly's caching. All other Cache-Control
directives will not, but will be passed through to the browser. For more in-depth information about how Fastly responds to these cache control headers and how these headers interact with Expires
and Surrogate-Control
, check out our documentation on HTTP caching semantics.
Expires
header
The Expires
header tells caches how long to store a piece of content. The downside is that it's a static date, and if your origin doesn't update it, the date will pass and browsers will start requesting that resource from Fastly every time.
Fastly will respect the Expires
header value only if your origin doesn't send Surrogate-Control
or Cache-Control
headers in the response.