Skip to content

Stale-while-revalidate

Stale-while-revalidate is an HTTP Cache-Control extension from RFC 5861 that serves stale content instantly while revalidating a fresh copy in the background.

Definition

What it is

Stale-while-revalidate (often abbreviated SWR) is an HTTP cache directive. It tells an intermediate cache (a CDN edge, a reverse proxy, a browser cache) that after a resource's freshness expires, it may still serve the old copy for a bounded additional window, and revalidate the fresh copy asynchronously. The client gets a response immediately — fast, always — and the cache updates its stored copy from the origin for the next request. The next visitor sees the refreshed content, and the visitor after that, and so on, until the origin changes.

This is a different contract from the default. Standard HTTP caching says "if fresh, serve it; if stale, revalidate synchronously (blocking) or return an error". SWR says "if fresh, serve it; if stale but within the SWR window, serve stale now and revalidate in the background". The client never waits for revalidation. The origin sees at most one revalidation per cache entry per window, not one per request. It is a small, specific extension with a large effect on perceived latency.

Why it matters

The default cache behaviour couples the two: a stale entry means the user waits for the origin to respond. If the origin is slow (200ms), the user waits 200ms. If the origin is down, the user gets an error. SWR removes both couplings. The user waits a cache round-trip (single-digit milliseconds at the edge) and gets content immediately. The origin load, already low because most requests hit the cache, stays low because revalidations happen in the background, off the hot path. When the origin has a hiccup, users do not see it; they see the last good copy.

The practical effect is measurable. Pages served through SWR see their p95 latency dominated by the cache hit time, not the origin. TTFB becomes stable even when the origin is having a bad day. The origin's revalidation load becomes a small fraction of what it would be under default caching, because revalidations are single-shot per window instead of per request. For any resource whose freshness is not hyper-critical — a blog post, a product listing, a marketing page, an API response that changes on the order of minutes — SWR is the single highest-leverage cache directive available.

How it works

The mechanism rests on three time values. max-age=N defines how long the entry is fresh: within N seconds, the cache serves without any check. stale-while-revalidate=W defines how long after freshness the cache may still serve stale while revalidating: within the W-second window after max-age, the cache serves the stale copy and fires a background request to the origin. Beyond max-age + W, the cache treats the entry as missing: the next request blocks on a synchronous revalidation, exactly as in default caching.

sequenceDiagram
  participant C as Client
  participant Cache
  participant Origin
  C->>Cache: GET /page
  alt Fresh (age < max-age)
    Cache-->>C: 200 (fresh)
  else Stale within SWR window
    Cache-->>C: 200 (stale)
    Cache->>Origin: revalidate (async)
    Origin-->>Cache: 304 or new content
  else Stale beyond SWR window
    Cache->>Origin: revalidate (sync)
    Origin-->>Cache: 304 or new content
    Cache-->>C: 200 (fresh)
  end

The directive composes with stale-if-error. Cache-Control: max-age=60, stale-while-revalidate=600, stale-if-error=86400 means: fresh for 60s; serve stale and revalidate in the background for the next 600s; if the origin fails, continue serving the last known good copy for up to 24h. The three directives together define the cache's tolerance envelope: how long it can go without confirmation, and how long it can hide an outage.

Deploying stale-while-revalidate

  1. Pick the max-age correctly

    max-age should match how often the content actually changes, not how often you would like it to. A pricing page that changes hourly should not have max-age=300.

  2. Set the SWR window to the outage window you can tolerate

    If you can hide a 10-minute origin outage without user impact, stale-while-revalidate=600. If the origin must recover faster, use a shorter window.

  3. Monitor the revalidation rate and stale hit rate

    A healthy SWR deployment shows a steady trickle of revalidations, not a flood. A flood means SWR is not caching properly; a dry spell means nothing is expiring.

  4. Combine with stale-if-error for full resilience

    SWR hides origin latency; stale-if-error hides origin failures. They are complementary, and most production deployments should enable both.

Different caches implement SWR differently. Fastly and Cloudflare support it at the edge as a first-class feature. CloudFront supports it via cache policies. Browsers vary: Chrome and Firefox implement it for certain contexts; Safari has partial support. A cache that does not understand the directive treats the response as a normal max-age entry, which is safe but loses the optimization. That makes SWR a "gracefully degrading" feature — you get the benefit where it is supported, and the default behaviour where it is not.

Common failures

A second failure is assuming universal support. RFC 5861 is an extension, not a baseline requirement. Many proxies, older CDNs, and most browser cache implementations do not understand it. A deployment that relies on SWR to hide origin outages will be surprised when the CDN in front of it drops the directive and passes the stale-miss through to the origin. The fix is to test what your actual cache fleet does with the directive, not what the RFC says should happen.

A third failure is combining SWR with aggressive must-revalidate. must-revalidate forbids serving stale under any circumstances, which contradicts SWR. A response with both directives is contradictory, and different caches resolve it differently. The fix is to remove must-revalidate from any response that uses SWR, and to explicitly document the intended behaviour in the cache policy.

Origin

Mark Nottingham, one of the most influential figures in HTTP caching, proposed the directive to solve a specific problem: a CDN edge that has a cached copy approaching expiry, and a burst of traffic hitting it simultaneously. Under default caching, every request in the burst triggers a revalidation, all at once, hammering the origin. SWR was designed to break that thundering herd: serve one stale copy, revalidate once, and let the origin handle a single request instead of a thousand.

RFC 5861 (2010) formalized the directive alongside stale-if-error. It was adopted relatively slowly — CDN support arrived first (Fastly, Cloudflare, Akamai), browser support followed but remains partial. By the 2020s, SWR was a standard recommendation in web performance literature and a default feature of most edge caching platforms. The general concept — "serve stale, revalidate in background" — has since been adopted in areas far beyond HTTP caching: React's SWR library, which uses the same name, applies the concept to data fetching in UI components.

The stale-while-revalidate cache directive allows a cache to serve a stale response while it revalidates the response in the background, reducing latency for the end user and load on the origin server.

— Nottingham, M., *RFC 5861: HTTP Cache-Control Extensions for Stale Content*, 2010

Evidence

FAQ

What is the difference between stale-while-revalidate and stale-if-error?

SWR serves stale content while revalidating a fresh copy in the background, hiding latency. stale-if-error serves stale content when the origin is unreachable, hiding failures. They complement each other: SWR smooths the common case (slow origin), and stale-if-error smooths the rare case (broken origin). Most production deployments should enable both.

Does stale-while-revalidate work in browsers?

Partially. Chrome and Firefox implement it in certain contexts; Safari support is incomplete. CDN edges (Fastly, Cloudflare, Akamai) support it more consistently. A deployment that depends on SWR should test the actual cache fleet, not assume RFC compliance equals universal implementation.

What is a safe stale-while-revalidate window?

Match it to the outage you can tolerate hiding. A blog post can have a window of hours. A product price that must be accurate within minutes should have a window of seconds. The window is the interval during which you accept stale content without user impact — that is a business decision, not a technical one.

Can I use stale-while-revalidate with APIs?

Yes, and it is increasingly common. An API that returns the same data repeatedly (user profile, product listing, search results) benefits from SWR exactly as HTML pages do. The caveat is that API clients may have their own caching logic; SWR at the edge and SWR in the client cache are separate layers.

What happens if the origin never responds?

Within the SWR window, the cache keeps serving stale content. Outside the window, the cache treats the entry as missing and blocks the next request on a synchronous revalidation. If the origin is down, that revalidation fails and the cache returns an error (unless stale-if-error extends the window). The bound is what prevents SWR from becoming "serve stale forever".

Connections

Stale-while-CacheCache invaTTLCDNLatencyStale dataMemcachedRedisCache

● broader ● narrower ● related

Used in the garden

Sources

Etymology

Origin: compound

  • stale (adjective): from Old French estale, "at a standstill"; no longer fresh
  • revalidate (verb): from Latin re-, "again", + validare, "to make strong or valid"

Semantic network

Stale-while-CacheCache invaTTLCDNLatencyStale dataMemcachedRedisCache

● broader ● narrower ● related

Used in the garden

Sources