Cloudflare Cache Everything + Varnish + Edge Cache TTL

So I have set up Cloudflare Cache Everything page rule on Managed Wordpress Hosting (Dreampress) that has Varnish server caching. Ever since I implemented CF Cache everything page rule, the Varnish
X-cache: MISS always
I contacted hosting and showed them my page rules.

They came up with this reply:

I understand the problem now. The Edge Cache will always override our
headers, which is why the Varnish cache will not work paired with this
feature:

https://blog.cloudflare.com/edge-cache-expire-ttl-easiest-way-to-override

“With Cache Everything, we respect all headers. If there is any header in
place from the server or a CMS solution like WordPress, we will respect
it. However, we got many requests from customers who wanted an easy way
to override any existing headers. Today, we are releasing a new feature
called ‘Edge cache expire TTL’ that does just that.”

Leaving just Cache Everything will respect our headers and both
CloudFlare and Varnish will work.

Is this correct? So in order to keep the varnish cache working, do I need to remove Edge Cache TTL from the page rule? By removing this option, will Cloudflare not keep the content on the CloudFlare edge network?

What is the best implementation of Edge Cache TTL?

  • I do not see Edge Cache TTL - Respect All Existing Headers option (as shown in your blogpost)
  • Should I simply remove the Edge Cache TTL?

This is expected.

A request comes from the user to Cloudflare. CF do not have a copy of the asset, so they fetche that asset from your Origin, which is running Varnish. Varnish probably does not have that asset, so it fetches it from its backend, and adds the MISS header. That asset (including its MISS X-Cache header) gets inserted in the Cloudflare cache.

The next request from a user to Cloudflare is a HIT in Cloudflare, so that asset gets returned. But the asset in Cloudflare contains the X-Cache: MISS header. If you have not done anything special, the TTL on both files will align, so once the file has expired in Cloudflare it will also be expired in Varnish.

The Varnish cache is still doing a job, it just might not be obvious all the time. If you don’t have tiered caching then the next POP that makes a request to your Origin will see a Varnish HIT, and if you do a Cloudflare cache purge your Varnish will kick in to shield your origin.

Every situation is different, but the Edge Cache page rule is essentially to override any existing cache control parameters. (or add that behaviour if it is missing). If you already have max-age or s-maxage being set in VCL or on the web server, then the Edge Cache TTL is generally not needed. You might also look at the CDN-Cache-Control header, which gives more granular instructions to Cloudflare.

@thijs is on the Community, and might have better information or recommendations on using Varnish and Cloudflare. (He literally wrote the book on Varnish). I use Varnish and Cloudflare successfully at a decent scale without issues, and with no Edge Cache page rules, and Cache Everything in place.

3 Likes

Hi @michael,
Thanks for the input. Yes. The Varnish was working in the back even though X-Cache: MISS
However, upon removing the Edge Cache TTL, the X-Cache: is HIT now for most of the pages.
It will be testing the performance and cache hit ratio with and without Edge Cache TTL.

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.