HowTo: Redis on Drupal 6 (...update: Memcached)

Update #3: Use the memcache_storage module instead of the memcache module because it better session support and it works with expire.

Update #2: If using memcache for sessions on Drupal 7, you must have a separate memcache server.  From the README.txt:

## SESSIONS ##

NOTE: Session.inc is not yet ported to Drupal 7 and is not recommended for use in production.

Here is a sample config that uses memcache for sessions. Note you MUST have a session and a users server set up for memcached sessions to work.

$conf['cache_backends'][] = 'sites/all/modules/memcache/memcache.inc';
$conf['cache_default_class'] = 'MemCacheDrupal';

// The 'cache_form' bin must be assigned no non-volatile storage.
$conf['cache_class_cache_form'] = 'DrupalDatabaseCache';
$conf['session_inc'] = 'sites/all/modules/memcache/unstable/memcache-session.inc';

$conf['memcache_servers'] = array(
    '10.1.1.1:11211' => 'default',
    '10.1.1.1:11212' => 'filter',
    '10.1.1.1:11213' => 'menu',
    '10.1.1.1:11214' => 'page',
    '10.1.1.1:11215' => 'session',
    '10.1.1.1:11216' => 'users',
);
$conf['memcache_bins'] = array(
    'cache' => 'default',
    'cache_filter' => 'filter',
    'cache_menu' => 'menu',
    'cache_page' => 'page',
    'session' => 'session',
    'users' => 'users',
);


Update: It turns out the Drupal Redis module doesn't support Session or Lock caching so we switched to using Memcached

  1. Install Memcached on the server (yum install memcached or apt-get install memcached) and verify it works (memcached-tool 127.0.0.1:11211 stats | grep 'curr_items\|uptime\|version' should return some valid data [change IP and port to match your environment])
  2. Download and enable the Memcache module
  3. Add the following to the bottom of your settings.php file (replacing sites/all with sites/default if you used that path instead, and change the host, port, and prefix for your specific environment; also, the 6.x branch has stable session caching but the 7.x branch has the memcache-session.inc file in the "unstable" folder):
$conf['memcache_key_prefix'] = 'mysite';
$conf['blocked_ips'] = array();  // optional performance enhancement
$conf['cache_inc'] = 'sites/all/modules/memcache/memcache.inc';
$conf['lock_inc'] = 'sites/all/modules/memcache/memcache-lock.inc';
$conf['session_inc'] = 'sites/all/modules/memcache/unstable/memcache-session.inc';
$conf['memcache_servers'] = array(
  '127.0.0.1:11211' => 'default',
);
$conf['memcache_bins'] = array(
  'cache' => 'default',
  'cache_form' => 'database',
);
$conf['memcache_persistent'] = TRUE;

----------------------

Original post (for historical documentation purposes):

Redis offers a significant performance boost to Drupal and it's less resource intensive than memcached.

So, you're all excited to install it on your Drupal 6 site but when you try to install the Redis module, it tells you it only supports Drupal 7.  So game over?  Nope, here's what you do:
  1. Download the Redis module (yes, the Drupal 7 version) into your site (I put mine in sites/default/modules but you can also put it in sites/all/modules etc.)  Note: "drush dl redis" won't work because Drupal 6 isn't listed for the module so you'll need to download and extract manually.  You don't need to enable this module for it to work.
  2. Download the Cache Backport module into your site (I put mine in sites/default/modules but you can also put it in sites/all/modules etc.).  You don't need to enable this module for it to work.  Also, you don't need this module if using Drupal 7.
  3. Download the Predis library (see buttons at bottom of the download page) and extract it to sites/all/libraries (you'll need to rename the extracted folder so you don't include the version number - e.g. ..../sites/all/libraries/predis/lib/Predis/....  Note: I believe the README.Predis.txt file has a typo because it states "sites/all/libraries/lib/Predis").  P.S. this tutorial assumes you've installed and enabled the Libraries API module.
  4. Install Redis on the server (yum install redis or apt-get install redis-server) and verify it works (redis-cli ping should return "PONG")
  5. Add the following to the bottom of your settings.php file (replacing sites/default with sites/all if you used that path instead, and change the host, port, and prefix for your specific environment):
$conf['cache_inc'] = 'sites/default/modules/cache_backport/cache.inc';
$conf['redis_client_interface'] = 'Predis';
$conf['cache_backends'][] = 'sites/default/modules/redis/redis.autoload.inc';
$conf['cache_class_cache'] = 'Redis_Cache';
$conf['cache_class_cache_menu'] = 'Redis_Cache';
$conf['cache_class_cache_bootstrap'] = 'Redis_Cache';
$conf['lock_inc'] = 'sites/default/modules/redis/redis.lock.inc';
$conf['redis_client_host'] = '127.0.0.1';
$conf['redis_client_port'] = 6379;
$conf['cache_prefix'] = 'mysite_';

     6.   Browse your site and verify it functions as expected.
     7.   In your terminal run redis-cli keys *cache_* to verify Drupal Redis cache is working.


Comments

Popular Posts