0
votes

I'm using Google Cloud CDN to cache an HTML page.

I've configured all the correct headers as per the docs, and the page is caching fine. Now, I want to change it so that it only caches when the request has no cookies, i.e. no cookie header set.

My understanding was that this was simply a case of changing my origin server to add a vary: cookie header to all responses for the page, then only adding the caching headers Cache-Control: public and Cache-Control: max-age=300 when no cookie header is set on the request.

However, this doesn't work. Using curl I can see that all caching headers, the vary: cookie header, are set as expected when I send requests with and without cookies, but I never get cache hits on the requests without cookies.

Digging into the Cloud CDN logs, I see that every request with no cookie header has cacheFillBytes populated with the same number as the response size - whereas it's not for the requests with a cookie header set with a value (as expected).

So it appears like Cloud CDN is attempting to populate the cache as expected for requests with no cookies, it's just that I never get a cache hit - i.e. it's just cacheFillBytes every time, cacheHit: true never appears in the logs.

Has anyone come across anything similar? I've triple-checked all my headers for typos, and indeed just removing the vary: cookie header makes caching work as expected, so I'm almost certain my configuration is right in terms of headers and what Cloud CDN considers cacheable.

Should Cloud CDN handle vary: cookie like I'm expecting it to? The docs suggest it handles arbitrary vary headers. And if so, why would I see cacheFillBytes on every request, with Cache-Control: public and Cache-Control: max-age=300 set on the response, but then never see a cacheHit: true on any subsequent request (I've tried firing hundreds with curl in a loop, it really never hits, it's not just that I'm populating a few different edge caches)?

2
Vary: Cookie is not supported as a cache key - content is not cached when it is used, as cookie values are very often unique per user. See cloud.google.com/cdn/docs/caching#vary_headers which hints at only a set of Vary headers being supported.elithrar
@elithrar thanks - I was wondering if this was it, but the wording is ambiguous Responses with Vary headers are cached only if the header has one of the values listed in Cacheable content - but then Vary is not mentioned at all in Cachable content - do you happen to know if it states explicitly anywhere in the docs which vary headers are and aren't supported?davnicwil

2 Answers

0
votes

As per the GCP documentation[1], it is informed that Cloud CDN respects any Vary headers that origin servers include in responses. As per this information it looks like vary:cookie is supported by GCP Cloud CDN since any Vary header that the origin serves will be respected by Cloud CDN. Keep in mind though that this will negatively impact caching because the Vary header indicates that the response varies depending on the client's request headers. Therefore, if a request for an object has request header Cookie: abc, then a subsequent request for the same object with request header Cookie: xyz would not be served from the cache.So, yes it is supported and respected but will impact caching (https://cloud.google.com/cdn/docs/troubleshooting-steps?hl=en#low-hit-rate).

[1]https://cloud.google.com/cdn/docs/caching#vary_headers

0
votes

I filed a bug with Google and it turns out that, indeed, the documentation was wrong.

vary: cookie is not supported by Cloud CDN

The docs have been updated - the only headers that can be used with vary are Accept, Accept-Encoding and Origin.