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 (TAM) or Account Manager to evaluate your hardware needs before implementing a post-settings.php hook.
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 then the 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 64MB is sufficient for Drupal applications running on Acquia Cloud.
    5. Click Save.
  2. You can use PHP 5.6 or PHP 7.
    To use memcached 2.2.0 with PHP 5.6, add the following code to your settings.php file:

    # Drupal 7
    $conf['memcache_extension'] = 'Memcached';
    # Drupal 8
    $settings['memcache']['extension'] = 'Memcached';
  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 (isset($settings['memcache']['servers'])) {
        // Memcache settings
        $settings['cache']['bins']['bootstrap'] = 'cache.backend.memcache';
        $settings['cache']['bins']['discovery'] = 'cache.backend.memcache';
        $settings['cache']['bins']['config'] = 'cache.backend.memcache';

        // Use memcache as the default bin
        $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/';
        $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 Memcache API and Integration module. If you are using Drupal 7, be sure to use version 7.x-1.6 or greater.

Once these requirements are met, CD applications can use the 64MB of allocated memcache.

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.

Acquia strongly recommends that you review the Memcache module's README.txt file for complete documentation regarding its configuration:

To implement both stampede protection and Memcache lock management in Drupal, add the following code as specified:

  • Drupal 8
    1. Add the following code to the settings.phpfile:

      $settings['memcache']['stampede_protection'] = TRUE;
    2. Add the following code to theservices.yml file to move lock management to Memcache:

      # Add in stampede protection
          class: Drupal\Core\Lock\LockBackendInterface
          factory: ['@memcache.lock.factory', get]
          class: Drupal\Core\Lock\LockBackendInterface
          factory: ['@memcache.lock.factory', getPersistent]
  • Drupal 7 - Add the following code to your settings.php file:

    # Add in stampede protection
    $conf['memcache_stampede_protection'] = TRUE;

    # Exclude some bins from stampede protection.
    # NOTE: This is an example. Please see module README.txt, as tweaking may be required! Proceed with caution.
    # See
    $conf['memcache_stampede_protection_ignore'] = array(
      // Disable stampede protection for the some bins
      $conf['memcache_stampede_protection_ignore'] = array(
        // Ignore some cids in 'cache_bootstrap'.
        'cache_bootstrap' => array(
    # Move semaphore out of the database and into memory for performance purposes
    $conf['lock_inc'] = './sites/all/modules/memcache/';

  • For more information, see this discussion on

    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

    Contact supportStill need assistance? Contact Acquia Support