How to setup Memcache on CentOS 7 for Drupal site

One way to optimize the authenticated users experience in Drupal site is to use Memcache. It works between the database and Drupal. Typically the queries are cached in the database but with Memcache the queries sent from Drupal to database are intercepted by Memcache and it serves them from RAM and this avoid hits to the disk or database. We see often that half of the queries are served by Memcache and we are looking significant improvement in authenticated users hits.

Lets take a look how to setup Memcache on CentOS 7 for Drupal site. The following procedures are tested on my Linode server running Centos 7 64-bit Linux distribution.

  1. Install the Memcache daemon:
      
    yum -y install memcached
      
    
  2. Configure Memcache daemon:
      
    vi /etc/sysconfig/memcached
      
    

    Change the following:

      
    CACHESIZE="256"
    OPTIONS="-l 127.0.0.1"
      
    

    CACHESIZE is the RAM you allot to Memcache daemon. Start it:

      
    systemctl start memcached.service
    systemctl enable memcached.service
      
    
  3. Confirm the Memcache service running status:
      
    memcached-tool 127.0.0.1:11211 stats
      
    
  4. Check the remote connectivity:
      
    watch "echo stats | nc 127.0.0.1 11211"
      
    
  5. To integrate PHP and Memcache we need to install php-pecl-memcache:
      
    yum -y install php-pecl-memcache
      
    
  6. In your Drupal site's "settings.php" file, append the following codes:
      
    /**
     * Memcache configurations
     */
    $conf['cache_backends'][] = 'sites/all/modules/memcache/memcache.inc';
    $conf['lock_inc'] = 'sites/all/modules/memcache/memcache-lock.inc';
    $conf['memcache_stampede_protection'] = TRUE;
    $conf['cache_default_class'] = 'MemCacheDrupal';
    // The 'cache_form' bin must be assigned to non-volatile storage.
    $conf['cache_class_cache_form'] = 'DrupalDatabaseCache';
    // Don't bootstrap the database when serving pages from the cache.
    $conf['page_cache_without_database'] = TRUE;
    $conf['page_cache_invoke_hooks'] = FALSE;
    // If this server has multiple Drupal installation
    // assign unique key for memcache namespace purposes
    $conf['memcache_key_prefix'] = 'www_yoursite_com';
      
    

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.