1. Home
  2. cPanel
  3. Memcached: How to configure in-memory cache

Memcached: How to configure in-memory cache

cPanel customer benefit from improved website performance provided by in-memory caching service Memcached. Configure Memcached in these easy steps.

What is memcached

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

The most common use of Memcached is storing cache tables of your CMS 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.

Configure memcached support in cPanel

Before you begin configuring Memcached, ensure the PHP memcached extension is activated.

  1. Login to cPanel
  2. Go to Select PHP Version and ensure the memcached (with d) is selected. Do not select memcache.
  3. Click Save button.

Install CMS plugins to support memcached

Depending on the CMS you plan to use Memcached for, you need to locate and install plugin that supports it. For Drupal 7, use the Memcache module, for Drupal 8, use the Memcache Storage module (refer to the module installation instructions and skip the rest of this page), for WordPress use W3C Total Cache

Installing in Drupal 7

  1. Download Memcache module
  2. Add the configuration variables into the settings.php file. Refer to the module’s bundled README file for other options available.

Note the correct location path of the memcache module and add the following code in your settings.php file, make sure you add secure hash of at least 24 characters:

$conf["cache_backends"][] = "./sites/all/modules/memcache/memcache.inc"; // Make sure installation path reflects the real module location.
 $conf["cache_default_class"] = "MemCacheDrupal";
 $conf["cache_class_cache_form"] = "DrupalDatabaseCache";
 // Important to add the variable below to your settings.php file
 // in order to avoid caches clash with other websites using memcached.
 // Good examle of the key prefix: "dGnyJieVUBocTL0PNDlI3Zx2HQjWtEzs", 
 // bad example: "mycompanydomain.com".$conf["memcache_key_prefix"] = "unique_secure_hash";
 $conf["memcache_key_prefix"] = "unique_secure_hash";p

Note! It is important to set the $conf[“memcache_key_prefix”] variable and set the key prefix to a unique secure hash value per every website and environment (in the case you use more than one site in your account or use dev/test/uat environment replicating your website). This is to avoid cache clashes and unpredictable behaviour of your website.

Enabling stampede protection and moving locks into memory

The Memcache module can provide stampede protection for better performance. This helps minimise 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 use Memcache for lock management without necessarily enabling stampede protection. To implement both stampede protection and Memcache lock management in your website’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";

 

Further tuning memcached in Drupal

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.

Using the Memcache Admin module

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.
View the Memcache statistics on your website’s Administration > Reports > Memcache statistics page.

Check the Memcache statistics page to see how the cache is behaving.

You can further improve performance of your website by adding the following configuration variables into the settings.php file:

$conf["page_cache_without_database"] = TRUE; 
$conf["page_cache_invoke_hooks"] = FALSE;

 

Installing in Drupal 8

  1. Download and install Memcache storage module
  2. Configure module according to module’s configuration instructions

Installing in WordPress

  1. Download and enable W3C Total Cache plugin
  2. Configure plugin to use Memcached service

Updated on 7 June, 2017

Was this article helpful?

Related Articles