Stale-While-Revalidate: Serve Old First, Refresh in the Background
Stale-while-revalidate serves the old copy first and refreshes in the background. If the refresh keeps failing, the copy never becomes fresh again.
When a copy expires, the LB cache serves the old copy first, then refreshes in the background. If the refresh keeps failing, the copy never becomes fresh.
The LB cache on the proxy route does not read the bypass header (A Bypass Header Works Only Where a Rule Reads It). But its copy had expired long ago. Why did it keep serving it to Akamai?
- In the HTTP standard, this behavior is called stale-while-revalidate (RFC 5861). Our LB cache turns it on with its own setting.
- The setting is the number of seconds after expiry during which the cache can still serve the old copy. On our proxy backend, the value is 86400 seconds (1 day).
- Nobody chose this value on purpose. It is the default (TTL Is Set in Three Places).
- The good side: replies are fast. When the origin is down, users still get the file.
- The cost: if the background refresh fails every time, the copy stays expired.
The Old Copy Carries a Large Age Forward
- When the LB cache hands the old copy to Akamai, the
Ageis already above 3600. - Akamai computes the fresh time left as 0 or negative.
- So Akamai can keep it for only 1 second (
max-age=1), and the next request must ask again. - This is what Cache Freshness: Fresh, Stale, and 304 says: a cache in front is only as fresh as the copy it receives.
This backend has no cache setting in IaC. But the cache is on, so it has a full set of defaults. The 86400 that lets the bug happen hides in a default that nobody wrote down.
“Serve old first” is a kind of protection. It works only if the background refresh really succeeds.
Sources: RFC 5861: HTTP Cache-Control Extensions for Stale Content
Related: Cache Freshness: Fresh, Stale, and 304, TTL Is Set in Three Places, A Bypass Header Works Only Where a Rule Reads It, The Proxy Rejects HEAD, So the Refresh Always Fails. Or go back to the series overview.