Friday, March 10, 2017

Laravel read and write connection not in sync

I'm using read and write MySQL connection setup in my Laravel 5.2 app:

'mysql' => [
    'host' => env('DB_HOST_WRITE', 'localhost'),
    'driver' => 'mysql',
    'port' => env('DB_PORT', '3306'),
    'database' => env('DB_DATABASE', 'forge'),
    'username' => env('DB_USERNAME', 'forge'),
    'password' => env('DB_PASSWORD', ''),
    'charset' => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix' => '',
    'strict' => false,
    'engine' => null
]

The website runs on multiple servers, using a load balancer.

Now, in the app, there are cases of read and write operations done one after another, e.g.

  1. insert new records into the database
  2. select some of the newly inserted records

With the current connection setup, it can happen that the select will return nothing, even if the records were inserted correctly.

What can be the reasons for this?



via lesssugar

Advertisement