Setting up memcache and memcached in Drupal

Memcache had always seemed like some magic black box to me until the last time I set it up and really took the time to understand what it does.

I'd say there are two main benefits to memcache:

  1. Reducing hits on the database - all of Drupal's normal caching can be set to store in memory instead of the database thereby reducing expensive inserts and large numbers of reads
  2. If you have your database on a different server then you will see a massive performance increase because cached data is stored on the web server and therefore you don't have the overhead of network IO

Setting up memcache consists of the following steps:

To install memcached and memcache:
sudo apt-get install php5-memcached php5-memcache

Then in your php.ini:

extension=memcache.so
memcache.hash_strategy="consistent"

Restart Apache and check your phpinfo() for memcache.

Then get the Drupal memcache module (you don't need to install it but you need the module in place). Once in place configure Drupal to use memcache by adding the following lines to your settings.php inside the $conf array (you may need to uncomment the open and close of the array:

'cache_inc' => './sites/all/modules/contrib/memcache/memcache.inc',
'memcache_servers' => array(
'127.0.0.1:11211' => 'default',
),
'memcache_bins' => array(
'cache' => 'default',
'cache_block' => 'default',
'cache_content' => 'default',
'cache_filter' => 'default',
'cache_form' => 'default',
'cache_menu' => 'default',
'cache_page' => 'default',
'cache_update' => 'default',
'cache_views' => 'default',
'session' => 'default',
'users' => 'default',
'cache_performance' => 'default',
),

Later on I might explain all the settings above, but essentially it tells Drupal what file to use to talk to memcache, where the server is and what tables to use memcache for.

How do you know it's working?

Memcache has several tools and you can telnet here and there but I've found the easiest way to know for sure is to check your cache tables aren't being populated. If you comment out the 'cache_inc' line in your settings.php then clear your cache or empty your cache table you should see it gets filled up as you browse your site. With the line uncommented and an empty table, you should find it stays empty. You will also see cache_set and cache_get queries drop out of the devel query logger.

Post new comment

By submitting this form, you accept the Mollom privacy policy.

User login

Author of...

  • My blog about 'Time is NOT money'... and value http://t.co/g2C6pZy0 12 years 10 weeks ago
  • I've found an excellent query logging and filtering module for Drupal. Not available via http://t.co/U1Xe92Th: http://t.co/2qvg1DgE 12 years 14 weeks ago
  • I've just spotted my first © 2011 at the bottom of a website. 12 years 14 weeks ago
  • @da_lune That's the spirit, oh, too late @gavinbrook, Wordpress is fantastic & now more than a blogging tool but Drupal = more functionality 12 years 18 weeks ago
  • @oliverpolden is having an amazing day for many reasons. I feel like giving back: http://t.co/JvKVVMBF Have a request? use the contact form. 12 years 18 weeks ago
  • The world's population is 7 billion. If a few 'competing' companies mutually helped each other get more customers business would be easy. 12 years 18 weeks ago
  • @Casablanca Amazing photos! Thank you for sharing. 12 years 19 weeks ago
  • Ahh, the Christmas Snow module for Drupal: http://t.co/QzvkQiT2 12 years 19 weeks ago
Oliver Polden