Magento performance optimisation: APC, Memcached and Varnish

  • 1
  • December 13, 2013
Douglas Radburn

Douglas Radburn

Head of Technical

We’re always trying to improve the performance of the Magento Community stores that we look after.

Once we’re done streamlining the application itself and making sure that we’ve done everything we can, we look at the platform itself. We do it this way to make sure that we don’t miss anything out. Server extensions such as Varnish make quick sites use less resources, not neccessarily slow sites quick – although that can also be the case!

All the software mentioned in this post can be set up to work concurrently on a Magento installation. There are a lot of different configuration options that can work, and some that will no doubt work better depending on your configuration. We’re used to Red Hat Linux servers running standard LAMP stacks. We’ve played with NGinx and PHP-FPM (both with Apache and NGinx) but that’s for a later post.

Using APC as an OPCode Cache module

Starting with the PHP 5.5 branch, Zend Optimizer+ is directly built in, so there will be no need for this step moving forward. In the meantime, if you’re running an earlier version, you’ll want to install an OPCode Caching module. Our favourite is Alternative PHP Cache – APC.

PHP is an interpreted language. This means that each time a PHP snippet is requested, it reads all of the files and “compiles” them into something that the server can execute – OpCode. OpCode caching caches this generated OpCode, leading to decreased timings when repeat requests are made.

Magento often has hundreds of included files in a single request meaning some real savings can be made.

OpCaching with APC is system-wide. There is no need to set up Magento to use it – PHP will use it by default.

If you want to know more about OpCaching, I’d recommend reading through this It has some interesting information and a couple of really good diagrams to explain how PHP caches the OpCode and how it uses it.

Installing APC

Depending on your platform, you should be able to install APC using your package manager.

yum install pecl-php-apc or through PECL pecl install apc

You can look at the apc.ini to fine-tune your settings then. The only one you have to make sure of, is actually enabling it!

It’s worth looking at a value between 64 and 256Mb for apc.shm_size configuration value (depending on your server). It’s also worth increasing apc.num_files_hint to 15000 or maybe even more. Magento contains upward of 20k PHP/PHTML files. APC will cache only 1000 files by default.

Remember to restart Apache/HTTPd or NGINX when you’re done so that it loads the module.

service httpd restart

service nginx restart


“Memcached is a high-performance, distributed memory object caching system, generic in nature, but originally intended for use in speeding up dynamic web applications by alleviating database load.” – Simply put, Memcached alleviates Magento’s heavy hits on your database by caching data away from it.

You’ll need to install Memcache (a PHP extension), and install Memcached – the Memcache Daemon on your server. Across a load balanced / multi server solution, Memcached only has to reside on one server, and the others can all use it. This provides a centralised source for cache management.

yum install pecl-php-memcache pecl-php-memcached or through PECL pecl install memcached memcache

As per APC, remember to restart Apache/HTTPd when you’re done so that it loads the module.

Memcache integration with Magento is much closer – remember, with APC, we’re closer linked to PHP. So, we have to make Magento aware of Memcached. How do we do this?

Magento offers a few examples on their site of how to configure this, but no definitive by-the-book example really. The key to this is a change to your local.xml file. Within your local.xml file, you should notice the following block:

Within this global section, we need to add the following block.

Memcached can run on any number of ports, 11211 is the default. If you’re using a shared cache storage, change the file_name_prefix per store to something more relevant, or run memcached instances on seperate ports per store; or both. If you’ve got a lot of front-end servers, dedicate one of them to be your memcached server and make all the others connect to it. That way, they’ll all store their data in one memcached instance. This makes the user bouncing between a number of servers a lot smoother.

It’s worth pointing out that Memcache only supports one level of key-value relationships. This means that it can’t actually store Magento cache tags. This means that you should either specify a slow back-end to maintain the cache content tag relationship, or, alternatively, don’t define one – purposefully break this.

Redis WILL allow cache tags, and can be used as a direct replacement for Memcached.


Varnish is a caching reverse proxy. It’s actually also a http accelerator, it can act as a load balancer and a fail-over system. As a basic concept, it will cache the generated HTML from a page, and serve that to a user rather than making a call to the Magento Framework. This leads to some pretty speedy load times. Magento is very slow to initialise, and if you want your first byte speed down to anything under one second, let alone under half a second – then full page caching is the only way to achieve this.

As mentioned at the beginning of this article, your store should already be fast and you should be looking to reduce server resource – however, Varnish is a great way to reduce your speed to < 300ms. Varnish requires at least a single page view in order to prime the cache, so it means that your un-cached performance still needs to be high. There are cache-warmers out there (crawlers that will run over your site and prime the cache ready for customer visits) – but we’ve not found any we’d recommend thus far.

Usually, Magento stores fall into two categories, and these will give you an idea of how to configure Varnish.

If your site has relatively low traffic, then you’re going to have a bigger trade-off. Either your TTLs need to be high to ensure a greater hit-rate, or you get up-to-date content. It’s a fine line between them – obviously you can’t have aggressive caching, but frequent changes to content. They don’t play well together. You could, in theory run some sort of cache primer, but I’d expect that it wouldn’t be worth it due to having potentially thousands of URLs.

If you’ve got a busy site, then you should be able to get away with low TTLs, and still see a good hit-rate from Varnish. This also means that you can update content more frequently.

So, let’s install Varnish.

yum install varnish

By default, if installed successfully, once started, varnish will listen on port 6081.

netstat -taupen | grep :6081

Next, you’ll need to configure /etc/varnish/default.vcl

There are a couple of Varnish plugins for Magento, and the one we like is: – by default, this comes with a VCL that can be used alongside Magento.

Download and install the plugin, then grab the provided VCL. Backup your default.vcl and copy this one over in it’s place.

You’ll need to move your back-end onto the port that you’re going to ask Apache to listen on. So, you’d usually expect that:


But you’re then going to create:


Thus, anything that Varnish can’t serve, Apache will.

So, modify the port n the back-end default and admin sections to listen on port 8080

backend default {
.host = "";
.port = "8080";

backend admin {
.host = “”;
.port = “8080”;
.first_byte_timeout = 18000s;
.between_bytes_timeout = 18000s;

Now change your httpd.conf to listen on port 8080 instead of 80, and change your NameVirtualHost definition to port 8080 too.

NameVirtualHost *:8080
Listen 8080

You may also have to change your virtual host definition for your domain if it includes the port number.

Having set these up, cycle apache and varnish.

service httpd restart && service varnish restart

You should now be running through Varnish. You can test this by hitting the front-end of your site and taking a look at your headers. You should also be able to run varnishstat from a command line and see traffic.

Now that it’s set up, enable the module in Magento and it will start hitting Varnish.

Any questions on the above, feel free to drop me a comment below.