Needs work
Project:
Redis
Version:
8.x-1.x-dev
Component:
Code
Priority:
Normal
Category:
Feature request
Assigned:
Unassigned
Reporter:
Created:
5 May 2017 at 15:54 UTC
Updated:
13 Apr 2026 at 12:30 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #2
tobby commentedComment #3
pryrios commentedI am deeply interested in this and was going to implement something similar myself. I will gladly try it to see how it works and help on completing it. It looks promising, and you may very well saved me a lot of time...
Comment #4
tobby commentedPryrios, glad to hear that. Certainly let me know how it fits your use case. I'd be interested in knowing how close to a drop-in replacement this module is for others.
FWIW, while discussing the approach I took on this module, I think a potential future step would be to abstract out the Redis portion and make this backend-agnostic, so that it could use anything else that has a PHP session handler (probably through the use of a basic plugin manager, to point the read/write functionality to the appropriate API).
Comment #5
pryrios commentedSo something on the lines of what https://www.drupal.org/project/session_proxy did on D7?
I was thinking about something similar, although I'm still figuring out how SessionManager and SessionHandler works (I'm not sure what is SessionHandler being used for).
I'm configuring redis/redis_sessions right now, so I will be able to tell you something later today.
Comment #6
pryrios commentedUpdate: It is working good as far as I have tested for basic functionality (login/logout, maintaing session for awhile, storing values in $_SESSION, etc...).
I will develop some tests in the coming days, including stress tests, as I progress on my project to see if I detect some strange behavior.
Good work!
Comment #7
perignon commentedI could use this on Drupal7. I am migrating from MongoDB to Redis. I will see if I have time to backport this.
Comment #8
berdirThanks. A test for this would be great, we can't execute them on drupal.org but we usually do that as merge requests then on https://github.com/md-systems/redis.
There is for example a full web test that enables all the redis services and does a few things, we could include the session module there.
Comment #9
andreperazzi commented@tobby is there a way to specify the redis database in the save_path?
I'm trying
But I'm getting a
Comment #10
andreperazzi commentedYeah, this patch doesn't seems to be working on 8.x-1.0-rc2
At first I thought it was working, but after flushing the redis db, I was still logged in. Also, The error I mentioned above happening while trying to logout.
Comment #11
pianomansam commented@andreperazzi Is Predis supported in the Drupal 8 version of this module? This module's frontpage leads me to believe that it isn't:
I have this patch up and running fine with phpredis.
Comment #12
o'briat1. The RedisSessionsSessionManager->getSavePath method only gets server IP from Redis module, the port is hardcoded.
2. I get the following errors with 8.5
Error: Call to undefined method Symfony\Component\HttpFoundation\Session\SessionBagProxy::all() in Drupal\redis_sessions\RedisSessionsSessionManager->getSessionBagUid() (line 172 of modules/contrib/redis/modules/redis_sessions/src/RedisSessionsSessionManager.php).or
RuntimeException: Failed to start the session in Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage->start() (line 146 of /app/vendor/symfony/http-foundation/Session/Storage/NativeSessionStorage.php).3. This module doesn't work with sentinel or cluster modes. Either add a method to ClientInterface to return the current Redis IP/Port or just stick to
$settings['save_path']Comment #13
dave reidI was encountering too many issues with the current patch. I addressed some of the feedback from #12. I think the behavior to disable saving anonymous user sessions should be configurable still.
Comment #14
berdirThis will definitely need test coverage.
\Drupal\Tests\redis\Functional\WebTest should already use it automatically as it relies on the example.services.yml. To run tests, someone needs to start a pulll request against https://github.com/md-systems/redis.
For other backends, we often extend the core test coverage e.g. \Drupal\Tests\redis\Kernel\RedisLockTest. I guess \Drupal\system\Tests\Session\SessionTest would be a candidate for this. There is at least one method that does a direct query against the session table, so at least that will need to be duplicated, but the others *might* work?
Comment #15
mike82 commentedWhy are you creating a new SessionManager? Wouldn't it be simpler to create a new SessionHandler and alter the service class for the core session_handler.storage to use the newly created RedisSessionHandler?
Comment #16
mpastas commented#13 patch + Fixing getSessionBagUid error
Comment #17
dave.weiner@mpastas, I just tried applying your patch to drupal/redis:1.2 on a site running drupal/core 8.6.15 and PHP 7.3.11 and I'm seeing this error now
Same setup but with PHP 7.2. and it runs just fine
`
Comment #18
pierrelbzGreat job but i have problem with bigpipe :
(D 8.8.1 , php 7.2.24)
Comment #19
mglamanThis seems broken if it exists in the submodule, which decorates the parent service.
Remove this, added by packager
Reroll for D9
This isn't needed since you're extending the class directly, not just implementing the interface.
This could be replaced with a parent::save call.
Comment #20
mglamanAlso, couldn't we find a way to leverage \Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler introduced in Symfony 4.1?
Comment #21
ugolek commentedIn the case of using clustered Redis (PhpRedisCluster), there are some changes required, check this for details.
I have attached a refreshed patch that provides basic support for the clustered Redis.
Comment #22
ugolek commentedI have rerolled patch and applied some adjustments from https://www.drupal.org/project/redis/issues/2876099#comment-13946294.
Comment #23
vladbo commentedAs I can see TTL for users sessions is not set (-1). Probably it should be fixed by something like this:
instead of
Comment #26
grimreaperHello,
I have created a MR for easier code review.
I have not tested the patch as I intended to do initially because I don't understand why there is a new class in the main Redis module and almost the same class in a submodule. Which service should be used?
Comment #27
grimreaperHello,
I have tested the class which was in the main module as its code seemed more elaborated. I put it in the submodules, cleaned up dependency injection.
I searched to leverage \Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandle as mentionned in comment 20. I will try to make the change tomorrow.
Still a lot of code I wonder why it here.
Also, I wonder if two classes should not be made to distingish phpredis and predis, and a factory introduced such as in the main Redis module.
Comment #28
grimreaperHello,
I have updated the MR to:
- use Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler as mentioned in comment 20
- only override the Session storage as mentioned in comment 15
Which results in a simpler change.
I have currently removed the settings.php config, as it uses the one used for the cache.
I think it should be possible to declare a dedicated redis connection for sessions, but this would mean to refactor ClientFactory if I am not mistaking.
And it still needs tests. I will try to give a look at that.
Thanks for the review.
Comment #29
grimreaperTest added.
I will open a PR on https://github.com/md-systems/redis
Comment #30
berdirYay tests. FWIW, I don't think tests on github are still working, I don't think that thing we use there is still available? If someone wants to integrate something else that is still available for free for open source projects that would be great, but I won't have time to do that myself I think.
Comment #31
grimreaperPR created https://github.com/md-systems/redis/pull/33.
Thanks for the review!
Tests does not seem to be triggered...
PS: lol cross posting :)
Comment #32
grimreaperAnd tests on drupal.org, customizing drupalci.yml to install a redis?
The problem may be the matrix of tests between phpreid and predis.
All the tests to update...
Comment #33
grimreaperSorry for spamming.
Is it only that the github repository needs to use https://www.travis-ci.com/ and no more https://travis-ci.org/?
Also I wonder, regarding the change size, if this could not be included in the Redis module directly, so no need to create a sub-module.
Comment #34
berdirI don't think we can install a redis server or php extension through drupalci commands.
And no, I don't think it's just changing to .com. .org was the free service for open source projects, AFAIK that is no longer provided at all.
Comment #35
grimreaperTo have been able to install java, go https://git.drupalcode.org/project/file_extractor/-/blob/4.0.x/drupalci.yml
Or Docker compose https://git.drupalcode.org/project/cmis/-/blob/3.0.x/drupalci.yml
I think it is possible to have a redis server running on drupalci.
On https://www.travis-ci.com/ it is written "Testing your open source projects is always 100% free!" but as I am not using it on a daily basis I don't know if some changes happened.
Comment #36
darvanenThis looks really good, nice simple implementation using existing components.
As far as code review goes I have one question about class naming: is this a factory or a proxy? It feels like a proxy to me but I'm very happy to be wrong.
I would personally like to see this simply included in the main redis module, I see no reason to cordon off sessions as a special case, but again, happy to be outvoted/wrong about that.
I couldn't use the patch generated by GitLab so if anyone else is looking to try this out, here's a patch against 8.x-1.x from the current state of the MR. No new work in this patch.
Tested out the patch, works beautifully, thank you!
Comment #37
darvanenSo I found a potential problem, in short: I keep getting logged out.
Turns out standard Drupal is pretty bad at session garbage collection, but redis is excellent, which is one of the reasons I wanted to use it in the first place. A bit of testing revealed to me (you all might already know these):
This led to a situation where I thought Drupal had some defaults in place that resulted in long session lifetimes and I expected that to remain true when I switched to redis as the backend. It didn't.
This could be solved by documentation of course. I wonder if it's reason enough to leave this in a separate submodule so that it's a conscious choice to include it and configure it properly.
Comment #38
darvanenAdded a bit of info about session lifetimes to the readme, can't hurt.
Comment #39
kevineinarsson commentedThis works well for me, marking as RTBC.
Only issue I noticed was if $settings['session_write_interval'] > gc_maxlifetime your sessions will always expire. Not that anybody will realistically run into this. I only did because I was testing writes.
Comment #40
s_leu commentedWorking well for me too, tested in the scope of #3267531: Error in WriteSafeSessionHandler when switching to read only DB user
Comment #41
b n pandey commentedI am using the redis module for drupal login session management in my project using this article (https://stackoverflow.com/questions/41486546/drupal-8-overriding-session...). Now what happens is if the same user login from multiple browsers it creates multiple sessions for the same user. But the requirement is only one active session will be allowed at a time for the same user id. If the user has an ongoing session in any other browser or system, upon logging in to another, will be logged out of the previous active session.
Can anyone help on this issue?
Comment #42
pianomansam commented@B N Pandey what you describe sounds like it's outside the scope of this project. A single session per user is not the default way PHP or Drupal sessions work.
Comment #43
b n pandey commentedThanks for your update @pianomansam. Is there any way to achieve this functionality, any code or reference using redis?
Comment #44
darvanen@B N Pandey redis is a storage engine, it stores whatever you put in it, so no, there is no redis-specific way to achieve what you're asking.
Sessions are not the way to keep track of users across devices, it just can't be done that way.
If you need to store user-specific data for logged in users maybe have a look at https://api.drupal.org/api/drupal/core%21modules%21user%21src%21UserData...
If you want to track an anonymous user across multiple devices you'll need some kind of third party identification system. Pretty sure that kind of thing would exist, also pretty sure it would violate a bunch of privacy principles.
Comment #45
darvanenForced rebase against dev branch. Here's a patch if anyone needs it.
Comment #46
darvanenAdded a commit to make the submodule compatible with Drupal 10
Comment #47
berdirMerge request is failing.
Comment #48
darvanenAh ok, it wasn't that simple then, I'll do a more thorough job, thanks for pointing it out :)
Comment #49
kksandr commentedYou can easily override the Drupal session factory via *services.yml to the Redis session factory (a component that Symfony provides).
I'm not sure if there are any downsides to this approach. If not, then mentioning this feature in the README would be helpful.
Comment #50
hmdnawaz commented@kksandr
How your
redis.clientclass looks like?Comment #51
kksandr commented@hmdnawaz I use phpredis, so it's
class: Redis, which is the global class provided by that PHP extension.Comment #52
hmdnawaz commentedIn case of Drupal redis module, can we use?
Comment #54
darvanen#52: works for me ;) thanks!
Comment #55
hmdnawaz commentedThere 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.
Comment #56
shiv_sharma commented#52: works for me ;)
just adding steps
in your custom module add
and in settings.php add :
$settings['session_handler_class'] = 'Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler';hope this help to others :)
Comment #57
darvanenI propose we adjust this issue to fully document that approach in the module code and docs.
Comment #58
devkinetic commentedI'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.
Comment #59
fraserthompsonFYI as far as I can tell the $settings variable overrides above do nothing in Drupal 11 and the only way to swap the session handler is via overriding the services in services.yml.
Comment #61
steinmb commentedThank you for working on this.
As @berdir now have open 2.x and writes that, no new features will be added to 8.x-1.x. Should perhaps this feature be built on top of the 2.x branch?
Comment #62
o'briatHi,
As I understand Redis the LRU eviction policy is a global server setting (all keys and db). So if you setup sessions and cache on the same instance, sessions could be delete if memory is full.
Did you find a way to manage this side effect?