Using Memcached

Memcached is a general-purpose memory cache server daemon. It can improve Drupal application performance by moving Drupal's standard caches out of the database and by caching the results of other expensive database operations.

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

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

Memcached availability

Acquia Cloud supports Memcached for subscriptions and products based on the following table:

Product Description
Acquia Cloud Professional You can enable and manage Memcached for your applications.
Acquia Cloud Enterprise Acquia will configure your servers with an appropriate amount of memory for Memcached. As an option, can purchase a dedicated Memcached server.
Acquia Cloud Site Factory To use Memcached with your websites, contact your Technical Account Manager for assistance.
Acquia Cloud Free Memcached is not supported. Learn more about Acquia Cloud Free and how to upgrade your subscription.
Acquia Cloud CD Acquia Cloud CD environments that meet certain requirements can use Memcached.

Enabling Memcached on Acquia Cloud

To enable Memcached, complete the following steps:

  1. To allocate memory to Memcached on Acquia Cloud Professional and enable it to run, use the following procedure:
    1. Sign in to the Acquia Cloud interface and select your application and environment.
    2. In the left menu, click Servers.
    3. In the card for the web server, click Configure.

      Configure Memcached memory allocation for a server

    4. Under Memcached memory (MB), enter the amount of memory to allocate to Memcached. A value of 64 MB is sufficient for Drupal applications running on Acquia Cloud.
    5. Click Save.
  2. Ensure that your server is using PHP 5.6. Memcache does not support PHP 7.
  3. Download, install, and enable the Memcache API and Integration module on your Acquia Cloud application. When enabled, you can receive notifications when you need to update the module, as well as view your application's Memcached statistics. Keep the module up to date with the latest stable release.
  4. 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 8

      if (file_exists('/var/www/site-php')) {
        // Memcache settings.
        $settings['cache']['default'] = 'cache.backend.memcache';
      }
    • 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 application's docroot.

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

Acquia Cloud CD prerequisites for using Memcached

Acquia Cloud CD customers can use Memcached, which is implemented on a per-website basis and is secured using SASL credentials. If you need Memcached support for your CD environment, the environment must meet the following prerequisites:

  • The environment uses the PECL memcached extension.
  • The environment must include the following modules, based on the Drupal version in use:

Settings for the modules should be updated automatically.

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.

To implement both stampede protection and Memcache lock management in your application's Drupal settings.php file, add the following code:

# 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 application. 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 in 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 Drupal application'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 application configuration.

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

By default, with stampede protection enabled, when a thread encounters a locked cache entry, it waits five seconds, and then tries to access the cache entry again. The Drupal Memcache module includes a memcache_stampede_wait_time variable that you can use to modify the time a thread waits before trying again, and a memcache_stampede_wait_limit variable to control how many tries are made by a thread. You can also use configuration variables to disable stampede protection for specific cache bins or patterns of cache IDs. For more information, see the ignore settings in the module. If your application is experiencing many threads in a lock wait state, you may be able to improve performance by adjusting these settings.

Drupal Projects

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.
By submitting this form, you accept the Mollom privacy policy.

Contact supportStill need assistance? Contact Acquia Support