luni, 22 noiembrie 2010

Speeding up Magento with APC or Memcached

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.


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-apcNote 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 element that allows you to specify which caching method you are using in the element. This can have one of the following values:


We are dealing with APC here so we’ll just add:

... apc MAGE_ ...

This is the recommended entry from the Understanding Magento Scalability and Performance Magento blog post.


You will notice the 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

... apc SITEA_ ...

Site B

... apc SITEB_ ...

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 onapc.optimization = 0 # Experimental keep offapc.shm_segments = 1 # Shared memory segmentsapc.shm_size = 128 # Max shared memory dependent on OSapc.ttl = 7200apc.user_ttl = 7200apc.num_files_hint = 1024apc.mmap_file_mask = /tmp/apc.XXXXXXapc.enable_cli = 1 # Allow command line php to functionapc.cache_by_default = 1 # Enabled, 0 for filtersapc.max_file_size = 10M # Maximum cached file sizeapc.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 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 section to your app/etc/local.xml:

... memcached ...

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


This is by far not all you can do in terms of optimizing your Magneto 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:

Do you have links or comments about Magento performance tuning and optimization that you think are useful. Please share them in the comments.


View the original article here


If you're looking for simple and effective ways to influence you, your results and the world, you'll find powerful tools here from an official UK number 1 trainer and coach. Enjoy and be impeccable!


Check it out!

Un comentariu:

  1. Hello

    Do you have an example of a local.xml file where apc and memcached are both enabled.
    I have enabled apc, wich gives better performance, but i tried to also setup memcached in the local.xml file, but i dont see any increase in speed because of this, and i suppose there are some small tweaks that needs to be done, when you enable both?
    Thanks.

    RăspundețiȘtergere