Hi,
We're using a single-master db cluster with one read-write node and several read-only nodes. From what I can tell, Laravel does support read and write specific connections (https://laravel.com/docs/8.x/database#read-and-write-connections) including a sticky option.
As explained in the docs, the sticky option forces all queries performed after a write-operation to use the write connection for the rest of the request cycle. However, requests only involving reading from the database will use only the read-only nodes.
Route::get("/write", function() {
// Should default to using read-only nodes
// Log connection -> read-only nodes
// Switch to write-node to perform write operation
User::create();
// Log connection -> write node
// With sticky option enabled, this query should also use the write-node
$users = User::all();
// Log connection -> write node
return $users;
});
Route::get("/read-only", function() {
// Should default to using read-only nodes
// Log connection -> read-only nodes
// This query should use the read-only nodes
$users = User::all();
// Log connection -> read-only nodes
return $users;
});
Now, this is all very nice, but how do we test it? Is it possible to examine which connection was used for each query somehow? We don't want to force the connection using ::connection("mysql:read") etc. We just want to make sure everything is working as expected.
I've added Log connection comments in the above code the simulate the ideal solution to our concern. That would enable us to inspect that the right connection is being used.
I hope someone can help us. Thanks!