8 June 2016

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.

No comments:

Post a Comment