Information for: DEVELOPERS   PARTNERS

Enabling stampede protection and moving locks into memory

About Memcached

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:

Important

Enabling stampede protection without also moving locks into Memcache can cause severe performance degradation if Memcache locks and requires waiting for a retry. You can use Memcache for lock management without enabling stampede protection.

To implement both stampede protection and Memcache lock management in Drupal, add the following code as specified, depending on your version of Drupal:

Drupal 8 procedure

To implement both stampede protection and Memcache lock management in Drupal 8, complete the following steps:

  1. Download and add this memcache.yml file to your website’s docroot/sites/default directory.

  2. In the downloaded memcache.yml file, uncomment the container_yamls line, and then modify the line to match the location of memcache.yml in your codebase:

    // DO MODIFY the following path (relative to DRUPAL_ROOT) depending
    // on where you placed this file in your codebase.
    $settings['container_yamls'][] = 'sites/default/memcache.yml';
    
  3. Uncomment the line following line to enable stampede protection in the cloud-memcache-d8.php file:

    $settings['memcache']['stampede_protection'] = TRUE;
    

Drupal 7 procedure

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

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

$conf['memcache_stampede_protection_ignore'] = array(
  // Ignore some cids in 'cache_bootstrap'.
  'cache_bootstrap' => array(
    'module_implements',
    'variables',
    'lookup_cache',
    'schema:runtime:*',
    'theme_registry:runtime:*',
    '_drupal_file_scan_cache',
  ),
  // Ignore all cids in the 'cache' bin starting with 'i18n:string:'
  'cache' => array(
    'i18n:string:*',
  ),
  // Disable stampede protection for the entire 'cache_path' and 'cache_rules'
  // bins.
  'cache_path',
  'cache_rules',
);

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