Stale-While-Revalidate (SWR)
Stale-While-Revalidate (SWR) is an HTTP cache control extension that instructs the edge to serve an existing cached (stale) response instantly to the client, while asynchronously initiating a background fetch to refresh the cache. Users experience zero latency wait time while always receiving fresh data.
Technical Architecture & Deep-Dive
Traditional Time-to-Live (TTL) cache expiration forces the unlucky user who requests an expired item to wait for the complete origin roundtrip. If the origin is slow or executing complex aggregation, that user experiences noticeable lag.
SWR decouples cache freshness from response latency. Using `max-age=60, stale-while-revalidate=300`, the edge serves the cached payload instantly if requested within 360 seconds, and if older than 60 seconds, quietly dispatches a non-blocking background subrequest to update edge storage.
Key Architectural Benefits
Implement Stale-While-Revalidate (SWR) at the Edge
Deploy lightweight API aggregation, edge caching, and secret injection in minutes without Docker containers or complex servers.
Frequently Asked Questions
Q:How do I specify SWR in HTTP headers?
Include the `stale-while-revalidate` directive in your `Cache-Control` header, for example: `Cache-Control: public, max-age=60, stale-while-revalidate=300`.
Related Architecture Concepts
Edge Caching is the practice of storing computed API responses on globally distributed Point of Presence (PoP) servers situated geographically close to users. By serving cached responses directly from edge memory, requests achieve sub-10ms latencies while offloading 95%+ of traffic from origin databases.
Sub-10ms Edge Latency describes API response times that complete in under 10 milliseconds from client request to response receipt. This is achieved by terminating TLS and serving cached, aggregated, or transformed responses from edge Point of Presence (PoP) servers located near the user.
A Point of Presence (PoP) is an edge data center node located at an internet exchange point (IXP) containing servers that cache data, terminate TLS handshakes, and execute serverless compute close to end users, minimizing long-haul internet routing latency.
Ready-to-Use Edge Recipes
REST API rate limits hit fast from Frontend / Web. Cache 300s on GET calls where the query string scopes the response.
To cache coinmarketcap crypto pricing api at edge, set cacheTtlSeconds on a GET pipeline targeting https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest?symbol={{query.symbol}}. Identical query strings reuse Cloudflare caches.default before upstream sees traffic.