Inferensys

Glossary

Stale-While-Revalidate

A Cache-Control directive that allows a CDN or browser to serve a stale cached response immediately while asynchronously fetching a fresh version from the origin in the background.
ML engineer managing model versions on laptop, version history visible, technical Git-like workflow.
CACHE STRATEGY

What is Stale-While-Revalidate?

A caching directive that prioritizes speed by serving immediately available content while updating it in the background.

Stale-While-Revalidate is a Cache-Control response directive that instructs a cache to immediately serve a stale (expired) cached asset to the client, while simultaneously and asynchronously revalidating the asset by fetching a fresh version from the origin server in the background. This mechanism ensures that subsequent requests receive the updated resource without the initial request suffering the latency of a blocking origin fetch.

This strategy is ideal for assets where speed is critical and slightly outdated data is acceptable, such as newsfeeds or social media timelines. The directive specifies a time window, e.g., Cache-Control: max-age=60, stale-while-revalidate=3600, meaning the cache serves fresh content for 60 seconds, then serves stale content for up to an hour while a background refresh occurs, effectively hiding server latency from the user.

CACHE EFFICIENCY

Key Characteristics

The stale-while-revalidate directive is a fundamental building block for high-performance, always-available web architectures. It decouples the user experience from origin server latency.

01

Immediate Stale Delivery

The primary mechanism: when a cached object's Time-To-Live (TTL) expires, the CDN or browser does not block the request. It instantly serves the stale (expired) version to the user, eliminating origin latency from the critical rendering path. This ensures a cache hit is always served, even during revalidation, maintaining sub-millisecond response times.

02

Asynchronous Background Refresh

While the stale asset is being served, a separate, non-blocking request is triggered to the origin server to fetch the latest version. Upon successful retrieval, the stale cached copy is atomically replaced with the fresh one. This lazy revalidation populates the cache for the next user without penalizing the current one.

03

Cache-Control Header Syntax

The behavior is controlled by the Cache-Control HTTP response header. The directive stale-while-revalidate=<seconds> defines the grace period during which a stale response can be served. A typical configuration is:

  • Cache-Control: max-age=60, stale-while-revalidate=3600 This allows a fresh response for 1 minute, and a stale response for up to 1 hour while a background refresh occurs.
04

Stale-If-Error Resilience

Often paired with stale-while-revalidate, the stale-if-error directive provides an additional safety net. If the asynchronous background refresh to the origin fails (e.g., a 500 error or network timeout), the CDN can continue to serve the stale asset for a defined period. This pattern creates a resilient, self-healing cache that masks complete origin outages from end-users.

05

CDN Implementation Variations

Major CDNs implement this strategy with different terminology:

  • Fastly: Uses stale-while-revalidate natively with Surrogate-Key based purging.
  • Cloudflare: Supports the directive but also offers Always Online as a fallback.
  • Akamai: Implements similar logic via Cache Prefresh behaviors in its property configurations.
  • Varnish: Uses beresp.stale_while_revalidate in VCL.
06

Impact on Core Web Vitals

By guaranteeing an instant response from cache, stale-while-revalidate directly improves Largest Contentful Paint (LCP) and reduces Time to First Byte (TTFB). It eliminates the latency tail caused by synchronous origin revalidation, making performance predictable and consistently fast for all users, regardless of traffic spikes.

CACHE STRATEGIES

Frequently Asked Questions

Clear, technical answers to the most common questions about the Stale-While-Revalidate caching directive and its role in modern web performance.

Stale-While-Revalidate is a Cache-Control response directive that instructs a cache to immediately serve a stale (expired) cached response to the user while asynchronously fetching a fresh version from the origin server in the background. The mechanism works in three phases: first, the cache serves the expired content instantly, eliminating origin latency from the user's critical path. Second, the cache initiates a non-blocking request to the origin to populate the cache with a fresh response. Third, the newly fetched response replaces the stale entry, ready for the next request. This directive is specified using the syntax Cache-Control: max-age=60, stale-while-revalidate=3600, where max-age defines the fresh window in seconds and stale-while-revalidate defines the grace period during which stale content can be served while revalidation occurs. The result is that users never wait on origin latency, perceiving consistently fast load times even during cache misses.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.