Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Google Cloud CDN `stale-while-revalidate` doesn't work as expected for some requests

### **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 to `USE_ORIGIN_HEADERS`.

#### **Expected Behavior**

When the cache expires, we expect the following behavior:

1. The client (browser) receives the stale cache immediately.
2. Cloud CDN fetches fresh content asynchronously from the backend to update the cache.

#### **Observed Behavior**

During testing, we observed the following behaviors:

1. **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 agent `Cloud-CDN-Google (GFE/2.0)`).

2. **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 the `Cloud-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 no `Cache-Control: no-cache` or 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 following `cacheDecision` entries: [ "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: true` indicates that the cache was present. However, the response was logged as `response_sent_by_backend`.

#### **Questions**

- Why does `stale-while-revalidate` not 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!
Solved Solved
0 1 258
1 ACCEPTED SOLUTION

Hi @yatsuda_retbit,

Welcome to Google Cloud Community!

It appears that a more thorough investigation for your project is necessary. For more detailed insights you may reach out to Google Cloud Support for assistance. Our 1:1 support team has specialized expertise in diagnosing underlying problems. When contacting them, provide all your comprehensive details and screenshots. This will help our 1:1 support team better understand and address your issue.

Was this helpful? If so, please accept this answer as “Solution”. If you need additional assistance, reply here within 2 business days and I’ll be happy to help.

View solution in original post

1 REPLY 1

Hi @yatsuda_retbit,

Welcome to Google Cloud Community!

It appears that a more thorough investigation for your project is necessary. For more detailed insights you may reach out to Google Cloud Support for assistance. Our 1:1 support team has specialized expertise in diagnosing underlying problems. When contacting them, provide all your comprehensive details and screenshots. This will help our 1:1 support team better understand and address your issue.

Was this helpful? If so, please accept this answer as “Solution”. If you need additional assistance, reply here within 2 business days and I’ll be happy to help.