The Freshness Dilemma
A cache faces a tension. Fresh content needs frequent origin fetches, but those fetches add latency. Stale while revalidate resolves this by decoupling serving from refreshing.
How It Works
A response carries a freshness lifetime and an extra stale while revalidate window.
- While fresh, the cache serves directly.
- After it expires but within the window, the cache still serves the stale copy at once.
- At the same time it triggers a background fetch to origin to refresh the entry.
The user never waits on the refresh. The next user gets the updated copy.
Why It Helps
- Low latency: nearly every request is served from cache without blocking.
- Smooth origin load: refreshes happen quietly in the background.
- Graceful aging: content drifts only slightly out of date, never far.
Limits And Tuning
The stale window is a contract about how old content may get. Short windows keep content fresh but force more blocking fetches when traffic is sparse. Long windows favor speed but risk serving outdated data.
A related directive, stale if error, lets the cache serve stale content when origin is unreachable, improving resilience during outages.
Key idea
Stale while revalidate serves an expired copy instantly while refreshing it in the background, so users avoid waiting on origin and content stays only slightly out of date.