Using a Varnish® CDN Stack with KeyCDN
What is Varnish® Cache?
Varnish® Cache is an open source, frontend accelerator also known as a caching HTTP reverse proxy. Varnish® can be used to cache both dynamic and static content and has shown to dramatically increase website speed and server performance.
Adding Varnish® to your stack is easy in that it can be done without any code changes. Additionally, when you start tweaking the settings in Varnish® and experimenting with the advanced features, caching capabilities and overall performance can increase even more.
Why use a reverse proxy?
As mentioned, using a reverse proxy such a Varnish® can drastically increase website speed. This is due to a few factors:
- Objects can be served more quickly from the cache server than the origin server as the cache server's workload is less intensive and less mixed.
- Offloading assets that infrequently change, such as CSS and JavaScript files, reduce the load on the origin server, allowing it to perform more useful requests. This allows for the origin server to render pages quickly as it is not serving static content on every reload.
- In the case that Varnish® is used as a dedicated server, the origin server receives fewer inbound connections since inbound connections would now see the caching proxy as the first server. The inbound connections that are received by the origin server are therefore more responsive, spending less time in context thread switching.
- Using Varnish® cache on the origin server is a great technique to reducing the time to first byte (TTFB). Varnish® helps reduce the processing time to the backend database enabling the TTFB to be achieved faster.
How a Varnish® CDN stack works with KeyCDN
Using a Varnish® CDN stack increases website deliverability speed and helps further reduce origin server load. Varnish® cache works with KeyCDN very similarly to how your origin web server works with KeyCDN. If the origin server serves assets from Varnish® cache to KeyCDN, KeyCDN will treat Varnish® just like any other origin and cache those assets. If the origin serves assets from the web server, then KeyCDN will cache those assets as expected.
It makes no difference where the assets are coming from (either Varnish® or the origin server) KeyCDN treats both sources equally and will cache the objects either way.
The Content-Length
HTTP header is enabled by default, however, if you have it disabled you will need to enable it again in the header on your origin server. This is mandatory for a cache HIT, otherwise you will get a cache MISS.
Checking if the content was served from Varnish®
There are two ways to check if the content that it being served to your CDN is delivered from Varnish.
- Check the
X-Varnish
HTTP header. For a cache HIT,X-Varnish
will have two IDs. The ID of the current request and the ID that populated the cache. - Check the
Via
HTTP header for something similar to1.1 Varnish
. This tells you that the content was served from Varnish®.
Summary
Using Varnish® cache can greatly help reduce the load on your origin server and help deliver content faster to your visitors. Using Varnish® cache in conjunction with a CDN further increases the speed of your website and is a great addition to sites that are delivering content to users across the globe.