How Octane handles DB connections? #42108
-
Hello everybody. I love Laravel and Octane projects. I have a question about DB handling in Octane. I'm an old Java developer. In java we can connect to DB with two methods. One is create new connection for every operation (this is worst becouse if we receive high load then Mysql is becoming ridiculous). The second method is creating a connection pool. For example I'm creating 10 connections and keep them alive. When I want to execute an SQL query then I'm getting an available connection from pool, execute the SQL command and release the connection to the pool. This is best option. I can automatically scale (increase/decrease) connection count by the high load. I want to learn how Octane handles the DB connections. Is it creating new connection for every DB operation or is there a connection pool? Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 8 replies
-
No one knows this? |
Beta Was this translation helpful? Give feedback.
-
Bump. Don't let this topic sink into oblivion. |
Beta Was this translation helpful? Give feedback.
-
On regular Laravel installation connection is open on first database request and remain open all process life (request, command, job, schedule...). With Octane, you can configure how to handle connections from config listeners https://github.com/laravel/octane/blob/1.x/config/octane.php By default every request call to |
Beta Was this translation helpful? Give feedback.
-
Hi everyone. I think this video from @themsaid answers the question nicely: https://www.youtube.com/watch?v=Zf6o7ag5WPI TL;DR "Octane opens a db connection when a worker starts and that same connection will be used to handle all incoming requests to that worker. This saves server resources because opening db connections is quite expensive." |
Beta Was this translation helpful? Give feedback.
-
It's still has an issue for now or has solved ? |
Beta Was this translation helpful? Give feedback.
-
If someone is using Read & Write Connections + Laravel Octane and wants it to work similar to the nginx + php-fpm approach, I can tell you how I did it. If you look at the source, it picks a random ip from a list and makes a connection, this works in the old approach, but when we connect and don't reset the connection, this approach doesn't work anymore. Let's say we have:
We also know that if we make different queries by selecting different connection name ( Actually, we can duplicate the configs 3 times, where each will be 1 IP in Create middleware that for every next query, will take the next connection from our list and set it via Done, the only thing left to do is "tweak" it, to share "write" instance connection for all connections (reduce the number of connections to the database), to handle exception if connection for any of the replicas is not available (select the next one, according to round-robin principle) so that we don't go into infinite loop if all replicas are unavailable, reset state, etc. Result:
Maybe as a temporary solution, but at least you won't have all requests going to one of the replicas (if the first IP was randomly selected in each worker) and you don't have to reconnect all the time if you drop the connection. Make this middleware + duplicate the configs, you don't need to edit anything else, the whole application will use the default connection to the database. |
Beta Was this translation helpful? Give feedback.
-
We ran into a problem recently that a database transaction that was started when processing an http request and wasn't committed or rolled back (due to a bug in the code) continued to exist and be used by the worker for new requests causing a weird inconsistent behaviour, as that worker was essentially using its own data snapshot isolated from the rest of the db by an open transaction. |
Beta Was this translation helpful? Give feedback.
Hi everyone. I think this video from @themsaid answers the question nicely: https://www.youtube.com/watch?v=Zf6o7ag5WPI
TL;DR "Octane opens a db connection when a worker starts and that same connection will be used to handle all incoming requests to that worker. This saves server resources because opening db connections is quite expensive."