Recently 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
- 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.
- 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.
- 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!