Advanced Caching Part 1: Google Cloud CDN and Varnish
Google Cloud CDN is a lightweight content delivery network solution. It offsets the load from your backend. And of course, it dramatically decreases HTTP response time for your end users. When this is not enough, there is Varnish Cache.
This blogpost walks you through implementing Varnish’s advanced caching rules in Google Cloud. This can be done behind the Google Cloud CDN, or completely on its own. It addresses the need of editing the HTML response body, so that the pseudo-dynamic content can be cached.
Varnish Cache closes the CDN gap with Google Cloud. Varnish allows you to set caching rules declaratively. This is done using a feature called Edge Side Includes (ESIs).
1. Google Cloud Marketplace
Login to your Google Cloud Platform (GCP) console and navigate to the Marketplace
2. Select a Varnish Image
Select a Varnish Cache image*, to be installed on a Google Compute Engine (GCE) virtual machine (VM)
3. Deploy the Image
Launching will navigate you to the GCE section of the GCP console
4. Select Machine Type
Depending on your use case, you may want to select powerful VM
Alternatively, consider manually deploying Varnish in a Google Kubernetes Engine (GKE) cluster
5. The Image Deploys Seamlessly
The GCP Marketplace image graciously configures the VM and networking ports for you
6. Edge Side Includes (link)
ESIs allow you to inject content into the response body
On the Varnish machine, create web file that has the <esi> tag
Here we put PHP in the HTML file, to demonstrate scripting apart from ESI
Next we create a second web file, which the above <esi> tag references
This second file contains JavaScript, which will run in the end user’s browser
7. ESI Works in Google Cloud!
In the below HTTP response, the PHP code runs on the server
However, the Varnish <esi> tag inserted the JavaScript as desired
This HTTP response is thus cached by Varnish
Effectively this is caching of a pseudo-dynamic web page
Google Cloud CDN Best Practices (link)