John Davidson

php - Issues with session write/read since changing from server sessions to redis sessions

0 comments
Message:


Our PHP application is showing some strange behaviour since we made the switch from regular server sessions to sessions stored in Redis.


The PHP code hasn't changed, but we notice an issue when writing a session variable which is read in the same function chain.


I'll try to give an example of the code with pseudo code:


  //some code
...
session_start();
//now we write the session variable
$_SESSION[key] = value;
session_close();
...
//some other code

return getSomeSpecificValue();
}

function getSomeSpecificValue(){
//some code
...
session_start();
$someVar = $_SESSION[key];
session_close();
...
//some code

}

Now the problem is that, since moving to redis sessions, it happens that the variable is empty when reading it, even though we have just written it.


That behaviour did not occur before the move to Redis. Is there a specific race condition or setting we are missing? Or do we need to change the code now?


If anyone can help us out with pointers on where to look, that would be much appreciated. If you need more info, let me know, we have been debugging this issue but can't seem to find a solution for it. Race condition? Data lock? ..


When refreshing the page, the value is not empty anymore, so we are pretty sure it's written to redis, probably on the first attempt (?) :-).



My Answer:
It sounds like you may be experiencing a race condition due to the way sessions are handled in Redis. When you call `session_close()`, it may not immediately write the session data to Redis, leading to the issue you are seeing when trying to read the session variable in the same function chain.

One way to potentially solve this issue is to call `session_write_close()` instead of `session_close()`. This function will write the session data to the storage mechanism (in this case, Redis) and then close the session. This should ensure that the session data is written before you try to read it in the same function chain.

Additionally, you may want to check the Redis configuration to ensure that it is properly configured for session handling. Make sure that Redis is set up to handle session data correctly and that there are no issues with data locking or race conditions.

If you continue to experience issues, you may need to consider restructuring your code to handle session data in a different way. This could involve separating the writing and reading of session data into different function calls or using a different approach to handle session data in your application.

Overall, it's important to carefully review your code and the Redis configuration to identify any potential issues that may be causing the behavior you are seeing. If you continue to have trouble, consider reaching out to the Redis community or seeking help from a developer with experience in handling session data with Redis.

Rate this post

3 of 5 based on 5027 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us