Enabling Memcached in Magento

Enabling Memcached in Magento

Mar 22

  • Created: Mar 22, 2010 2:49 PM

To help increase the speed of Magento page loads as much as possible, it is recommended to use the Memcached support built within Magento. Memcached is a high-performance memory object caching system that is designed to speed page loads on dynamic content websites. Magento supports Memcached for caching many objects, but it is not enabled by default and will require some simple changes to your configured local.xml file to use.

First, be sure that Memcached is installed and running on your server. It by default listens on port 11211. A ‘netstat’ will verify that you do indeed have a Memcached listening socket on port 11211 indicating that is it running. Configuring Memcached to listen on localhost is recommended as it would be a security risk allowing any external sources access Memcached directly if it were to be listening on an external ip that wasn’t firewalled.

You also need to be sure that Memcached support is loaded into PHP. A view on a phpinfo() page can verify this, just look for the ‘Memcache’ block in the phpinfo and be sure it is ‘enabled’.

To enable Memcached within Magento, you will need to add the following block of code to your app/etc/local.xml file:

<cache>
  <backend>memcached</backend><!-- apc / memcached / empty=file -->
  <memcached><!-- memcached cache backend related config -->
    <servers><!-- any number of server nodes can be included -->
      <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>

This code will precede right before thetag. An example of the code placement can be found in app/etc/local.xml.additional. You will need to make sure that the above variables ‘host’ and ‘port’ are set to the correct values. Localhost (127.0.0.1) and port 11211 should be correct for most single dedicated server Magento installs. Once the code is added, save the local.xml file and check your site. No process restarts are needed, as soon as the code is saved it goes into effect allowing Magento items to be cached in Memcached. If after saving your site fails to load, go back and check your local.xml configuration and servers Memcached config.

There is a quick way to see the Memcached statistics, you can telnet to port 11211 on the Memcached server and execute a ‘stats’ command:

$ telnet localhost 11211
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
stats</p>
<p>STAT pid 2783
STAT uptime 1597393
STAT time 1269284713
STAT version 1.2.8
STAT pointer_size 64
STAT rusage_user 486.774998
STAT rusage_system 2147.863475
STAT curr_items 1177
STAT total_items 167313
STAT bytes 2022179
STAT curr_connections 5
STAT total_connections 1038840
STAT connection_structures 88
STAT cmd_flush 0
STAT cmd_get 61209568
STAT cmd_set 167313
STAT get_hits 60060311
STAT get_misses 1149257
STAT evictions 0
STAT bytes_read 3389616170
STAT bytes_written 122304480231
STAT limit_maxbytes 1073741824
STAT threads 5
STAT accepting_conns 1
STAT listen_disabled_num 0
END

In this case we could telnet to localhost on port 11211. Executing a ‘stats’ will show Memcached statistics such as hit rate and maximum connections. Looking at these variables can help you tune your Memcached servers configuration.

Posted in: Magento / Tagged: ,
  • Pingback: Install and configure memcached for Magento on RedHat 5.5 | Anthony Taliercio - Managed Hosting Atlanta

  • http://profile.yahoo.com/4KTX4HIA46QELZFGQQ2ISMT37Q Terry

    Great article Brad. But what if I wanted to use memcache and APC in my Magento install. APC is opcode cache (for compiled PHP code). Memcache is not opcode cache but for database calls and session handling. So how would I add both memcache and APC to the local.xml file to ensure that both are working as they should? Also, according to a litespeed web server engineer, APC and memcache will not cache your product info page, but the Litespeed Web Server Cache will. I just installed Litespeed web server and wouldn’t it be smart to enable all three caches on my store? My store currently has 10,000 products, and in a couple weeks it will grow to 150,000 products, so my store will definitely need all the help that it can get. What are your suggestions on how I should edit my local.xml file to include the various caches to ensure that all three are working as they should?

  • http://twitter.com/shirtsofholland ShirtsofHolland.com

    Same question

  • http://twitter.com/Mouwlengte7 Mouwlengte7

    Does aynone know the answer?

  • Tony

    If you get the error client denied by server configuration: ../local.xml remove the var/cache directory. :)

  • Pingback: infirock

  • http://www.facebook.com/vi.wickam Vi Wickam

    Do you have any specific recommendations for memcached settings that work well with magento?

  • Josh Ribakoff

    The XML is double escaped I can’t read it.

  • will_hough

    Fixed the code, not sure how that happened. Thank’s for letting us know Josh.

  • Josh Ribakoff

    Thanks, also we’re using Magento Enterprise and it didn’t work without also adding Memcached

    My observations after enabling memcache was that its 1/10th of a second slower on my localhost. Enabling APC sped things up whereas memcache slowed them down. I’d suggest experimenting with different types & combinations of cache systems.

  • Michal Skrzypecki

    For APC you don’t have to change or add anything to magento.

  • txyoji

    apc does a number of things, one of them is opcode caching and the other is it exposes an api for doing key-value pair storage in memory, similar to memcached.

    If you have apc installed or are using zend_server then you don’t need memcache. Its already built into your php environment.

  • http://www.quacos.com Giovanni

    This saved my day.

  • http://www.quacos.com Giovanni

    I suffered segmentation faults using Magento on PHP 5.4 (FPM) and APC.

    Now I let APC doing just opcode caching and installed memcached for Magento stuff.