DEV Community

Dimitrios Desyllas
Dimitrios Desyllas

Posted on

Run specific select raw queries on write database in order to have fresh data on replication lag

How I can arbitaty execute some raw sql queries on write host. I do need it in order to have the most fresh data. It won't be on all the queries but on handfull critical ones.

I use my Laravel 5.7 application a master-slave database replication scheme using postgresql:

'mydb' => [
    'driver' => 'pgsql',

    'read'  => [
        'host' => env('DB_HOST_READ', '192.168.2.3'),
    ],
    'write' => [
        'host' => env('DB_HOST_WRITE', '192.168.2.5'),
    ],
    'port'     => env('DB_PORT', '5432'),
    'database' => env('DB_DATABASE', 'mydb'),
    'username' => env('DB_USERNAME', ''),
     'password' => env('DB_PASSWORD',

DO you have any idea?

Top comments (1)

Collapse
 
brandinchiu profile image
Brandin Chiu

This is precisely the kind of thing I would defer to the datastore to figure out for you.

There are a number of off-the-shelf products that exist that will properly direct your incoming traffic to where it needs to go based on some subset of conditions.

Moving this focus to that database proxy instead of handling it in your application is probably going to save you a lot of headaches, and makes your application less tightly coupled to your architecture.