stale-if-error for Origin Server Resilience

Learn how stale-if-error keeps your site available when the origin server fails by serving cached content during outages and 5xx errors.

Directives

Detailed Explanation

Building Resilience with stale-if-error

stale-if-error is a Cache-Control extension that allows caches to serve stale content when the origin server returns a 5xx error or is unreachable. It acts as a safety net, keeping your site functional during outages.

How It Works

Cache-Control: max-age=300, stale-if-error=86400
  • For the first 300 seconds, serve the fresh cached response
  • If the origin server fails (500, 502, 503, 504, or network error) within the next 86400 seconds (24 hours), serve the stale cached version instead of an error page

Error Conditions That Trigger stale-if-error

  • 500 Internal Server Error
  • 502 Bad Gateway
  • 503 Service Unavailable
  • 504 Gateway Timeout
  • Network failures (DNS resolution failure, connection refused, timeout)

Combining with stale-while-revalidate

The two stale directives complement each other:

Cache-Control: public, max-age=3600, stale-while-revalidate=60, stale-if-error=86400
  • Normal operation: Fresh for 1 hour, then SWR for 1 minute
  • During outage: Serve stale content for up to 24 hours

CDN Support

Most major CDNs support stale-if-error:

  • Cloudflare: Supported (enabled by default on some plans)
  • Fastly: Supported via Varnish configuration
  • Akamai: Supported
  • AWS CloudFront: Limited support (requires custom error pages)

When NOT to Use stale-if-error

  • Financial transactions: Serving stale bank balances could mislead users
  • Real-time data: Stock prices, live scores, or auction bids must never be stale
  • Authentication pages: Stale login forms might submit to wrong endpoints after deployment

Use Case

A SaaS documentation site sets 'max-age=3600, stale-if-error=86400' on all pages. When the origin server goes down for scheduled maintenance at 3 AM, users in different time zones continue accessing documentation from the CDN cache. Instead of seeing a 502 error, they see the content that was cached up to 24 hours ago. Since documentation changes infrequently, the stale content is virtually identical to the latest version.

Try It — Cache-Control Builder

Open full tool