Varnish Cache for Magento 2 - Configuration Suggestions

Magento 2, a complete reconfiguration of the popular ecommerce platform, is built for speed and is a vast improvement in website performance over Magento 1.x versions. One of the major differences in Magento 2 is that it is built to integrate with Varnish Cache, an extremely fast http-accelerator that is configurable to cache a variety of content, from static objects such as pictures to Javascript snippets and even full HTML documents.

When set up correctly, Varnish Cache can vastly improve the speed of any website, and for ecommerce sites such as those using Magento, an improvement in speed of even 1 second can translate to improved conversion rates, larger cart sizes, and increased revenueb .

Varnish Cache is configured using its own language, Varnish Configuration Language or VCL. Magento has provided a default VCL file for Magento 2 users which includes standard instructions to set up caching on a Magento 2 site. This VCL file utilizes Varnish Cache version 4. While this VCL will get Magento 2 sites started with cache setup, Section suggests adding several configurations to allow sites to cache more content and experience faster speeds and a higher cache hit rate. Achieving a higher cache hit rate also reduces load on the website origin server, resulting in lower hosting costs and allowing the origin server to focus on critical applications such as the checkout process.

Varnish Cache VCL Additions for Magento

Here are Section’s suggested additions to the Magento 2 default VCL file which will give websites a more advanced cache setup. These suggestions can also be utilized with other instances of Varnish Cache not related to Magento, and we would recommend every site using Varnish Cache implement these configurations.

VMODs:

Varnish Modules or VMODs are extensions which add to what can be configured directly in VCL. The Varnish Cache website keeps a list of VMODs, and you can learn more about how they work to add functionality to Varnish Cache here.

Section recommends adding several VMODs:

  • Varnish UUID Module/ import uuid; Allows you to generate unique random ID’s or hashes based on values that you provide. Can be used to build features that require unique IDs or hashing of strings.

  • Varnish GeoIP Lookup Module/ import geoip; This allows you to look up a visitor’s GeoIP address and include instructions that take their location into account. This is especially important for sites with global visitors as it can send them to the correct site (www.mysite.co.uk vs www.mysite.com). You will need a GeoIP library installed to lookup the GeoIP: at Section we are able to gather city-level information.

  • Header VMOD/ import header; This allows you to manipulate requests more than in the default VCL, making it easy to set and handle cookies and group and consolidate headers. This module is now included in a Varnish Cache Module Collection that has been put together - other features in the collection include variable support, advanced cache invalidates and more. See the full collection here.

Normalizing query strings

When visitors come to your site through an ad, email, or other tracked link the URL they visit will usually have a string attached so that activity can be tracked. For example, if traffic is served from google ads the string will include “gclid=XXX.” If Varnish Cache is not configured correctly, the cache will see each of these as unique URLs which would not be served from the cache, even though the page content is the same. Magento’s default VCL file includes instructions to remove the gclid so URLs with the gclid paramater can be served from cache, however we recommend excluding several other common tracking tags such as utm_source (also Google tracking) and mc_cid (mailchimp email tracking):

utm_source|utm_medium|utm_campaign|gclid|cx|ie|cof|siteurl|mc_cid|mc_eid

Overriding cache control response headers to improve browser caching

When setup correctly, browser and server caches should work together so that when possible content is served from a browser cache which reduces the requests to both your web server and your cache server. For ecommerce visitors who go back to the same site and product pages over and over before making a purchase, the ability to serve product photos and more from a browser cache is very valuable.

At Section we add instructions that allow Varnish Cache to see what objects are stored in the browser, which therefore do not need to be fetched from the server cache. The default Magento value for caching content is just a day - our VCL lets you set browser cache times to longer for items such as logo files that are unlikely to be changed each day. An example of how we configure Varnish Cache to cache items for longer can be seen in Github under the vcl_backend_response section. Read more about overriding cache control response headers to cache objects for longer times.

Caching API calls

Some API calls are cacheable, and we recommend adding configuration that caches these API calls for faster response and offloads repetitive API calls from origin servers.

Sharing a cache between two Magento stores:

The Magento 2 default VCL specifically prevents users from sharing an object between domain names with the below section. However, sites can share a cache for common objects - for example, all assets under /assets/images/ could be shared on country-specific versions of the same site. If desired, you can override this rule for a specific URL so that one cache can be used for multiple Magento store domains.

 if (req.http.host) {
     hash_data(req.http.host);
 } else {
     hash_data(server.ip);
 } ```


### Device detection ###
The Magento default VCL doesn’t include device detection, and assumes that websites are responsive so that they mobile and desktop browser would be served the same page. However, some sites are setup with a separate mobile site such as m.mysite.com, and we recommend setting up VCL so the cache can be split by device type.

## Get started with Varnish Cache for Magento on our CDN ##

Section has a [quick-install version of Varnish Cache for Magento 2](https://marketplace.magento.com/sectionio-metrics.html) available in the Magento Extension Marketplace: this allows Magento users to pull in the Magento 2 default VCL and go live on our global CDN all within the Magento portal. To make the changes detailed above for additional Varnish Cache benefits, users can simply access and edit the VCL file in the Section portal.

To install Varnish Cache for Magento 1, [sign up for a Section account](https://console.section.io/) and [follow these instructions](/docs/magento/#magento-19-and-earlier) or [contact us](/contact-us/) for help.

<a href="http://get.section.io/magento-guide-performance-speed/">
<img src="/assets/images/blog/oct-2016/ebook_cta1.png" alt="magento ebook" style="width:600px;">
</a>

Similar Articles