8 June 2016

Magento Varnish how to - part three


Part Three


Welcome back once again to the third and final part of this Magento Varnish mini series.  The first part discussed why you should be using Varnish on your store, the second part showed you some figures for the kind of performance benefit a Magento Varnish cache is likely to give, and this final part shows you just how straight forward it is to actually get a Magento Varnish cache up and running on your store by using the easy integration offered by Evolved Caching.

How do I setup a Magento Varnish cache?

 

Varnish cache can be a complex piece of software to setup and customise to your exact site requirements with the default configuration it ships with catering for some, but certainly nothing like all sites.  As discussed in part one of this series, it basically won't cache Magento's pages at all while running it's default configuration and so needs some work to allow it to cache correctly.

That's where Evolved Caching comes in.  While Evolved Caching is a highly performance driven, advanced Magento full page caching solution in it's own right, it also massively simplifies using Varnish to cache your store pages by way of it's caching key cookie.  This cookie contains a unique reference describing everything about each page, including data that is normally stored in the session.  The cookie is delivered to Varnish for every request, and so rather than caching only by URL - which is extremely limiting in the case of Magento, Varnish can then cache the full breadth of possible page variations Magento can generate, and this includes caching pages when products are in the cart, or the customer is logged in.

Evolved Caching takes care of everything related to adding dynamic content to the page, defining which pages should be cached (or not) and allows Varnish to just get on the with the job of serving super fast cached content.  You can also manage the Varnish cache from within admin including automatic cache clearing and warming of pages as they are updated.  Evolved Caching does all the hard work for you!

So how can you get a Magento Varnish cache for your store?  This is how to do it.

The easy integration is given by Evolved Caching so as a starting point you can grab your copy here and follow the quick setup guide to get running with that.  You also need a clean, unmodified install of Varnish and you can find full instructions here.

Once you have these basic requirements in place there are just three simple steps you need to follow to get your Magento Varnish cache.
  1. Use our vcl configuration file for either Varnish 4 or Varnish 3 in /etc/varnish/default.vcl on your server.
  2. Enable 'flush varnish cache' under the general options section of the Evolved Caching settings.
  3. Turn on 'enable cookie' under the caching key cookie section of the Evolved Caching settings.
That's it!

Now you just need to restart Varnish, and clear the Magento cache (including the Evolved Caching cache) and you should see Varnish serving cached content as you navigate the site.  the Varnish cache will also be cleared and populated as you use admin.  If you are not sure how to tell if Varnish is serving cache or not have a look at the bottom of our Evolved Caching Varnish documentation page.

Thanks for reading this mini series, we hope you find your Magento Varnish cache gives your store a real performance boost!

Magento Varnish how to - part two


Part Two


Welcome back to part two of this mini series on how to get a Magento Varnish cache on your Magento store.  Part one talked about why Varnish is something you should be using, and this part talks about the kind of performance increase you should be seeing by using Varnish on your store.  The next and final part shows you how easy it is to actually get Varnish working on your Magento using Evolved Caching.

Magento Varnish performance

 

So we've hopefully convinced you that using Varnish on your Magento store is a great idea, but what kind of performance increase are you likely to see?  This does depend to some extent on the spec of the server Varnish is running on, but whatever the spec Varnish is going to deliver it's cached content many, many times faster than having Magento process the request.  To give you an idea, here are some benchmarks we ran with an Evolved Caching Magento Varnish setup as described in the last part of this mini series.  The times shown below are the time it takes the server to generate a full page of HTML content to deliver to a customers browser (being the part that Magento is slow at), it doesn't take into account connection speed which can vary greatly for each user.

Home page

With standard Magento caches enabled the server takes an average of 175ms to build this page of HTML.

With Varnish serving a cached page the server takes an average of 18ms to build this page of HTML

Varnish is therefore around 10x faster in this case.

CMS page

With standard Magento caches enabled the server takes an average of 140ms to build this page of HTML.

With Varnish serving a cached page the server takes an average of 18ms to build this page of HTML

Varnish is therefore around 8x faster in this case.

Product page

With standard Magento caches enabled the server takes an average of 370ms to build this page of HTML.

With Varnish serving a cached page the server takes an average of 18ms to build this page of HTML

Varnish is therefore around 21x faster in this case.

Custom page

With standard Magento caches enabled the server takes an average of 480ms to build this page of HTML.

With Varnish serving a cached page the server takes an average of 18ms to build this page of HTML

Varnish is therefore around 27x faster in this case.


So you can see there is quite a difference in the time it takes the various pages to be built by Magento, while Varnish keeps a consistent 18ms to serve it's cached content throughout.  Because more complex pages take a longer amount of time to build by Magento, it makes sense that you see a greater performance benefit on these pages than on simple pages which are quick for Magento to build.  Category and product pages are obviously key to every Magento store and it's on these pages you are likely to see the greatest improvement.  So the longer and more complex your stores pages are, the greater performance benefit you are going to see by using Varnish.  If you want to see more, you can find the benchmarks for Evolved Caching here - the performance of Evolved Caching's own full page cache is virtually identical to Varnish so these figures are directly comparable to the same setup using Varnish.  Our store also uses Varnish so you are welcome to browse and test there as well.

Thanks for reading part two of this Magento Varnish cache mini series, the last part shows you how easy it is to get Varnish working on your Magento store by using Evolved Caching.

Magento Varnish how to - part one


Part One


Magento performance and speeding up Magento are hot topics, and ones which have seen a lot of discussion in the Magento community for years.  That's because it's something which every store owner should address and with Varnish being such a well established, fast, platform independent caching solution, using Varnish with Magento makes a lot of sense.

This mini series talks about using Varnish along side Magento in three sections.  Firstly it tells you why it's something you should be doing, secondly it shows you the kind of performance improvement you should see, and finally it shows you the super simple setup you need to get a Magento Varnish cache on your store using Evolved Caching.  This section looks at why you should be using a Magento Varnish cache on your store.


Why should I use a Magento Varnish cache?

 

Probably one the most noticeable things you have found when generally using and browsing your Magento store is that it's probably slower than you want it to be.  If it's that immediately noticeable for you, it's going to be immediately noticeable to your customers with the difference being they are probably going to end up on a competitors store which gives them a faster and therefore better shopping experience.  So the rather obvious solution is to improve the performance of your store with there being a couple of good standard options available.  The first is a Magento full page cache, and the second - being the one we are talking about in this mini series, is to use the external caching solution Varnish.

You are probably familiar with Varnish to some extent or have heard about it at least, but what exactly is it?  Well it's a highly configurable, advanced caching system which sits on the server between your customers browser and your Magento store.  It processes requests coming in to the server, and if it can serve it's own cached content then it will, otherwise it will pass the request to Magento to be processed as it would be without Varnish.  If it serves it's own cached content then the request never even reaches Magento.

Varnish is a time proven, configurable, and perhaps most of all very fast caching solution that will cache and serve full pages worth of HTML content with the result being the pages on your store will load extremely fast for your customers if you start using it.  That's great, but to gain these benefits typically a certain amount of work is needed by a developer or sysadmin to properly configure Varnish to work with Magento.  The problem is that the way Varnish caches by default doesn't fit well with Magento - it only caches pages by URL, and doesn't cache anything if the user has any cookies set.  So with this default Varnish configuration running you will get virtually no cached pages served at all and this is because Magento creates cookies right from the first request.  Additionally some important information about how pages should be displayed are stored in the users session and don't have to show in the URL, for instance category sorting parameters like products per page, sort order and so on.  You can soon see that because of these kinds of things using Varnish in Magento isn't quite as straight forward as it could be and the default configuration needs some changes to work happily with Magento.

But with these initial setup hurdles aside (which we will tell you how to easily overcome in part three of this mini series), Varnish is a caching system which it is well worth implementing on every Magento store as the performance gain your customers will see is significant.


Why is Varnish so great for Magento?

 

Magento is a heavy application with millions of lines of PHP code.  Each line of PHP takes a finite amount of time to run, and even though the time taken to run a single line is very small, when you start talking about tens of thousands of lines of code running for every request, you can really start to understand how Magento can be slow - it's just because there is so much code to process, the server can't do it any quicker.  So without increasing the spec of the server to run PHP quicker, the other option is to run less PHP to deliver a page to the browser.  Well that's exactly what Varnish and indeed all of the Magento full page caching solutions do in order to speed up Magento - it's not making the server any 'faster' it's just allowing the server to do a great deal less work for the same end result.

There is one key difference worth noting between a good Magento Varnish implementation and most of the Magento full page caching solutions, and one which makes Varnish significantly faster.  As noted above, Magento can be slow purely because of the number of lines of PHP code which need to run, and a large percentage of these lines need to run to 'startup' or initialize Magento.  Initializing Magento is unfortunately something which needs to happen for every single request Magento receives.

When you serve full page cached content certain areas of the page cannot be cached, for instance the mini cart, or the login links as this content changes dependant on the actions the customer takes such as adding a product to the cart, or logging in to the store.  So to have these parts of the page display the right information you need to collect that data uncached (called dynamic content) from Magento, but in order to get this information you need to initialize Magento - which as explained above is slow to do.

The problem here is that most full page cache solutions collect this dynamic data before they deliver the cached content to the browser and in the process significantly add to the time it takes to serve the cached content, with the result being pages load slower than they should in your customers browser.  Evolved Caching actually work differently to this - very much the same as Varnish.  The full page cache content is sent to the browser immediately meaning the pages load very quickly for your customers, with dynamic content then collected after this.  This means you don't need to initialize Magento at all in order to serve the cached page which makes for much faster times serving cached pages to the customer than most Magento full page cache solutions.

So to cut a long story short, Varnish is a such a good option for Magento because when it's configured correctly, like with Evolved Caching, you don't need to initialize Magento in order to serve a cached page.

Thanks for reading part one of this mini series, part two talks about the kind of performance increase you are likely to see by using a Magento Varnish cache on your store, and part three covers the easy steps you need to follow to get this caching working on your store.