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:
- apc
- memcached
- xcache
We are dealing with APC here so we’ll just add:
<global>
...
<cache>
<backend>apc</backend>
<prefix>MAGE_</prefix>
</cache>
...
</global>
This is the recommended entry from the Understanding Magento Scalability and Performance Magento blog post.
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
<global>
...
<cache>
<backend>apc</backend>
<prefix>SITEA_</prefix>
</cache>
...
</global>
Site B
<global>
...
<cache>
<backend>apc</backend>
<prefix>SITEB_</prefix>
</cache>
...
</global>
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.
To see what the default settings are and for an explanation of the configuration parameters, check the official PHP site’s APC chapter.
We will recommend this APC configuration that was posted in the official Magento forums.
extension = apc.so #name dependent on your APC cache install [APC] apc.enabled = 1 # Turn APC cache on apc.optimization = 0 # Experimental keep off apc.shm_segments = 1 # Shared memory segments apc.shm_size = 128 # Max shared memory dependent on OS apc.ttl = 7200 apc.user_ttl = 7200 apc.num_files_hint = 1024 apc.mmap_file_mask = /tmp/apc.XXXXXX apc.enable_cli = 1 # Allow command line php to function apc.cache_by_default = 1 # Enabled, 0 for filters apc.max_file_size = 10M # Maximum cached file size apc.stat = 1 # 1 for dev, 0 for production, whether the source file is checked for mod date #apc.include_once_override = 1 # Use PHP5.3+ for include_once optimization
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:
<global>
...
<cache>
<backend>memcached</backend>
<memcached>
<servers>
<server>
<host><![CDATA[127.0.0.1]]></host>
<port><![CDATA[11211]]></port>
<persistent><![CDATA[1]]></persistent>
</server>
</servers>
<compression><![CDATA[0]]></compression>
<cache_dir><![CDATA[]]></cache_dir>
<hashed_directory_level><![CDATA[]]></hashed_directory_level>
<hashed_directory_umask><![CDATA[]]></hashed_directory_umask>
<file_name_prefix><![CDATA[]]></file_name_prefix>
</memcached>
</cache>
...
</global>
These settings are for a single local server running Memcached on the default 11211 port.
To see your Memcached in action you can issue a telnet localhost 11211 command and type: stats
Information source credits for the Memcached section go to Nexcess and their blog post on how to Enable Memcached in Magento
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:
- Magento Blog: Several Articles on Magento Performance
- Magento Forum: A Guide to Making Magento Faster
- Magento Forum: APC Setup
- Magento Groups: Magento Performance and Optimization
- Magento Group Wiki: Optimising Your Web Stack Performance for Magento
- Slides (from Magento Developers Paradise): Methods and Best Practices for High Performance eCommerce
- Guido Jansens Blog:
101 ways to speed up your Magento e-commerce website - Yoast Blog: Magento performance hosting
- Nexcess Blog: Enable Memcached in Magento
- Inchoo Blog: Boost the speed of your Magento
- Inchoo Blog: Magento performance, research and improvement
- Oggetto Web Blog: Magento performance: Enterprise vs Community
- Tinybrick: (Magento) Speed Testing
Do you have links or comments about Magento performance tuning and optimization that you think are useful. Please share them in the comments.
Originally published on magebase.com. Copyright © 2010 Magebase - All Rights Reserved.




Proud members of the
Nice article, for people who have a socket to connect to use:
@Gijs
I just corrected your code… see below the comment box how to insert code in the comments. Thanks for your contribution.
Sorry xml doesn’t show, just put the socket url with unix:/// in the tag host and leave the port set to zero
Some great suggestions for using caching. We’ve successfully used memcache before in a federated configuration and it’s pretty powerful.
If I could, something we built to go hand-in-hand with memcache is a whole-page caching tool called Lightspeed (http://www.tinybrick.com/magento-modules/performance/improve-magentos-slow-performance.html/).
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.
Happy caching!
Also, here’s a nicer (slightly less technical) Speed Test article that should help most users gauge how efficient their front-end and back-end are:
http://guides.tinybrick.com/magento/speed-testing
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?
Sure. Well other than the price ($500 once vs $13k/yearly), Lightspeed offers the similar performance as the EE whole-page caching module, but Lightspeed also offers:
- advanced tagging which allows for more finite control over caches (http://guides.tinybrick.com/magento/lightspeed#tagging)
- hole-punching for displaying selected dynamic content “punches” after a cached page loads (http://guides.tinybrick.com/magento/lightspeed#hole-punching)
While I don’t have hard numbers on testing other than the benchmarks that are on the product page (http://www.tinybrick.com/improve-magentos-slow-performance.html#benchmarks), we’ve had several enterprise customers that have tried both and prefer our product.
Thanks Chris, this is the sort of info I was looking for. Very good to know. Thanks again for your contribution.
Instead of yslow try page speed extension from google:
http://code.google.com/intl/nl/speed/page-speed/docs/extension.html
We will try the apache mod if we have time:
http://code.google.com/intl/nl/speed/page-speed/docs/rules_intro.html
If you are using disk with questionable speed (over commited cloud or shared hosting) and want to be certain it will never use files you can split the 2 cache parts between APC and memcached.
Apc
Memcached
Apc
Just posted the more complete config in a blog post.
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?
Thanks
Pete.
@pete try experimenting with the file_name_prefix element in the memcached configuration xml in local.xml.
Great performance tips!
Useful tips, thank you for sharing. I run magento at web hosting, it is so slow, then I upgrad it to VPS, then it is speeding up. I will also try this method. Thanks again.
Sorry for a silly question but where should I transfer the apc?I mean on what folder?I’m sorry,I’m totally newbie
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.
Hi Robert,
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.
Thanks.
@Sam Wong
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.
unfortunatelly not work with 1.4.1 ;(
@Karol Of course it works with 1.4.1.
Robert, when i try your code, my store crash, and reports goes like this:
a:4:{i:0;s:28:”Can’t get filling percentage”;i:1;s:2132:”#0
/home/admin/domains/domain.com/public_html/lib/Zend/Cache/Backend/Memcached.php(403):
Zend_Cache::throwException(‘Can’t get filli…’)
#1
/home/admin/domains/domain.com/public_html/lib/Zend/Cache/Backend/TwoLevels.php(488):
Zend_Cache_Backend_Memcached->getFillingPercentage()
#2
/home/admin/domains/domain.com/public_html/lib/Zend/Cache/Backend/TwoLevels.php(173):
Zend_Cache_Backend_TwoLevels->_getFastFillingPercentage(‘saving’)
#3
/home/admin/domains/domain.com/public_html/lib/Zend/Cache/Core.php(381):
Zend_Cache_Backend_TwoLevels->save(‘a:7:{s:10:”bloc…’,
‘fa7_CORE_CACHE_…’, Array, NULL)
#4
/home/admin/domains/domain.com/public_html/lib/Varien/Cache/Core.php(76):
Zend_Cache_Core->save(‘a:7:{s:10:”bloc…’, ‘CORE_CACHE_OPTI…’, Array,
NULL, 8)
#5
/home/admin/domains/domain.com/public_html/app/code/core/Mage/Core/Model/Cache.php(349):
Varien_Cache_Core->save(‘a:7:{s:10:”bloc…’, ‘CORE_CACHE_OPTI…’, Array,
NULL)
etc
etc
Ok, now we have some more detail…
Seems like M1.4 has an issue with Memcached due to a problem in the Zend Framework. Check if this article will help: http://www.magentocommerce.com/boards/viewthread/230185/
PS> Google is your friend
i found your blog by google, isnt?
i try your memcache settings. for now i works. i have multistore, mayby APC will be better ?
[...] 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 ?
@Karol You should read this article: http://magebase.com/magento-tutorials/improving-the-file-cache-backend/ – it will explain why you may be experiencing the slow-down and provides a solution.
@Robert, I unfortunatelly have 1.4.1 ;( ehhhh
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.
It’s great tips to enhance speed of Magento. Thanks for such nice post.
we have just setup apc on our server and thanks to this post it works wonders
many thanks
If you really want speed on the cheap!
Try memcached, with tmpfs and the BRIM fullpage caching module.
A very brief and descriptive comparison between XCache, Varnish, Boost, APC, and Memcached is shown at Brief Summary: XCache, Varnish, Boost, APC, and Memcached
Did you mean sudo apt-get install php-apc instead of sudo apt-get install php5-apc? I get an “E: Unable to locate package php5-apc” when I run that command as it is written in this article.
Hi! Thanks for this detailed tutorial! I’m new to Magento and feel lost with all of the codes and stuf..
I’d like an extension to do the optimissation for me…
here’s a related article http://www.shopping-cart-diagnostics.com/blog/magento-store-performance-what-you-should-know/ do you think it’s worth giving a try?
Hi, great article. But now comes the biggie.
- How do I combine both APC and memcached? (we have both, something with slow/fast)
And some small questions. Maybe we can exchange emails Robert?
- Suppose I install Nitrogento or another FPC: will it save the cache on disk, or in the memcache?
- Where is the /var/cache and /var/session stored in these examples? In the memcache, or still on disk.
@snh
something like:
Apc
Memcached
Apc
grr (must read instructions)
Thanks for that. Will try.
Must say that I was advised recently to only activate APC (and not both).
We installed Brim FPC that we are quite happy with. Although nothing made our site fly as the freeware code of Ezapps Zoom (but, het no support!).
The problem now lies in our backend. Updating and setting up a new store is so cumbersome because of all the loading times.
Very detail learning, thank you
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.