Last updated October 28, 2016. Created on April 19, 2011.
Edited by ressa, Francewhoa, Garrett Albright, SebCorbin. Log in to edit this page.

Installation

These are the broad steps you need to take in order to use this software on Drupal 6.x.Information pertaining to the Drupal 7.x version is also found in the README.txt file from the module distribution.

For 6.x (order is important):

  1. Install the memcached binaries on your server. See
  2. Install the PECL memcache extension for PHP.
  3. In php.ini set memcache.hash_strategy="consistent".
  4. Put your site into offline mode.
  5. Download and install the memcache module.
  6. If you have previously been running the memcache module, run update.php.
  7. Start at least one instance of memcached on your server.
  8. Edit settings.php to configure the servers, clusters and bins that memcache is supposed to use. (see code snippet below)
  9. Edit settings.php to include memcache.inc. For example, $conf['cache_inc'] ='sites/all/modules/memcache/memcache.inc';
  10. Bring your site back online.

For 7.x (order is important):

  1. Install the memcached binaries on your server. See
  2. Install the PECL memcache extension for PHP. This must be version 2.2.1 or higher or you will experience errors.
  3. Put your site into offline mode.
  4. Download and install the memcache module.
  5. If you have previously been running the memcache module, run update.php.
  6. Start at least one instance of memcached on your server.
  7. Edit settings.php to make memcache the default cache class, for example:
      $conf['cache_backends'][] = 'sites/all/modules/memcache/memcache.inc';
      // The 'cache_form' bin must be assigned no non-volatile storage.
      $conf['cache_class_cache_form'] = 'DrupalDatabaseCache';
      $conf['cache_default_class'] = 'MemCacheDrupal';
      $conf['memcache_key_prefix'] = 'something_unique';
    
    
  8. Bring your site back online.

For instructions on 1 and 2 above, please see the INSTALLATION.txt file that comes with the memcache module download.

Servers

NOTE: As of 6.15, it is possible to efficiently run memcache in one daemon, shared by all bins. You can safely ignore advice found elsewhere on the internet that advises one memcached daemon per bin.

In terms of reporting, there are still some advantages to having more daemons. If you want the simple version, you can start one default memcache instance on your web server like this:
memcached -m 24 -p 11211 -d
If that is enough to meet your needs, there is no more configuration needed.

If you want to utilize this module's sophisticated clustering feature and spread your cache over several machines, or if your cache is found on a machine other than your web server, read on.

Security

You should probably lock down the memcache server so that it only listens for connections from the hosts that need to be served.

The default on some installations is that memcache listens to connections from all addresses. To close that hole, modify your memcached configuration as follows:

On RHEL/CentOS, edit /etc/sysconfig/memcached:

 OPTIONS="-l 127.0.0.1"

Format is OPTIONS="-l ${HOSTIP}"

On Debian/Ubuntu, edit /etc/memcached.conf:

 -l 127.0.0.1

Format is -l ${HOSTIP}

Configuration

The available memcached servers are specified in $conf in settings.php. If you do not specify any servers, memcache.inc assumes that you have a memcached instance running on the local machine on port 11211 (127.0.0.1:11211).

If this is true, and it is the only memcached instance you wish to use, no further configuration is required. If you have more than one memcached instance running, you need to add two arrays to $conf; memcache_servers and memcache_bins.

The arrays follow this pattern:

$conf['memcache_servers'] = array(
  host1:port => cluster, 
  host2:port => cluster, 
  hostN:port => cluster
);
$conf['memcache_bins'] = array(
  bin1 => cluster, 
  bin2 => cluster, 
  binN => cluster
);

WARNING: Avoid the use of "localhost" and instead use real IP addresses or hostnames that will remain consistent across the network.

The bin/cluster/server model can be described as follows:

  • Servers are memcached instances identified by host:port.
  • Bins are groups of data that get cached together and map 1:1 to the $table param in cache_set(). Examples from Drupal core are cache_filter, cache_menu. The default is 'cache'.
  • Clusters are groups of servers that act as a memory pool.
  • Many bins can be assigned to a cluster.
  • The default cluster is 'default'.

Here is a simple setup that has two memcached instances, both running on 10.1.1.1. The 11212 instance belongs to the 'pages' cluster and the table cache_page is mapped to the 'pages' cluster. Thus everything that gets cached, with the exception of the page cache (cache_page), will be put into 'default', or the 11211 instance. The page cache will be in 11212.

$conf['memcache_servers'] = array(
  '10.1.1.1:11211' => 'default',
  '10.1.1.1:11212' => 'pages'
);
$conf['memcache_bins'] = array(
  'cache_page' => 'pages',
);

Here is an example configuration that has two clusters, 'default' and 'cluster2'. Five memcached instances are divided up between the two clusters. 'cache_filter' and 'cache_menu' bins go to 'cluster2'. All other bins go to 'default'.
D6:

$conf['cache_inc'] = './sites/all/modules/memcache/memcache.inc';
$conf['memcache_servers'] = array(
  '10.1.1.1:11211' => 'default',
  '10.1.1.1:11212' => 'default',
  '10.1.1.2:11211' => 'default',
  '10.1.1.3:11211' => 'cluster2',
  '10.1.1.4:11211' => 'cluster2'
);
$conf['memcache_bins'] = array(
  'cache' => 'default',
  'cache_filter' => 'cluster2',
  'cache_menu' => 'cluster2'
);

D7:

$conf['cache_backends'][] = 'sites/all/modules/memcache/memcache.inc';
$conf['cache_default_class'] = 'MemCacheDrupal';
// The 'cache_form' bin must be assigned no non-volatile storage.
$conf['cache_class_cache_form'] = 'DrupalDatabaseCache';
$conf['memcache_servers'] = array(
  '10.1.1.1:11211' => 'default',
  '10.1.1.1:11212' => 'default',
  '10.1.1.2:11211' => 'default',
  '10.1.1.3:11211' => 'cluster2',
  '10.1.1.4:11211' => 'cluster2'
);
$conf['memcache_bins'] = array(
  'cache' => 'default',
  'cache_filter' => 'cluster2',
  'cache_menu' => 'cluster2'
);

Prefixing

If you want to have multiple Drupal installations share memcached instances, you need to include a unique prefix for each Drupal installation in the $confarray in settings.php:

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

Sessions

NOTE: Session.inc is not yet stable in Drupal 7 (see #656838: [META] Port sessions to D7 for current status).
You MUST have setup separate memcached instances for both session and users for memcached sessions to work.
Here is a sample config that uses memcache for sessions.

$conf['cache_default_class'] = 'MemCacheDrupal';
// The 'cache_form' bin must be assigned no non-volatile storage.
$conf['cache_class_cache_form'] = 'DrupalDatabaseCache';
$conf['session_inc'] =  './sites/all/modules/memcache/memcache-session.inc';
$conf['memcache_servers'] = array(
  'localhost:11211' => 'default',
  'localhost:11212' => 'session',
  'localhost:11213' => 'users',
);
$conf['memcache_bins'] = array(
  'cache' => 'default',
  'session' => 'session',
  'users' => 'users',
);

Troubleshooting

See the troubleshooting guide.

Memcache Admin

A module offering a UI for memcache is included. It provides aggregated and per-page statistics for memcache.

Memcached PECL Extension Support

We also support the Memcached PECL extension. This extension backends to libmemcached and allows you to use some of the newer advanced features in memcached 1.4.

NOTE: It is important to realize that the memcache php.ini options do not impact the memcached extension, this new extension doesn't read in options that way.
Instead, it takes options directly from Drupal. Because of this, you must
configure memcached in settings.php. Please see the PECL Memcached documentation for possible options.

An example configuration block is below, this block also illustrates the
default options (selected through performance testing). These options will be set unless overridden in settings.php.

$conf['memcache_options'] = array(
  Memcached::OPT_COMPRESSION => FALSE,
  Memcached::OPT_DISTRIBUTION => Memcached::DISTRIBUTION_CONSISTENT,
);

These are as follows:

  • Turn off compression, as this takes more CPU cycles than it's worth for most users
  • Turn on consistent distribution, which allows you to add/remove servers easily

Other options you could experiment with:

Memcached::OPT_BINARY_PROTOCOL => TRUE
This enables the Memcache binary protocol (only available in Memcached 1.4 and later). Note that some users have reported SLOWER performance with this feature enabled. It should only be enabled on extremely high traffic networks where memcache network traffic is a bottleneck.

Additional reading about the binary protocol:
http://code.google.com/p/memcached/wiki/MemcacheBinaryProtocol

Memcached::OPT_TCP_NODELAY => TRUE
This enables the no-delay feature for connecting sockets; it's been reported that this can speed up the Binary protocol (see above). This tells the TCP stack to send packets immediately and without waiting for a full payload, reducing per-packet network latency (disabling "Nagling").

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

Crell’s picture

Note that this page is specific to Drupal 6. The way you hook up memcache in Drupal 7 is a bit different, specifically the way you tie into the cache system. See the README.txt file in the module for the updated installation instructions until this page is updated. (Please delete this comment when this page is updated.)

skolesnyk’s picture

One important security hole is present in default memcached daemon configuration. That's it listen to connections from all IPs.

So, to close that hole, edit /etc/sysconfig/memcached with

OPTIONS="-l 127.0.0.1" or change IP to whatever suits your case.

wizonesolutions’s picture

A tip when trying to get this going on Ubuntu 11.04 is that sudo apt-get install libmemcached (needed for building the PECL extension) does not work, as you'd think it would. Use sudo apt-get install libmemcached-dev instead. This depends on other packages that satisfy the dependency.

FillPDF Service - http://fillpdf-service.com - Hosted solution for FillPDF

gerold’s picture

the above code for Prefixing doesn't work on my D7 setup. it doesn't gave an error message but there's no data passed to memcached. here's what worked for my D7 setup:

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

kim.pepper’s picture

Note you MUST have a session and a users server set up for memcached sessions to work.

Do you mean you need to just need to have a session and user table in Drupal for this to work? Or do you you mean you need to have a separate memcache instance just for sessions and users?

Kim

Kim Pepper
~~~~~~~
http://www.previousnext.com.au

ann_pennington’s picture

After doing a clean install of MAMP/MAMP Pro 2.x with PHP 5.3.20, I followed commonly distributed instructions for installing memcached and then the PHP extension memcache. So in case someone else has a similar configuration, here's what I discovered:

$ cd /tmp
$ wget http://pecl.php.net/get/memcache-2.2.7.tgz
$ tar -zxvf memcache-2.2.7.tgz
$ cd memcached-2.2.7
$ /Applications/MAMP/bin/php/5.3.20/bin/phpize
$ MACOSX_DEPLOYMENT_TARGET=10.8 CFLAGS='-O3 -fno-common -arch i386 -arch x86_64' LDFLAGS='-O3 -arch i386 -arch x86_64' CXXFLAGS='-O3 -fno-common -arch i386 -arch x86_64' ./configure
$make
$ cp modules/memcache.so /Applications/MAMP/bin/php/php5.3.20/lib/php/extensions/no-debug-non-zts-20090626/

Success! So if you encounter the challenge with lazy symbol binding when trying to install memcache extension from pecl, hope these instructions help.

joshuautley’s picture

http://zurmo.org/wiki/installing-memcache-on-windows

Note the first option is for win32. If you have win64 (as I do) ref. the fourth bullet point of the install instructions (http://s3.amazonaws.com/downloads.northscale.com/memcached-win64-1.4.4-1...)

Note that the .dll with the win64 version is for PHP 5.3. If you are running PHP 5.4 (as I am) you will need a different .dll (http://windows.php.net/downloads/pecl/releases/memcache/3.0.6/php_memcac...)

It took me 4.5 hours to do this all because I didn't have the correct .dll file. With the correct and current info it should only take 15 minutes if that.

- Cheers

GaëlG’s picture

It looks like this simple commands are enough now:

sudo apt-get install memcached php5-memcached
sudo service apache2 restart
ressa’s picture

In the Drupal 8 README file, php5-memcache is recommended (no "d" at the end), so the commands would be:

sudo apt-get install memcached php5-memcache
sudo service apache2 restart

Try Drupal 8 at simplytest.me
ishwar’s picture

I want to configure memcache for REDHAT server.
And our server is in VPN environment and for security reason I unable to download the packages through just entering the command.

Also This How to install Memcache Fedora / CentOS / RHEL link is not working.

I need all packages first download from server(repo) also I need all repo links along with process so that I can install as well.

Is there any solutions for the same.

Thanks in advance
Ishwar

coffeduong’s picture

Hi Ishwar,
You can download Memcached source from here https://memcached.org/files/memcached-1.4.31.tar.gz
If it doesn't work, I hope this tutorial will help you http://www.ducea.com/2008/01/10/howto-install-memcached-from-sources-on-...
Have a nice day :)