Skip to content

Serving stale HTTP Cache responses

Posted on:February 23, 2024

Today’s tech tip

Did you know that you can sometimes use cache responses even if they’re stale?
This applies if the Request Cache-Control header contains the max-stale=N directive OR the Cached Response’s Cache-Control header contains stale-if-error=N or stale-if-revalidate=N. In either of these cases we can still use a cached response for N seconds after it went stale!

Very useful for implementing more complex, conditional caching strategies.

Example

If we sent the request below, we would be telling the cache that it may reuse a response as long as it is not older than 5 days (max-age=18000). Additionally the cache MAY reuse the response for a further 2 days max-stale=7200 if it has any reason to (e.g. it has trouble contacting the origin server).

A screenshot of a http request with a Cache-Control header with a value of max-age=18000, max-stale=7200

References: