Nexcess
Nexcess Blog Logo
March 22, 2010
By Brad

Enabling Memcached in Magento

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:

[xml]

memcached















[/xml]

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:

[bash]
$ telnet localhost 11211
Trying 127.0.0.1…
Connected to localhost.
Escape character is ‘^]’.
stats


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
[/bash]

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.

Avatar for Brad
Brad
Brad is Director of System Operations at Nexcess. With over a decade in systems administration, he oversees out internal systems and was the author of The Definitive Guide to Magento 2 Optimization. Few systems at Nexcess escape his insight. When not monitoring our hosting infrastructure or upholding PCI security standards, Brad celebrates life with his family and plays a mean game of Puerto Rico.