Using Memcached

Memcached is a general-purpose memory cache server daemon. It can improve website performance by moving Drupal's standard caches out of the database and by caching the results of other expensive database operations. Acquia Cloud supports Memcached for all paid subscriptions. If you are an Acquia Cloud Professional customer, you can enable and manage Memcached for your websites. If you are an Acquia Cloud Enterprise customer, Acquia will configure your servers with an appropriate amount of memory for Memcached.

The most common use of Memcached with Drupal is storing Drupal's cache tables in Memcached instead of in the website database. This reduces the load on the database with every page load.

Using Memcached may not make any noticeable difference to a website that is already performing well at every layer in the server stack. Memcached is most helpful to websites that need to reduce the load on their database servers.

Enabling Memcached on Acquia Cloud

To enable Memcached, complete the following steps:

  1. Configure the server to allocate memory to Memcached.
  2. Download and install the Memcache API and Integration project.
  3. Add configuration code to your website's settings.php file.

Allocating memory to Memcached on Acquia Cloud Professional

By default, Acquia Cloud Professional servers have no memory allocated to Memcached and therefore Memcached isn't running.

To allocate memory to Memcached on Acquia Cloud Professional:

  1. Open the Servers page at Cloud > Servers.
  2. Click the arrow next to the Reboot button and select Configure.
  3. Under Memcached memory, enter the amount of memory to allocate to Memcached. A value of 64 MB is sufficient for Drupal sites running on Acquia Cloud.

Install the Memcache module

Download and install the Memcache API and Integration project on your Acquia Cloud website. You do not need to enable it in order for Memcached to work on your Acquia Cloud website. However, if you do enable it, you can receive notifications when you need to update the module, as well as view your website's Memcached statistics. Keep the module up to date with the latest stable release.

Adding Memcached configuration code to settings.php

Add the following configuration code to your settings.php file immediately after the Acquia database require statement, depending on your installed version of Drupal.

Configuration for Drupal 7

The first two of these settings cause Drupal to use Memcached instead of MySQL for data that's normally stored in database caches. The third setting makes sure that the cache_form table is assigned to non-volatile storage.

if (isset($conf['memcache_servers'])) {
  $conf['cache_backends'][] = './sites/all/modules/memcache/memcache.inc';
  $conf['cache_default_class'] = 'MemCacheDrupal';
  $conf['cache_class_cache_form'] = 'DrupalDatabaseCache';
}

Be sure the cache_backends path is the actual path to the Memcache module in your website's docroot.

Configuration for Drupal 6

if (file_exists('/var/www/site-php')) {
  require('/var/www/site-php/XXX/XXX-settings.inc');

// Memcache settings.
$conf['cache_inc'] = './sites/all/modules/contrib/memcache/memcache.inc';
$conf['memcache_bins'] = array(
     'cache' => 'default',
     'cache_form' => 'database',
);
}

The second setting makes sure that the cache_form table is assigned to non-volatile storage.

Optionally, you can cache user sessions in Memcached. Add the following line of code to your settings.php file immediately after the $conf['cache_inc'] statement. You can add this code whether or not you use memcache.inc or memcache.db.inc.

$conf['session_inc'] = './sites/all/modules/memcache/memcache-session.inc';

Be sure the cache_inc path and session_inc path is the actual path to the Memcache module in your website's docroot.

After you publish the settings.php file, Memcached is enabled.

Enabling stampede protection and moving locks into memory

The Memcache module can provide stampede protection for better performance. This helps minimize the performance hit that can otherwise occur if multiple requests simultaneously try to add the same item to the cache. Stampede protection uses Drupal's locking layer, so that only one process at a time can attempt to add an item to Memcache. However, MySQL's InnoDB storage engine is not well-suited to managing locks in the semaphore table under high loads, and therefore it is very important that if you enable stampede protection, you also move lock management out of the database and into memory. If you enable stampede protection without also moving locks into Memcache, you can experience severe performance degradation. You can implement both stampede protection and Memcache lock management in your site's Drupal settings.php file like this:

# Add in stampede protection
$conf['memcache_stampede_protection'] = TRUE;
# Move semaphore out of the database and into memory for performance purposes
$conf['lock_inc'] = './sites/all/modules/memcache/memcache-lock.inc';

For more information, see Memcache In-Depth in O'Reilly's High Performance Drupal and this discussion on drupal.org.

Tuning your Memcached settings

The Memcache Admin Drupal module, which is included in the Memcache API and Integration project, provides statistics about how Memcached is behaving on your website. By analyzing these statistics, you can decide whether you may need to allocate more memory to Memcached on your server.

To use the Memcache Admin module:

  1. Enable the Memcache Admin module within the Memcache API and Integration project. There is no need to enable the Memcache module. However, if you do enable it, you can receive notifications when you need to update the module.
  2. View the Memcache statistics on your site's Administration > Reports > Memcache statistics page.

Check the Memcache statistics page to see how the cache is behaving. This page reports the following:

  • Amount of memory the Memcached daemon is using
  • Percentage of memcached memory used compared to the total memory allocated to it
  • Number of evictions since the last time the service was started or stats reset

    Compare the ratio of evictions to total memcache activity. If evictions are a high proportion of total memcache activity, consider whether you should allocate more memory to Memcached. Bear in mind, however, that if you allocate more memory to Memcached on your server, you'll have less memory for other server functions, including PHP. Proper memory allocation depends on your site configuration.

For more information, see Memcache monitoring and flush using the nc command.

Related topics