HTTP caching basics·续
Stale-if-error#
When staleness is allowed, the end user will not be impacted by potentially slow backends during the revalidation process. Thanks to stale-while-revalidate, the cache can be instructed to serve stale data while a new version of the resource is being fetched.
But what happens when the origin web server is down?
As long as the stale-while-revalidate value is high enough, stale content will be served and the failed revalidation will go unnoticed as far as the user is concerned.
By setting a very high stale-while-revalidate value, some business rules may be violated in situations where the origin web server is healthy.
The stale-if-error directive sets the staleness when the origin is down. Here’s an exammple:
Cache-Control: public, max-age=900, stale-while-revalidate=100,
stale-if-error=86400
In this example an object is stored in the cache for 900 seconds and if the origin is healthy, this object may be served up to 100 seconds past the expiration of the object.
If the backend is down, the staleness can be drastically increased. In this case, a stale object may be served a full day past its expiration time.
Must-revalidate and proxy-revalidate#
When staleness is not allowed, the must-revalidate keyword is used to enforce this. Here’s an example:
Cache-Control: public, max-age=3600, must-revalidate
In this case the cached object is fresh for an hour, but as soon as it expires synchronous revalidation is mandatory.
If the web client is allowed to serve stale objects from the cache, but intermediary caches aren’t, you can use the proxy-revalidate keyword to enforce this.
Cache-Control: public, max-age=3600, stale-while-revalidate=100,
proxy-revalidate
In this case the object is cached for an hour. After that an extra 100 seconds of staleness is allowed while revalidation takes place. Because of proxy-revalidate, staleness is only allowed by the web client. Caching proxies are not allowed to serve stale content.
No-cache and no-store#
If the web server returns an uncacheable response, the Cache-Control: no-cache, no-store syntax can be used to instruct the cache not to cache this response.
The no-cache directive forces the cache not to serve the cached resource to the requesting client and instead revalidate the content with the origin web server.
The no-store directive instructs the cache not to store this resource in the cache.
Both directives can be used separately, but they can also be combined.
Expires#
The Expires header can also be used to set the time to live of an object. The Expires header doesn’t use relative numbers like the Cache-Control header does. Instead it sets the date and time of expiration.
Here’s an example:
Expires: Sat, 4 May 2024 08:00:00 GMT
This cacheable resource is considered fresh until Saturday May 4th 2024 at 8 o’clock GMT.
By setting a date and time in the past, the Expires header can instruct the cache not to store the object.
Vary#
The Vary header is used to create cache variations. As explained earlier, cache variations are used to create multiple variations of a cached object, based on a request header.
Here’s an example:
Vary: Accept-Language
This example will create a cache variation for this resource based on the value of the Accept-Language request header. This will allow multilingual websites that use the same URL structure for multiple languages to be properly cached.
Here’s another example:
Vary: X-Forwarded-Proto
This example will create a cache variation based on the value of the X-Forwarded-Proto request header. This header is not sent by the client, but by a TLS proxy that terminates the TLS connection. Possible values are https and http.
This cache variation ensures that there’s an HTTP and an HTTPS version of each page to avoid mixed content.
Etag and If-None-Match#
The Entity tag that is returned through the Etag response header is used to identify a specific version of the resource.
The Etag value could be any value, but it must be unique to the version it represents. Consider it a fingerprint of the content.
When a web server returns an Etag header, the value can be presented by the client upon subsequent requests under the form of an If-None-Match request header.
This If-None-Match header represents the version of the resource it currently has. This value can be compared to the Etag that is returned by the web server. If the values are identical, the content hasn’t changed and a 304 Not Modified status code can be returned without attaching a body to the HTTP response.
If the values of If-None-Match and Etag differ, the content has changed and a regular 200 OK response is returned that includes a body.
Last-Modified and If-Last-Modified#
The Last-Modified response header also identifies a specific version of a resource. Unlike the Etag header, it uses a last modified date to identify that resource.
Here’s an example:
Last-Modified: Mon, 8 Nov 2021 18:28:00 GMT
This value represents the last time the resource was modified. The value of that response header can be presented to the web server upon subsequent requests under the form of a If-Modified-Since header.
Here’s an example:
If-Modified-Since: Sun, 7 Nov 2021 13:18:21 GMT
The value of the If-Modified-Since header is older than the one presented by the Last-Modified header. This means the content has changed and a 200 OK response should be returned.
If the values of If-Modified-Since and Last-Modified were identical, the client has the most recent version of the resource and a bodyless 304 Not Modified response could be returned.
Age#
An Age header is used to inform the client how long the object has been stored in cache.
Here’s an example:
Age: 100
This means the object has been stored in the cache for 100 seconds.
Imagine the following example:
Cache-Control: max-age=300
Age: 100
We know that the max-age=300 directive sets the Time To Live of the cached object to 300 seconds. The fact that the Age header is set to 100 seconds means that the cache object has a remaining lifetime of 200 seconds.
HTTP caching flow#
When a reverse caching proxy server like Varnish is used to accelerate your origin server, there is a specific flow depending on the scenario.
We’d like to present four scenarios:
- The cache miss flow
- The cache hit flow
- The cache revalidation flow
- The conditional revalidation flow
Cache miss flow#
When a client requests content from an empty cache, a cache miss occurs and the cache has to fetch the content from the origin. The following diagram illustrates this process:

Although we try to keep origin fetches to a minimum, a cache miss is not necessarily a bad thing. A cache miss is simply a hit that hasn’t happened yet.
When the origin web server responds, the caching proxy will store the response in the cache with a lifetime that was specified by the Cache-Control or Expires header and will serve the cached object to clients requesting it.
Cache hit flow#
Once the object is stored in cache, subsequent requests will result in a cache hit, as illustrated in the diagram below:

As you can see, no connection to the web server is needed. This is by design and takes away the pressure from that origin web server while the caching proxy is serving the cached version of those origin responses.
Cache revalidation flow#
At some point the cached object will expire and the content will need to be revalidated with the origin web server.
This involves an origin fetch, just like a cache miss. But unlike the cache miss scenario, the cache can choose to serve the stale content while asynchronously revalidating with the origin.
The diagram below clarifies the revalidation flow:

If you pay close attention, you’ll see that the order of execution is different: the client response can be returned before the origin revalidation response is received.
The following Cache-Control will enable asynchronous revalidation thanks to its stale-while-revalidate directive:
Cache-Control: public, s-maxage=3600, stale-while-revalidate=200
If we want to serve stale content when the origin web server is down, we could use the following Cache-Control header and leverage the stale-if-error directive:
Cache-Control: public, s-maxage=3600, stale-if-error=86400
Conditional revalidation flow#
Revalidation can also be done conditionally. This means that the caching proxy will identify the version of the object through specific request headers, such as If-None-Match or If-Modified-Since.
The values of these headers are come from the Etag or Last-Modified response headers that are part of the cached object.
If the latest version matches the version that is advertised by the proxy, the origin will acknowledge this and not send the full payload. A 304 Not Modified response is returned, the stale content is then considered fresh again and revalidation is paused until the content expires again.
A version matches if the Etag and If-None-Match values are identical or if the Last-Modified and If-Modified-Since values are identical:

If the versions differ, the full response is sent by the origin and the content is considered fresh again:

Conditional requests can also be sent from the client to Varnish. In that case Varnish will return the expected 304 Not Modified response without the body, instead of returning the payload of the cached object.
Conditional revalidation allows backends to consume less bandwidth by only adding payload to the HTTP response if the content has changed. If the origin is optimized for conditional revalidation, CPU, memory and disk I/O consumption can also be reduced.
