How to Install Memcached For Drupal 7

In this article we’re going to take our Drupal performance a step further by installing and configuring Memcached. This is another great open source caching system which can dramatically increase your site’s performance by alleviating database load. By default Drupal saves cached information into database tables, which is great, but still requires Apache to hit the database to retrieve it. Memcached speeds things up by saving this information in RAM memory, which is significantly faster to access.

Run the following command lines to install the Memcached packages. You may already have some of these installed, but it doesn’t hurt to run them again.

$ sudo apt-get install memcached libmemcached-tools
$ sudo apt-get install php5-dev php-pear make
$ sudo pecl install memcache

Memcached needs its own memcache.conf file in our PHP configuration in order to run:

$ sudo nano /etc/php5/conf.d/memcache.ini

Made sure the following lines are present in this file.

The more memory we can give to Memcached the better. The default value is 16MB, but if feasible, try allocating as much as 1/4 of your total available RAM. To make these changes open up memcached.conf:

$ sudo nano /etc/memcached.conf
# Change this default value to ~ 1/4 of your total
# available RAM
-m 16

Restart Memcached and Apache for these change to go into effect:

$ sudo /etc/init.d/memcached restart
$ sudo /etc/init.d/apache2 restart

Now check to see if Memcached is running:

$ sudo netstat -tap | grep memcached

You should see something like:

tcp 0 0 localhost:11211 *:* LISTEN 25266/memcached

Installing Memcached for Drupal 7

That’s the server installation out of the way. Now let’s install and configure the Memcached Drupal module which integrates Drupal’s caching system with Memcached. Start by putting your site in offline mode and download and enable the Memcached module. Then add the following to settings.php:

$conf['cache_backends'][] = 'sites/all/modules/memcache/';
$conf['cache_default_class'] = 'MemCacheDrupal';
$conf['cache_class_cache_form'] = 'DrupalDatabaseCache';

If you’re using a multi-site installation it’s very important to add this to each settings.php file, changing the unique_key for each installation:

$conf['memcache_key_prefix'] = 'unique_key';

Put your site back in online-mode and you’re done! The Memcached module also ships with an Admin submodule which you can enable to see how Memcached is performing. Once you’re happy with your settings I would go back and disable this module as it is unnecessary for day to day activities.

UPDATE (4/10/2013): I’ve updated and tweaked this article with additional information. Everything should still be pretty relavent.

UPDATE (5/24/2013): I recently came across this handy little script that will give you improved UI statistics of memcached and how it’s performing. Just throw this file in your Drupal root directory, make sure to update the username and password, and voilà! All done.

As a point of reference, a safe goal should be to consume ~90% of the memory allocated to memcache. If you ever hit 100% the system will constantly be adding and dumping queries from the cache, hurting your overall performance.