neo4j-php-client icon indicating copy to clipboard operation
neo4j-php-client copied to clipboard

Share session between HTTP request in web apis / web applications

Open vzenix opened this issue 2 years ago • 11 comments

Is your feature request related to a problem? Please describe. I have complex queries, the first execution in a session it consume to much time, but next time execute it fast in the same HTTP Request, but when y start a new HTTP, the library create a new session, and again the query work slow, I have this time for example in complex queries

Microtime: '0.90921611785889' sec
Microtime: '0.18274807929993' sec
Microtime: '0.1828088760376' sec
Microtime: '0.18080997467041' sec
Microtime: '0.18127393722534' sec

Describe the solution you'd like Share session between diferent http request

Describe alternatives you've considered I have not an alternative, i need do complex queries.

Additional context Thanks for all

vzenix avatar Apr 05 '23 13:04 vzenix

Hello @vzenix,

Thank you for raising this request. I don't fully understand this to be honest. The HTTP API is stateless by design.

We are working on an optimization with the bolt protocol, but that is still some months away from releasing. Can you test if your speed improves if you change to the neo4j or bolt scheme in your connection URL?

Kind regards,

Ghlen

transistive avatar Apr 12 '23 06:04 transistive

I try, but not, it's not a problem of the library itself, the query have a cost of time, but, in the same session neo4j use a cache system for optimize it, and if you don't close the session and you launch again this query the time uses is more less

In other SGBD like mysql/mariadb you have methods for configure persistent session, don't close instantly the session, and then share session betwen separate thread of http requests

I dont know how work bolt, if use a ID of session for example, an static socket for the thread or other method so i don't know if it is possible to do

vzenix avatar Apr 19 '23 08:04 vzenix

@transistive what optimizations on bolt? 😁

stefanak-michal avatar Apr 19 '23 08:04 stefanak-michal

Yes, this is possible, and i was already working on it, but only on bolt. I'll just push my work on it later today. I found it to be exceptionally difficult though, and it requires some smart handling of the server states as you cannot store it with the socket.

transistive avatar Apr 19 '23 08:04 transistive

I had a personal use case where opening and closing of the sockets took over 40 percent of the time of each request.

transistive avatar Apr 19 '23 08:04 transistive

I don't understand. Persistent connection means that the server (apache) doesn't close TCP connection to database and let it use it by next request handled by php. But how you can affect from php this apache behaviour? Because the php process is not persistent. Even the bolt library is written in php, so how can you keep connection open? What am I missing?

stefanak-michal avatar Apr 19 '23 08:04 stefanak-michal

You can enable a socket to stay alive regardless of a php section: https://www.php.net/manual/en/function.pfsockopen.php

transistive avatar Apr 19 '23 08:04 transistive

So you wrote third connection class for Bolt library? ... interesting

stefanak-michal avatar Apr 19 '23 08:04 stefanak-michal

I pushed the work now. It is not even working and I don't have time to write up my analysis but you can check it out if you want: https://github.com/neo4j-php/Bolt/pull/115

transistive avatar Apr 19 '23 08:04 transistive

The key finding for me was that there is not a lot of control given to us by PHP to control the behavior of these sockets. Sometimes they might stay alive, sometimes not. On top of that their ID may or may not be unique, so it is very difficult to provide a second layer to attach the server state to.

transistive avatar Apr 19 '23 08:04 transistive

I'll take a look at it later (hopefully this week). I'm really curious. Did you tried to ask me how to deal with this problem in past?

stefanak-michal avatar Apr 19 '23 09:04 stefanak-michal