Create submodule to store PHP sessions in Redis

Created on 5 May 2017, almost 8 years ago
Updated 27 March 2023, almost 2 years ago

I have written a module called Redis Sessions. It is dependent on the Redis module to connect to a Redis instance, and use that for session management using the Redis module's API. It's intended to be a drop-in replacement for Drupal core's sessions in the database, with all the normal Drupal performance enhancements (like no session writes for anonymous users and the session_migrate method).

I'd like to share this module and potentially get it added to the Redis project as a submodule. I'll add a patch for this.

Feature request
Status

RTBC

Version

1.0

Component

Code

Created by

🇺🇸United States tobby

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇦🇺Australia darvanen Sydney, Australia

    Forced rebase against dev branch. Here's a patch if anyone needs it.

  • Pipeline finished with Failed
    over 1 year ago
    Total: 146s
    #44745
  • 🇦🇺Australia darvanen Sydney, Australia

    Added a commit to make the submodule compatible with Drupal 10

  • Status changed to Needs work over 1 year ago
  • 🇨🇭Switzerland berdir Switzerland

    Merge request is failing.

  • 🇦🇺Australia darvanen Sydney, Australia

    Ah ok, it wasn't that simple then, I'll do a more thorough job, thanks for pointing it out :)

  • Pipeline finished with Failed
    over 1 year ago
    Total: 529418s
    #44747
  • You can easily override the Drupal session factory via *services.yml to the Redis session factory (a component that Symfony provides).

    services:
      redis.client:
        class: Redis # Your Redis client class.
        factory: [ '@redis.factory', 'getClient' ]
      session_handler.storage:
        class: Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler
        arguments: [ '@redis.client', { prefix: 'drupal_session:' } ]
    

    I'm not sure if there are any downsides to this approach. If not, then mentioning this feature in the README would be helpful.

  • 🇵🇰Pakistan hmdnawaz

    @kksandr

    How your redis.client class looks like?

  • @hmdnawaz I use phpredis, so it's class: Redis, which is the global class provided by that PHP extension.

  • 🇵🇰Pakistan hmdnawaz

    In case of Drupal redis module, can we use?

    services:
      redis.client:
        class: Drupal\redis\Client\PhpRedis # Your Redis client class.
        factory: [ '@redis.factory', 'getClient' ]
      session_handler.storage:
        class: Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler
        arguments: [ '@redis.client', { prefix: 'drupal_session:' } ]
    
  • First commit to issue fork.
  • Pipeline finished with Failed
    6 months ago
    Total: 166s
    #260094
  • Pipeline finished with Failed
    6 months ago
    Total: 275s
    #260112
  • 🇦🇺Australia darvanen Sydney, Australia

    #52: works for me ;) thanks!

  • 🇵🇰Pakistan hmdnawaz

    There is an issue when using the RedisSessionHandler. When you clear the cache from the UI or use Drush, the logged-in user gets logged out.

  • 🇮🇳India Shiv_Sharma

    #52: works for me ;)
    just adding steps

    in your custom module add

    services:
      redis.client:
        class: Drupal\redis\Client\PhpRedis
        factory: [ '@redis.factory', 'getClient' ]
    
      session_handler.storage:
        class: Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler
        arguments: [ '@redis.client', { prefix: 'drupal_session:' } ]

    and in settings.php add :
    $settings['session_handler_class'] = 'Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler';

    hope this help to others :)

  • 🇦🇺Australia darvanen Sydney, Australia

    I propose we adjust this issue to fully document that approach in the module code and docs.

  • Pipeline finished with Failed
    4 months ago
    Total: 189s
    #325243
  • Pipeline finished with Failed
    4 months ago
    Total: 355s
    #325242
  • 🇺🇸United States devkinetic

    I'm not sure what exactly is the "correct" way to do this... on a recent project we simply did $settings['session_storage']['class'] = 'Drupal\redis\Cache\PhpRedis'; and it seemed to work pretty well! if anyone can provide a comparison or justification on the best approach, I would much appreciate it.

    In general, for our project, which consists of mostly logged in users, we saw a ~600% increase in throughput under load between that setting and also adding $settings['session_write_lock'] = FALSE;. Managing sessions through Redis appears to be a boon for performance and it would be great if this got rolled into the "default" config and setup.

    YMMV of course, as there are so many factors to consider.

Production build 0.71.5 2024