Using Memcached with Drupal 7.x

Last updated 11th May 2021


Web PaaS recommends using Redis for caching with Drupal 7 over Memcached, as Redis offers better performance when dealing with larger values as Drupal tends to produce. However, Memcached is also available if desired and is fully supported.


Add a Memcached service

First you need to create a Memcached service. In your .platform/services.yaml file, add or uncomment the following:

    type: memcached:1.4

That will create a service named cacheservice, of type memcached, specifically version 1.4.

Expose the Memcached service to your application

In your file, we now need to open a connection to the new Memcached service. Under the relationships section, add the following:

    cache: 'cacheservice:memcached'

The key (left side) is the name that will be exposed to the application in the PLATFORM_RELATIONSHIPS variable. The right hand side is the name of the service we specified above (cacheservice) and the endpoint (memcached). If you named the service something different above, change cacheservice to that.

Add the Memcached PHP extension

You will need to enable the PHP Memcached extension. In your file, add the following right after the type block:

# Additional extensions
        - memcached

Add the Drupal module

You will need to add the Memcache module to your project. If you are using a Drush Make file, add the following line to your project.make file:

projects[memcache][version] = 1.6

Then commit the

You must commit and deploy your code before continuing, then enable the module. The memcache module must be enabled before it is configured in the settings.platformsh.php file.


The Drupal Memcache module must be configured via settings.platformsh.php.

Place the following at the end of settings.platformsh.php. Note the inline comments, as you may wish to customize it further. Also review the README.txt file that comes with the memcache module, as it has a more information on possible configuration options. For instance, you may want to consider using memcache for locking as well and configuring cache stampede protection.

The example below is intended as a "most common case".


if (!empty($_ENV['PLATFORM_RELATIONSHIPS']) && extension_loaded('memcached')) {
  $relationships = json_decode(base64_decode($_ENV['PLATFORM_RELATIONSHIPS']), true);

  // If you named your memcached relationship something other than "cache", set that here.
  $relationship_name = 'cache';

  if (!empty($relationships[$relationship_name])) {
    // These lines tell Drupal to use memcached as a backend.
    // Comment out just these lines if you need to disable it for some reason and
    // fall back to the default database cache.
    $conf['cache_backends'][] = 'sites/all/modules/contrib/memcache/';
    $conf['cache_default_class'] = 'MemCacheDrupal';
    $conf['cache_class_cache_form'] = 'DrupalDatabaseCache';

    // While we're at it, use Memcache for locking, too.
    $conf['lock_inc'] = 'sites/all/modules/contrib/memcache/';

    foreach ($relationships[$relationship_name] as $endpoint) {
      $host = sprintf("%s:%d", $endpoint['host'], $endpoint['port']);
      $conf['memcache_servers'][$host] = 'default';

    // If using a multisite configuration, adapt this line to include a site-unique
    // value.
    $conf['memcache_key_prefix'] = $PLATFORM_ENVIRONMENT;

Did you find this guide useful?

Please feel free to give any suggestions in order to improve this documentation.

Whether your feedback is about images, content, or structure, please share it, so that we can improve it together.

Your support requests will not be processed via this form. To do this, please use the "Create a ticket" form.

Thank you. Your feedback has been received.

These guides might also interest you...

OVHcloud Community

Access your community space. Ask questions, search for information, post content, and interact with other OVHcloud Community members.

Discuss with the OVHcloud community

In accordance with the 2006/112/CE Directive, modified on 01/01/2015, prices exclude VAT. VAT may vary according to the customer's country of residence.