Contact
Site: US UK AU |
Nexcess Blog

Enabling Memcached in Magento

March 22, 2010 16 Comments RSS Feed

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

16 comments for “Enabling Memcached in Magento

  1. February 17, 2012 at 12:11 am

    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?

  2. September 10, 2012 at 5:23 am

    Same question

  3. October 15, 2012 at 3:54 pm

    Does aynone know the answer?

  4. Tony
    December 21, 2012 at 7:42 pm

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

  5. Pingback: infirock
  6. March 21, 2013 at 3:23 pm

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

  7. Josh Ribakoff
    July 5, 2013 at 8:49 am

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

  8. will_hough
    July 5, 2013 at 9:24 am

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

  9. Josh Ribakoff
    July 5, 2013 at 9:27 am

    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.

  10. Michal Skrzypecki
    July 20, 2013 at 5:57 pm

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

  11. txyoji
    November 14, 2013 at 4:07 pm

    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.

  12. April 16, 2014 at 5:55 am

    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.

  13. April 16, 2014 at 5:55 am

    This saved my day.

  14. Pingback: Gregory Smith
  15. Jigar Savla
    October 31, 2017 at 4:28 pm

    Thanks for sharing this.
    We configured this, but now facing issue with admin global notification message, how do we overcome that ?

Comments are closed.