Get "PHP 8 in a Nuthshell" (Now with PHP 8.4)
Amit Merchant

Amit Merchant

A blog on PHP, JavaScript, and more

Separating Database Hosts to Optimize Read and Write Operations in Laravel

When it comes to database performance, it would be primarily judged by the fact that how well the database can handle read and write operations.

The read and write dynamics

In high-traffic websites, for instance, e-commerce or social media websites, where a lot of read and write operations are going on simultaneously, the database would be the bottleneck.

For instance, in MySQL, read performance can be affected when write operations are performed concurrently since write operations may acquire locks that can delay read operations, especially if the database is heavily utilized and the number of concurrent operations is high.

On top of that, the disk I/O is shared between read and write operations in most cases. High volumes of writes can lead to I/O contention, slowing down read operations.

The master and slave approach

To fix this, one way is to use separate database hosts for read and write operations. This can be achieved by creating master and slave instances of the database and using replication to synchronize the data between the two databases.

The master would be used to perform the write operations, while the slave would be used for read operations.

Master and Slave Database Hosts

Laravel making it easy

Once this is set up, it’s time to use them in the application. Laravel makes it particularly easy to manage it all.

All you need is to configure the read and write hosts in the config/database.php file like so.

'mysql' => [
    'read' => [
        'host' => [
            '192.168.1.1',
            '196.168.1.2',
        ],
    ],
    'write' => [
        'host' => [
            '196.168.1.3',
        ],
    ],
    'sticky' => true,
 
    // code commented for brevity
],

As you can tell, Laravel will automatically detect the read and write operations and use the hosts accordingly.

Note: Both the read and write hosts would be using the other options such as port, database, username, password, and charset from the mysql configuration. So, it will be shared among all the hosts.

An optional sticky option can be set to true to allow the immediate reading of records that have been written to the database during the current request cycle. Meaning, that if you have a record that has been written to the database, you can immediately read it from the write database host in the same request cycle. It “sticks” to the write host.

In closing

This is how you can use separate database hosts to optimize read and write operations in Laravel. Based on the requirements of your application, you can tweak the configuration, like number of hosts or stickiness, accordingly and it will surely improve the performance of your application.

Learn the fundamentals of PHP 8 (including 8.1, 8.2, and 8.3), the latest version of PHP, and how to use it today with my new book PHP 8 in a Nutshell. It's a no-fluff and easy-to-digest guide to the latest features and nitty-gritty details of PHP 8. So, if you're looking for a quick and easy way to PHP 8, this is the book for you.

Like this article?

Buy me a coffee

👋 Hi there! I'm Amit. I write articles about all things web development. You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.

Comments?