How to Implement Laravel Read/Write Splitting with Nginx Load Balancing

This guide explains how to configure Laravel for read/write database separation and set up Nginx load balancing, covering hardware requirements, Laravel config/database.php adjustments, Nginx upstream strategies, detailed server blocks, and essential parameter explanations for a scalable web architecture.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
How to Implement Laravel Read/Write Splitting with Nginx Load Balancing

Introduction

As internet applications proliferate, massive data storage and access become bottlenecks. Large‑scale services handling millions to billions of page views per day put extreme load on databases, threatening system stability and scalability. Load balancing and read/write separation are essential to address these challenges.

Hardware Setup

The example uses five servers: three dedicated to load balancing, one for data backup, and one for emergency handling. Each server has 8 GB RAM, 40 GB disk, and a 4‑core CPU.

Laravel Read/Write Separation

Laravel 6 can achieve read/write splitting by editing config/database.php. The configuration defines separate hosts for read and write operations. After the change, SELECT queries are routed to the read host(s) while INSERT/UPDATE/DELETE statements go to the write host.

'read' => [
    'host' => ['192.168.72.100','196.168.72.101','196.168.72.102'],
],
'write' => [
    'host' => ['196.168.72.99'],
    'sticky' => true,
    'driver' => 'mysql',
    'database' => 'database',
    'username' => 'root',
    'password' => '',
    'charset' => 'utf8mb4',
    'collation' => 'utf8mb4_unicode_ci',
    'prefix' => '',
],

Nginx Load Balancing Strategies

Round Robin (default) : Distributes requests evenly across servers.

Least Connections : Sends a request to the server with the fewest active connections.

IP Hash : Binds a client’s IP to a specific server, ensuring subsequent requests from the same IP go to the same server.

Detailed Nginx Configuration

The upstream block defines the backend servers, their weights, fail limits, and special flags such as backup and down. The server block sets the listening port, server name, access log, and a location block that proxies traffic to the upstream group.

upstream server1 {
    server 192.168.72.100:80 weight=3 max_fails=3 fail_timeout=15 max_conns=1000;
    server 192.168.72.101:80 weight=2 max_fails=3 fail_timeout=15;
    server 192.168.72.102:80 weight=1 max_fails=3 fail_timeout=15;
    server 192.168.72.103:80 backup;
    server 192.168.72.104:80 down;
}

server {
    listen 80;
    server_name www.xxx.com;
    access_log logs/host.access.log main;
    location / {
        proxy_pass http://server1;
        index index.html index.htm index.php;
    }
}

Parameter Explanations

weight : Higher values give the server a larger share of traffic.

max_fails : Number of failed attempts before the server is considered unavailable.

fail_timeout : Time period to wait after reaching max_fails before retrying the server.

backup : Server is used only when all non‑backup servers are down or busy.

down : Server is temporarily excluded from load balancing.

max_conns : Maximum simultaneous connections allowed.

Conclusion

By applying the Laravel read/write configuration together with the Nginx load‑balancing setup, a web service can efficiently distribute read traffic, isolate write operations, and achieve high availability. For full data consistency, the underlying MySQL cluster should be configured with master‑slave or master‑master replication.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Backend Developmentload balancingread/write splittingNGINXLaravel
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

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.