Databases 4 min read

Laravel Read/Write Database Separation Configuration Guide

This article explains how to set up read/write database separation in Laravel by configuring multiple database connections in the config/database.php file, covering both identical and distinct credentials for read and write servers, and describing the purpose of the sticky option to ensure data consistency during a request.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
Laravel Read/Write Database Separation Configuration Guide

In high‑concurrency scenarios a single database server can become a bottleneck and a single point of failure; separating read and write operations onto different servers mitigates this risk.

Read/write separation basics : SELECT statements are treated as read operations, while INSERT, UPDATE and DELETE statements are treated as write operations. Laravel supports read/write splitting through raw SQL, the query builder, or Eloquent ORM.

Configuration when read and write servers share the same credentials can be done in config/database.php as follows:

<?php
'mysql' => [
    'read' => [
        'host' => [
            '192.168.100.101',
            '196.168.100.102',
        ],
    ],
    'write' => [
        'host' => [
            '196.168.100.103',
        ],
    ],
    'sticky' => true,
    'driver' => 'mysql',
    'database' => 'your_database',
    'username' => 'your_username',
    'password' => 'your_password',
    'charset' => 'utf8mb4',
    'collation' => 'utf8mb4_unicode_ci',
    'prefix' => '',
];

The read key defines the hosts used for read‑only queries, while the write key defines the hosts used for write queries. The sticky option, when set to true , forces all subsequent reads in the same request to use the write connection after a write has occurred, ensuring immediate visibility of newly written data.

Configuration when read and write servers have different credentials requires specifying the username and password for each connection individually:

<?php
'mysql' => [
    'read' => [
        [
            'host' => '192.168.100.101',
            'username' => 'your_username',
            'password' => 'your_password',
        ],
    ],
    'write' => [
        [
            'host' => '192.168.100.103',
            'username' => 'your_username',
            'password' => 'your_password',
        ],
    ],
    'sticky' => true,
    'driver' => 'mysql',
    'database' => 'your_database',
    'charset' => 'utf8mb4',
    'collation' => 'utf8mb4_unicode_ci',
    'prefix' => '',
];

When the database accounts and passwords differ across servers, each server’s credentials must be configured separately as shown above.

Laravel makes read/write separation straightforward, but the exact configuration depends on the underlying database architecture. Future articles will demonstrate a three‑server setup that aligns with the concepts presented here.

BackendRead-Write SplittingPHPLaravelDatabase Configuration
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.