Jackson MacKenzie

How Does Varnish Cache Work with a CDN like Akamai?

Blog Post created by Jackson MacKenzie Employee on Dec 10, 2014

Reverse proxies in front of webservers are becoming more and more common in the high-traffic world. Whether it be for static content or dynamic output caching, benchmarks show that a front-end accelerator like Varnish can drastically improve delivery speeds & server performance.


Source: PalePurple.co.uk


Now combine that with Akamai, and suddenly things start to get ridiculously speedy! Things also start to get confusing. So how can you get this cool technology to play well with your CDN? Let's start with the basics.

What is Varnish Cache?


Varnish Cache is a web application accelerator, also known as a caching HTTP reverse proxy. Varnish is not a CDN, but rather an accelerator installed in front of an HTTP origin server. Varnish's configuration decides what content should be served from Varnish, and how the request or response should be altered.


Source: 3scale.net


How does Varnish work with Akamai?


Akamai can work great with Varnish! In the diagram above, Akamai functions outside of the blue box. Akamai will treat a Varnish-enabled origin exactly like any other origin. If the origin serves an object from Varnish, Akamai will cache that object from Varnish. If the origin serves an object from the webserver, Akamai will cache that object from the webserver. In other words, Akamai does not bypass Varnish, unless this is specified in your headers or Varnish configuration. If there is stale content on Varnish, Akamai will cache that stale content.


How can I see if an object was served from Varnish before getting to Akamai?

  1. Check the X-Varnish response header. If it has two IDs, then the request came from Varnish cache. The first number is the current request ID. The second number is the ID of the request that populated the cache.
  2. Check the Via header for something like Via: 1.1 varnish. This means the request came from Varnish cache.


What are the best practices for updating content on Varnish-enabled origin servers?


Always purge varnish cache before running an Akamai purge! Follow these steps to ensure that Akamai does not receive stale content from Varnish:


  1. Update the content at origin
  2. Request the content directly from origin and ensure content is updated (this will require spoofing)
  3. If you have multiple origins, repeat step 2 on all origins
  4. Clear Varnish cache for the content (see their website for detailed instructions)
  5. Request the content directly from Varnish and ensure the content is updated (this will require spoofing)
  6. Clear the content from Akamai cache using CCU, ECCU, or an API call


Are there alternatives for my webserver?


There are a lot of other front-end proxies out there that can help with that extra layer of caching—some are built into the webserver. Feel free to share your experiences and benchmarks here. It's awesome to see how these two technologies speed up the web!