Senior LevelLaravel
How does Laravel handle Read/Write Database Connection Splitting with sticky connections?
❓ Interview Prompt: Explain read/write database connection splitting, replica connections, sticky option, and preventing replication lag race conditions.
Laravel allows configuring separate database hosts for read (SELECT) and write (INSERT/UPDATE/DELETE) queries to scale database workloads across read replicas:
Replication Lag & The 'sticky' Option:
If a user creates a record (Write to Primary DB) and is immediately redirected to an index page (Read from Replica DB), replication lag may cause the new record to appear missing!
•
'"sticky" => true': Instructs Laravel to read from the Primary DB for the remainder of the current session after a write occurs!
Code Implementation Example
php
<?php
// In config/database.php
'mysql' => [
'read' => ['host' => ['192.168.1.101', '192.168.1.102']],
'write' => ['host' => ['192.168.1.100']],
'sticky' => true, // Prevents replication lag issues
'driver' => 'mysql',
];💡Configuring read/write database splitting with sticky connection flag.
🎯 Interview Key Takeaway
Configure read/write database splitting to scale reads across replicas, and enable sticky=true to eliminate replication lag race conditions.
#Read/Write Splitting#Database Replicas#Sticky Connections#Senior
Back to all Laravel Questions→