abbood
abbood

Reputation: 23548

Does laravel automatically failover Database connections?

Background

We use Laravel to serve an enterprise application. We specifically separate the analytical queries from the operational one like so:

        'pgsql' => [
            'driver'   => 'pgsql',
            'write'    => [
                'host'     => env('DB_HOST', 'localhost'),
            ],
            'read'    => [
                'host'     => env('DB_HOST_READ', 'localhost'),
            ],
            ..
        ],
        'pgsql-read' => [
            'host'     => env('DB_HOST_READ', 'localhost'),
            ..
        ],
        'pgsql-analytical' => [
            'host'     => env('DB_HOST_ANALYTICAL', env('DB_HOST_READ' , 'localhost')),
            ..
        ]

In the queries where we hit the analytical database, we specify it like so:

    $query = Store::on('pgsql-analytical')->leftJoin(// etc

or like so in the files where all the queries should hit the Analytical DB

 \Config::set('database.default', 'pgsql-analytical');

Problem

We were investigating a sudden spike in our Write DB Database connections, and we found a pattern that when our Analytical D CPU Util has maxed out, suddenly there is a spike in Write DB connections:

enter image description here

Question

Is there a mechanism in Laravel that automatically fails over from read DB connections to the Write one?

This ticket suggests that there is failover amongst slave replicas:

if we have more than one database slave (read hosts) and our application fails to connect to one of them, we want our application to try the next one instead of simply failing.

But does Laravel actually fail over from the read replicas to the master DB?

Upvotes: 2

Views: 707

Answers (0)

Related Questions