Stale-if-error

On this page 7

Stale-if-error is a Cache-Control extension directive that allows a cache to serve an expired (stale) response when an attempt to fetch a fresh copy from the origin fails. In content delivery, it turns a cached object into a safety net during origin outages, so users keep receiving content instead of an error page.

The directive is defined in RFC 5861 together with its sibling stale-while-revalidate. It takes a value in seconds, for example Cache-Control: max-age=600, stale-if-error=86400, which means the response is fresh for ten minutes and may still be served for up to one day after expiry if the origin is unavailable.

How stale-if-error works in a cache

Stale-if-error activates only when a cached response is past its freshness lifetime and the cache tries to revalidate or refetch it. If that request ends in an error condition, the cache may return the stale copy instead of passing the failure to the client.

RFC 5861 defines an error as a 500, 502, 503 or 504 status code, or a situation in which the origin cannot be reached at all, such as a connection refusal or timeout. A 404 or other 4xx response is not treated as an error for this purpose, because it is a valid answer from a working origin.

The window is measured from the moment of expiry, not from the time the response was stored. The table shows how a cache handles a request at different points in the lifetime of an object sent with max-age=600, stale-if-error=3600.

Age of cached copyOrigin stateCache action
0 to 600 sAnyServe fresh copy without contacting origin
600 to 4,200 sHealthyFetch or revalidate, serve new response
600 to 4,200 s5xx or unreachableServe stale copy instead of the error
Over 4,200 s5xx or unreachablePass the error to the client
Cache behavior over the lifetime of one response

Where the directive can be set

Stale-if-error can appear in both response and request headers, and the two placements mean different things. In a response, the origin grants permission to caches; in a request, a client states how much staleness it is willing to accept during failures.

PlacementWho sets itEffect
Response Cache-ControlOrigin serverAllows shared and private caches to fall back
Request Cache-ControlClient or upstream proxyStates the acceptable stale age on error
Surrogate-Control or CDN headerOrigin for edge onlyApplies the fallback at the CDN, not in browsers
CDN configuration ruleOperatorOverrides or adds the behavior per path
Placements of stale-if-error and their meaning

Support varies. Major CDNs honor stale-if-error or provide an equivalent setting, often under names such as serve stale or stale on error. Browser support is limited, so the directive mainly protects users through shared caches at the edge rather than in their own browser cache.

Stale-if-error belongs to a group of freshness and revalidation controls, and each one answers a different question about when old content is acceptable. Confusing them is a common source of unexpected caching behavior.

DirectivePurposeTriggers whenServes stale content
max-ageSets freshness lifetimeAlways appliedNo, defines fresh period
stale-while-revalidateHides revalidation latencyCopy is stale, origin healthyYes, while refetching in background
stale-if-errorMasks origin failuresRefetch returns an errorYes, until the window ends
must-revalidateForbids stale useCopy is staleNo, error is returned instead
no-storePrevents cachingAlways appliedNo, nothing is cached
Comparison of staleness-related directives

The two RFC 5861 directives are complementary rather than alternatives. A typical configuration uses a short stale-while-revalidate window of seconds or minutes for speed and a long stale-if-error window of hours or days for resilience. Note that must-revalidate and proxy-revalidate explicitly prohibit serving stale content, so they should not be combined with stale-if-error on the same response.

Choosing a stale-if-error window

The right window depends on how harmful outdated content is compared with an error page. The decision table below gives typical starting points for common content types.

Content typeSuggested windowReason
Static assets with versioned namesSeveral days or moreContent never changes under the same URL
Article and marketing pagesSeveral hours to a daySlightly old text is better than an outage
Product listings and pricesMinutes to an hourStale prices may cause disputes
API responses for dashboardsMinutesData should signal age to users
Personalized or account pagesNot usedUsually private and not cached at the edge
Suggested windows by content type

Longer windows only help if the object is still in the cache. Edge caches evict rarely requested objects, so low-traffic URLs may be gone when the outage begins. Origin shielding and tiered caching increase the chance that a stale copy survives.

Limitations and risks

Stale-if-error improves availability, but it does not make the origin redundant and can hide real problems. Teams should understand its limits before relying on it.

  • Silent outages: users see working pages, so monitoring must watch origin errors directly rather than edge success rates.
  • Outdated critical information: security notices, prices or stock levels can remain wrong for the whole window.
  • Cache purge interaction: purging an object during an outage removes the fallback copy and exposes the error.
  • Uneven support: some caches ignore the directive or apply only part of the RFC 5861 rules.
  • Non-cacheable responses: content marked private or no-store never benefits from the fallback.

Used deliberately, stale-if-error is one of the cheapest resilience measures available to a site, because it needs only a header and a supporting cache.

Put simply

Stale-if-error is a permission to show yesterday’s copy when today’s copy cannot be fetched. Imagine a newsstand that normally gets fresh papers every morning. If the delivery truck breaks down, the seller can keep offering the previous edition for a while instead of leaving the shelves empty.

Visitors may see slightly old information, but they still get something useful. The key trade-off is freshness versus availability, and the website owner decides how old is still acceptable.

In practice

A regional news publisher serves its site through a CDN and sends article pages with Cache-Control: public, max-age=120, stale-while-revalidate=60, stale-if-error=43200. Pages are refreshed every two minutes under normal conditions, and the edge may fall back to cached versions for up to 12 hours if the content management system fails.

During a major storm, traffic spikes while a database upgrade goes wrong, and the origin returns 503 errors for 40 minutes. The CDN keeps serving the most recent cached copy of every popular article and the home page. Readers notice only that new stories stop appearing, while the operations team sees the origin alerts and restores the database. No visitor receives an error page on cached URLs, and fresh content resumes as soon as the origin recovers.