Introduction

If you have a similar setup to us, you might have two databases in different regions to avoid read latency in each region.

Having that master-slave relation works perfectly, and will make your write operations fast in both regions, but writing in the slave from its own region is slow, giving it is actually writing in master (cross-region).

Reading in the slave region right after a write to the master region might return a miss, since there is a replication lag that you also have to be aware.

Setup

Let’s assume we have two Regions: A and B.

To have a bit of context, this setup is made by using Laravel’s database configuration.

Our default database connection has different read / write hosts. The magic occurs on the .env file for Region A, or for Region B.

'default' => [

'read' => ['host' => env('DB_HOST_READ')],

'write' => ['host' => env('DB_HOST_WRITE')],

'database' => env('DB_MASTER'),

...

So for Region A .env we have:

# We read and write from the same database

DB_HOST_READ=db.region-A

DB_HOST_WRITE=db.region-A

And for Region B .env we have:

DB_HOST_READ=db.region-B # We read in B

DB_HOST_WRITE=db.region-A # We write in A

Problem

We are now going to focus on Region B requests.