Jump to content

Does SessionHandlerDB resolve session blocking on AJAX requests?


thetuningspoon
 Share

Recommended Posts

I am having trouble with executing multiple AJAX requests at the same time. The response is very slow and seems to be synchronous instead of asynchronous. I am using jQuery's ajax() function, so I don't think there's any issue on the client side. I think it is an issue with Session locking/blocking.  I read somewhere that SessionHandlerDB would resolve this issue, but enabling it has not improved the situation any. I just want to confirm whether or not SessionHandlerDB is designed to handle concurrent requests efficiently, so I know what to rule out.

Link to comment
Share on other sites

I don't think it would resolve the issue. SHDB just saves to DB instead of the file-system. But PHP will still do one session request after another and probably uses db table locks instead of file locks (at least that's my guess).

Are you using PW session functions or native PHP? Towards the bottom of this article there is PHP 7 option:

session_start([
    'read_and_close' => true,
]);

https://ma.ttias.be/php-session-locking-prevent-sessions-blocking-in-requests/

The author also mentions Redis (there's also a redis session handler db module for PW), but if you're not using it it won't be of much use...

  • Like 3
  • Thanks 1
Link to comment
Share on other sites

I did try turning off SessionHandlerDB and using php's session_write_close() and that didn't seem to help either. I think I also tried $session->close() (which just calls php's function).

Moreover, there are a couple ajax requests which do need to be able to write to the session, so a blanket session_write_close if $config->ajax is true wouldn't solve my issue :(

Edit: I am going to try Redis. Will let you know how it goes.

Link to comment
Share on other sites

  • 8 months later...

An update on this for others:

Using Redis for sessions solved the problem for me. I had to make some changes to the module to get it to work right (if anyone wants my code, let me know).

Today I happened to try migrating my project from using ProcessWire's default MyISAM database engine to using InnoDB (had to convert all tables and set $config->dbEngine in site/config.php). I was playing around with the demo version of my system (which is not using Redis). And interestingly enough, I THINK this (in combination with SessionHandlerDB) actually resolved the issue with simultaneous ajax calls! Perhaps this is because InnoDB has row-level locking instead of table-level locking?

  • Like 3
Link to comment
Share on other sites

@thetuningspoon

I think you are on target here. There's a lot to be said for the InnoDB engine, and the row level locking vs table level should reduce contention and decrease latency with it. Redis, despite being single threaded, is writing to an in-memory key value store - so it's blindingly fast. 

I would be interested in seeing your updated code for the Redis session handler - if that's what you mean, as I need to update that module.

  • Like 4
Link to comment
Share on other sites

@netcarver

Thanks for chiming in. I just submitted a pull request on GitHub. Looks like I had submitted a bug report at the time, which this pull request resolves. I also added a password config option, and made some change to the read() method (but I don't remember what it's doing exactly).

  • Like 4
Link to comment
Share on other sites

  • 8 months later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...