Information for: DEVELOPERS   PARTNERS

Enabling stampede protection and moving locks into memory

About Memcached

Excessive database load may overwhelm the Drupal’s semaphore table, which can cause deadlocks. Encountering deadlocks during database write operations (including INSERT, UPDATE, and DELETE operations) can indicate that Drupal’s lock API is in conflict with InnoDB’s row-level locking mechanism on Drupal’s semaphore table.

Acquia recommends you keep the semaphore locking mechanism in your InnoDB database, due to your InnoDB database being a persistent storage location. Due to semaphore locks being intended for persistent storage, Memcached (as non-persistent storage) isn’t an ideal location for this use case.

Important

If you want to use Memcached to store your semaphore locking mechanism, and are both willing to assume the risk and have tested the effects on your application, the Memcache API and Integration module provides the features and instructions necessary to store Drupal’s semaphore table in Memcached.

Stampede protection

You can also use Memcached to provide stampede protection. In certain situations, using Memcached can help reduce the risk of performance degradation when several requests simultaneously try to write to the cache.

Stampede protection uses Drupal’s locking layer to allow only one process at a time to try to send an item to Memcached. However, MySQL’s InnoDB storage engine isn’t well-suited to managing locks in the semaphore table when experiencing high loads. Due to this behavior, it’s important to also move lock management out of the database and into memory if you enable stampede protection.

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

Important

Enabling stampede protection without also moving locks into Memcached can cause a severe performance degradation if Memcached locks, and then waits to retry. You can use Memcache for lock management without enabling stampede protection.

Enabling stampede protection and lock management in Memcache

For instructions describing how to enable both stampede protection and Memcache lock management in your Drupal application, select the tab that indicates your installed version of Drupal:

Complete the following steps:

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

  2. Add the following lines to enable stampede protection to the sites/default/cloud-memcache-d8.php file (Acquia Cloud) or the factory-hooks/post-settings-php/acsfd8.memcache.settings.php file (Acquia Cloud Site Factory), immediately following the $settings['container_yamls'][] = $memcache_services_yml; line:

    // Enable stampede protection.
    $settings['memcache']['stampede_protection'] = TRUE;
    
    // Move locks to memcache
    $settings['container_yamls'][] = 'sites/default/memcache.yml';
    

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';