We often hear complaints about how Magento is slow and performs poorly. Developers know, however, that performance is relative and that we can do a number of things to speed up a Magento site. This article will focus on configuring and using APC and/or Memcached. We have a resources section at the end of this article with links to more performance optimization techniques.

The options obviously also depend on the specific hosting arrangement. I will assume for the purposes of this article, that you have control over your hosting server and are able to install the necessary add-ons and make configuration changes to Apache and PHP. If you are on a shared environment, you may have to check with your hosting support if you can apply the tips from this article. But, you know that you should at least be on a VPS if you are running Magento.

APC – Alternative PHP Cache

Using APC is really easy. You can just install the PHP module and restart your Apache server and your sites should immediately benefit from this.

To install APC on a Debian based Linux distro, run:

sudo apt-get install php5-apc

Note that you will need either root or sudo privileges to perform the install

Then just restart your Apache server. You should start seeing improvements after browsing to a few pages on your site.

This is great already, however, there is another step you can do to integrate APC with your Magento site.

If you look at: app/etc/local.xml.additional, you will notice that there is a <cache> element that allows you to specify which caching method you are using in the <backend> element. This can have one of the following values:

You will notice the <prefix> element containing MAGE_. This is simply a prefix to tell APC what to use when it creates the cache. This is particularly important if you are running several Magento sites on the same web server. In this case, you will need to enter a unique prefix for each site. For example:Site A

Finally, to put the polish on your speed improvements, you can also tweak the APC configuration to further optimize your cache. This is done by editing the apc.ini file usually located (on Debian based distros) in: /etc/php5/conf.d/apc.ini.

Make sure you remove all the ; comments from your real file as stated in the corresponding forum post.

Of note is the apc.shm_size parameter. By default it is only 32M so the recommendation is to increase it to 128M. However, be careful if you are on a VPS with limited RAM, often you only have 384M or 512M so you may need to reduce your value to 64M. Especially if you want to combine this optimization technique with optimizing the MySQL parameters which also usually require increasing values for memory usage.

Memcached

Memcached is a free & open source, high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.

Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering.

Magento supports Memcached out of the box but by default, it is not enabled since we can’t assume that your server will have all the prerequisites installed.

To enable Memcached you will need to check if the daemon is running. By default the daemon is configured to listen to port 11211 so you can perform a netstat command and see if there is activity on 11211. If it’s running, you need to check if Memcached PHP support is enabled by looking at the output of phpinfo().

Once again, if you need to install the necessary components, on a Debian type distro the commands are something like:

sudo apt-get install memcached php5-memcache

You’ll need to enable Memcached via its configuration file (there will be a notice about this when you run the above command). Then start the service and restart Apache.

After this, you can add the following <cache> section to your app/etc/local.xml:

Conclusion

This is by far not all you can do in terms of optimizing your Magento site. We have only outlined a couple of popular caching mechanisms that are natively supported by Magento. Some people report better performance when using eAccelerator or XCache in place of APC and you can also try combining the various cache engines. However, be careful if you are also using Zend Server or Zend Optimizer as some of the caching engines may clash with those.

Here are some resources we’ve come across on the net that talk about Magento performance:

Author - Robert Popovic

I launched Magebase in April, 2010 and am its editor and contributor. My main topics of interest are Magento development, customization and how to get the most out of Magento with the least amount of headache.

Since it caches entire pages on the frontend (or whatever other pages you like), traffic can potentially completely avoid using magento all together when loading cached pages (which can dramatically increase page. Which is the pinnacle of caching for magento–only loading what parts of magento that you have to–that is.

Thanks for your contribution Chris, and good to know you offer a Magento module for full page caching for Magento CE as it is already available in Magento Entrerprise. Would you be able to comment on how your extension compares with the Enterprise full page caching?

I am seeing some “interesting” behaviour when multiple shops share the same memcache.What is the best way for several magento shops to share the same memcache, multiple instances of memcache on different ports? Is their a similar “prefix” option as you have mentioned with APC?

You don’t need to transfer the APC. APC is a PHP module that needs to be either installed as a module available on most of the popular Linux distributions by using the standard software installers such as apt-get or yum or compiled into your PHP if you are compiling PHP from source. Contact your hosting provider for more details.

Nice article! but i just wondering how to define different APC prefix for different store/site?
We are using the multistores functions of Magento so all our stores are currently sharing one single same app/etc/local.xml.

are you experiencing issues with your multi-website configuration when using the APC cache? The prefix is more relevant to multiple Magento installs rather than one single install. Not sure if you need to worry about this in your case.

Oh..That means the configuration is for multiple instances of Magento installed on same server? I was talking referring to multi-stores feature which single Magento instance support multiple domains. In that case please ignore me 🙂 Thank you Robert.

[…] Magento makes it easy to configure all this by the way – have a look at the file app/etc/local.xml.additional for further information. I will not go deeper into the setup here because we have already written about this. […]

hello. i have two stores on one installation. And i have this issue: stores are slow. But when i delete var/cache – stores speed up 200%. After few hours slow down again… and after deleting cache – speed up….. any ideas ?

Memcached is a great tool, we’ve used it in multi web head deployments where we wanted to share the cache between a handful of machines. But wouldn’t recommend it if your on a single server. Since memcached has to hit the network stack for cache access, the APC object cache is going to be faster and a bit easier for most installations.

We also recently release Brim’s Full Page Cache for Magento. It’s really easy to install and supports block updates for recently viewed products, compare products, shopping cart, etc. right out of the box.

Hi, thanks for the good tutorial. I have a question about multistores. I have a vps with APC and it’s working fine with one magento install and 1 store. Now I have addes an extra store with a different URL. Now the second URL is very slow and I think it’s because APC Is not doing anything with it. Any idea how I can fix this?

APC should be working regardless as it is enabled on the PHP configuration level and if you have a single install Magento with multi-stores then you should not have to do anything special. Your performance issue could be due to some other factors? You can check this article about how to display and monitor your server’s APC stats.

I have been playing with this for a while with no luck it seems and our magento is running fairly slow even thought it is on quad core server with NGINX and php-fpm and as optimized as i can get it (i run several other web servers hosting Kayako systems and other company websites that all fly!)

I put in my local.xml various things

apc
MAGE_

or

apc
store_

store_* is the prefix for my store names. i presume it does not have to match what i have in my index files?

1. The storage is always empty no matter how we are using it – for opcode, or just for cache.

There is one common issue which can cause this situation. Most likely PHP on the server works in the Fast-CGI mode. In this mode parallel PHP processes don’t have access to each other’s data stored at APC. This is an old APC/PHP bug and there is no good solution for this problem: either change PHP mode or use other cache engine.

[…] Speeding up Magento with APC or Memcached – … – There are quite a few discussions on the Magento forums and various blogs about Magento performance tuning and optimization. A few simple Google searches will …… […]