pantheon-systems / wp-redis

WordPress Object Cache using Redis.
https://wordpress.org/plugins/wp-redis/
GNU General Public License v2.0
225 stars 68 forks source link

[feature request] Enable to use Redis as user session storage #325

Closed jespervnielsen closed 3 years ago

jespervnielsen commented 3 years ago

Since wordpress stores user sessions in db, it could maybe be worth to have the option to store user sessions in redis.

I have looked around, and did find this https://github.com/ethitter/WP-Redis-User-Session-Storage/blob/master/inc/class-wp-redis-user-session-storage.php

But then i thought "does pantheon not store user sessions in redis" or what do they do?

So this is kind of a feature request, or just a question.

Would you go with just to include above repo, as well as this repo, or would it make sense to implement user sessions in this repo as well?

danielbachhuber commented 3 years ago

Hey @jespervnielsen,

Thanks for the question!

Since wordpress stores user sessions in db, it could maybe be worth to have the option to store user sessions in redis.

Could you explain with a bit more detail why you think it would be useful to have the user sessions stored in Redis?

jespervnielsen commented 3 years ago

@danielbachhuber

Its much faster to read from Redis rather than MySql - but thats why this repo exists in the first place :D

WordPress uses "user meta" sessions tokens. (class: WP_User_Meta_Session_Tokens) https://developer.wordpress.org/reference/classes/wp_user_meta_session_tokens/ Example screenshot, from a small WooCommerce site with a few visits each day. (I could imagine this is much bigger on big WooCommerce sites) where there is 6000+ entries related to the sessions image

So each user requires writes, when they initialize, and then reads on every pageload.

As far as i understand, redis has 2 elements. "cache" and a "session store". Right now, this repo, makes use of "cache" - and it would be nice, if it also handles "session store"

I think it could make a huge improvement for sites with many users and/or WooCommerce. At least it would make a improvement for the hosting providers, since it would reduce the reads (maybe also writes?) to the db.

Im not sure how often the Session Manager is called, but on a test site, when in the wordpress dashboard, the WP_User_Meta_Session_Tokens::get_sessions() gets called 3 times. on 1 request. (for 1 user). and 1 time when on frontpage. So many users would require many unessesary reads from the database rather than just using redis.

"Articles" about redis and user management:

Stackoverflow, about if its "safe" to use redis (mostly because people think of redis as "cache" and does'nt know "session manager" is an option. https://stackoverflow.com/questions/10278683/how-safe-is-it-to-store-sessions-with-redis#:~:text=Redis%20is%20perfect%20for%20storing,and%20writes%20will%20be%20fast.&text=If%20you%20cannot%20afford%20losing,are%20saved%20to%20the%20disk.

Redis article about session management. https://redis.com/solutions/use-cases/session-management/

Redis article to understand the difference of cache and session store. https://redis.com/blog/cache-vs-session-store/

danielbachhuber commented 3 years ago

Thanks for the detail, @jespervnielsen!

Its much faster to read from Redis rather than MySql - but thats why this repo exists in the first place :D

Generally, this should only be the case if you're performing complex queries in MySQL. If a simple SELECT is slow on an indexed column, then something might be wrong with your database.

Offloading user session storage to Redis is probably outside the scope of WP Redis. WP Redis is meant to be a drop-in replacement for the WordPress Object Cache, and WordPress' internal Object Cache doesn't take on any responsibilities for user session management.

Hope this helps!