Drupal7: Redis PHP Sessions

redisRecently I was asked how to replace the default Drupal database-based sessions with sessions provided by Redis. I have been interested in for some time in compartmentalizing Drupal’s components as much as possible. In the past for instance I have looked into replace the default search provided by Drupal and its database with Apache Solr. Doing these types of infrastructure improvements can vastly improve your site performance if done right. This Redis session inquiry intrigued me.

Redis is an open source, BSD licensed, advanced key-value cache and store which is in-memory, supports networking and can be durable. That’s a fancy way of saying it can store sessions in-memory on a different computer on your network. This can speed up Drupal by removing the sessions and their workload from your Drupal database.

In vanilla PHP sessions are handled and created by the system. As the PHP documentation mentions either PHP, or a PHP Extension may provide a session save handler — out of the box Drupal 7 provides its own session save handler to store and manage sessions. In order to use Redis we must override the Drupal session save handler (override, the override! haha).

This is most easily achieved by using the Session Proxy module. It provides a proxy object which 3rd party libraries may use to manage sessions. Out of the box the Session Proxy modules supports the following kinds of sessions:

  1. Database session handling: core default, ported to the object oriented API. It comes with an additional SQL query done more than core session.inc default implementation due to the API strong encapsulation.
  2. Native PHP session handling: by selecting this session management mode, you explicitly remove any core session management, and let PHP manage it. This can be useful if you intend to use low-level PHP extensions, such as Memcache or PhpRedis to handle session in PHP low-level layer instead of core. This allow to use any PHP native session storage to work gracefully: some of them are high performance.
  3. Cache based storage engine: Using the same session handling than core, but deporting raw session storage into a cache backend instance. You can use any already-functioning cache backend to store sessions. Sessions will use their own bin, and won’t be cleared at cache clear all time.

So we can leverage this module and make use of (2) above. By simply installing this module and configuring it we can leverage Redis sessions.

Redis can be installed on any system with relative ease. Ubuntu installation would be sudo apt-get install php5-redis.

The Session Proxy README.txt then gives the following sample configuration settings for Drupal’s settings.php file:

<?php
  // If you set this to TRUE, session management will be PHP native session
  // management. By doing this, all other parameters below will be ignored.
  $conf['session_storage_force_default'] = TRUE;

By setting session_storage_force_default to TRUE we tell Drupal to use what’s installed on the system — we just installed phpRedis. We can then alter our php.ini setting and tell PHP how to handle sessions:

; In php.ini for system or application.
session.save_handler = redis
session.save_path = "tcp://localhost:6379"

By then restarting your webserver Drupal should be using the Redis backend for storing sessions! Yay!

Looking for quality web hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , , , , , | Posted under Drupal | RSS 2.0

Author Spotlight

David Gurba

David Gurba

I am a web programmer currently employed at UCSB. I have been developing web applications professionally for 8+ years now. For the last 5 years I’ve been actively developing websites primarily in PHP using Drupal. I have experience using LAMP and developing data driven websites for clients in aviation, higher education and e-commerce. If you’d like to contact me I can be reached at david.gurba@arvixe.com

Leave a Reply

Your email address will not be published. Required fields are marked *


1 × = 9

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>