Setting Up Redis Caching with Drupal
Introducing Redis #
In the past, I have spoken about configuring Varnish with your Drupal site.
Varnish stores web pages in memory so the web servers don’t have to create the same web page over and over again. The web server only recreate a page when it is changed. Additionally Varnish can serve web pages much faster then any application server is capable of - giving the website a significant speed up.
Another caching technology that is often used as a compliment to Varnish is Memcached:
Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering.
In the world of Drupal, Memcache is most often used as a more efficient replacement for database caching. Redis, which is an advanced key-value store, can be thought of in this context as a drop-in replacement for Memcache.
Is It Time to Ditch Memcache for Redis? #
Memcache has served us well, but recently, I have begun to replace it with Redis on the sites that I manage. It offers what I see as two big benefits over Memcache:
- Persistence. Redis offers a wide range of persistence options that can be configured to meet the needs of your site.
- Powerful data types (strings, lists, sets, hashes, and sorted sets).
Either one of these could be the killer feature for you, but in my experience, for sites with large amounts of editorial data and not-insignificant caching concerns, the possibility of persisting that cached data is what gets people excited. If you restart Memcache, the data store is going to be empty when it starts back up since all of the data is stored in volatile memory. While this can sometimes be convenient when troubleshooting, in the real world this could bring your site to a crawl. The ability to restart your Redis server, whether it’s voluntary or otherwise, and not worry about having an empty cache is worth the price of admission on its own.
Implementing Redis with Drupal #
So how do we get this going? Here’s a secret, it’s even easier then setting up Memcache.
- Install Redis on your server. To install Redis on Ubuntu run the following commands:
apt-get install redis-server
Also: Instructions for installing Redis on CentOS.
Configure Redis. Below is an example redis.conf configuration. For more detailed instructions on configuring Redis, Linode has a great guide.
Download and install the Redis Drupal module. If you are running Drupal 6, you will also need to install the Cache backport module.
Install one of the PHP Redis libraries. The two recommended options here are Predis and PhpRedis. The Drupal Redis module contains specific installation instructions for each of these libraries but the next step contains instructions for Predis.
Configure your Drupal site to use Redis/Predis for caching instead of the Drupal cache database tables.
$conf['cache_backends'] = 'sites/all/modules/redis_cache/predis.inc';
$conf['cache_class_cache'] = 'RedisPredisCache';
$conf['cache_class_cache_menu'] = 'RedisPredisCache';
$conf['cache_class_cache_bootstrap'] = 'RedisPredisCache';
You can also configure any other bins you deem necessary. Predis will attempt to connect to localhost by default, but if you have set up Redis on its own server, point it there:
$conf['redis_cache_uri'] = 'tcp://18.104.22.168:1234';
Between its powerful features and its simple setup, Redis can be an attractive alternative to Memcache for Drupal caching. Feel free to jump on in the deep-end with Redis for the next site you build or run it in parallel with Memcache for an existing site. You can switch between the two with just some small changes to your settings file until you are comfortable moving to Redis completely.
If you are interested in my services to help you build or scale your Drupal site, please contact me at arkic.