Magento 2 and Varnish Cache

Magento 2

Magento 2 has been in development for several years, through the Merchant Beta release it was announced that there will be out of the box support for Varnish 4. Awesome.

Magento 2 improvements

This is great news on many fronts as modern Magento releases (CE 1.8+ and EE 1.13+) have included a form keys feature that has made HTML caching (and good Magento performance!) harder to implement. The form keys issue is a security feature introduced that makes each HTML page unique and hence cannot be stored in Varnish.

The work around until now has been to use a Magento extension (Such as Nexus Turpentine) however this can introduce further configuration / complexity to your Magento implementation. Without other options however, this is still the best method to achieve speed / performance improvements in Magento 1.

Magento 2 Varnish design

The sourcecode within the Magento 2 codebase that relates to Varnish Cache uses a similar pattern to the existing Turpentine extension where a custom HTTP header is sent with each page response (The custom header within Magento 2 is called “X-Magento-Tags-Pattern”).

The custom header contains a list of tags that identify features of this HTML page, For example this might be a Category page response that contains an image or pricing for Product ID 123.

If Product ID 123 is then updated, Magento will send a PURGE message to Varnish that any item in its cache that has a HTTP header (X-Magento-Tags-Pattern) with the value of “Product ID 123” needs to be removed from cache. This allows the actual Product page and any associated pages to be cleared from cache when new versions are generated without clearing the cache for all other content. This is good for performance!

Magento Implementation

Implementing Magento 2 and Varnish

In order to run Varnish on your Magento 2 implementation you need to install Varnish and then deploy a Varnish configuration file (A Varnish VCL file) into your Varnish implementation.

Within the process to implement Varnish in front of Magento 2 is to copy paste this code into the portal.

If you have any issues / questions about setting up Magento 2 Varnish please touch base through, You can have a globally distributed Varnish implementation running in seconds, with the features normally missing from a Varnish install - simple access to metrics and logs

Blog Categories

Interested in articles about a specific topic? Click on a category to see all related content. Sign up

Want to get started improving your website performance, scalability, and security? Sign up for a 14 day free trial of and see what we can do for you!

Get started