Question Title
Google Cloud CDN stale-while-revalidate doesn’t work as expected for some requests
Content
Background
We are currently using Google Cloud CDN with the following cache configuration:
-
Cache-Control Header:
public, max-age=300, stale-while-revalidate=86400 -
Cache expiration is set to 300 seconds.
-
After expiration, the cache should serve stale content for up to 86400 seconds (1 day) while asynchronously fetching fresh content from the backend.
-
Cache Mode:
The Cloud CDN cache mode is set toUSE_ORIGIN_HEADERS.
Expected Behavior
When the cache expires, we expect the following behavior:
- The client (browser) receives the stale cache immediately.
- Cloud CDN fetches fresh content asynchronously from the backend to update the cache.
Observed Behavior
During testing, we observed the following behaviors:
-
Case where it works as expected:
When a request is sent after the cache expiration (300 seconds), Cloud CDN correctly serves the response from the cache (response_from_cache). Immediately afterward, the logs show that Cloud CDN asynchronously fetches new content from the backend (a request with the user agentCloud-CDN-Google (GFE/2.0)). -
Case where it does not work as expected:
Approximately 3 hours later, another request from the same browser results in Cloud CDN fetching content directly from the backend (response_sent_by_backend).
In this case, no asynchronous backend request (with theCloud-CDN-Google (GFE/2.0)user agent) was recorded.
Investigations
-
Client Request Headers:
The request headers sent from the browser were inspected, and there was noCache-Control: no-cacheor any other header that could bypass the cache.
Additionally, no cache-bypass actions (e.g., Shift + Reload) were performed during testing. -
Cloud CDN Cache Logs:
The logs for the problematic request include the followingcacheDecisionentries: [ “REQUEST_CACHE_CONTROL_NO_CACHE”, “RESPONSE_HAS_CACHE_CONTROL”, “RESPONSE_CACHE_CONTROL_PUBLIC”, “RESPONSE_HAS_CONTENT_TYPE”, “CACHE_MODE_USE_ORIGIN_HEADERS” ] -
Cloud CDN Cache State:
During the problematic request,cacheLookup: trueindicates that the cache was present. However, the response was logged asresponse_sent_by_backend.
Questions
- Why does
stale-while-revalidatenot work as expected for some requests, resulting in a direct fetch from the backend? - What could be potential conditions or misconfigurations that lead to this behavior? Are there any additional points we should check to identify the root cause?
Additional Information
- Service: Google Cloud CDN
- Cache Mode: USE_ORIGIN_HEADERS
- Cache-Control Header:
public, max-age=300, stale-while-revalidate=86400
I can provide additional logs or details if needed. Thank you for your help!