Jump to content
thetuningspoon

Does SessionHandlerDB resolve session blocking on AJAX requests?

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.

Share this post


Link to post
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 2
  • Thanks 1

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
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 3

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...