Laravel Advanced & Architecture
5-8 Years (Senior)
Hard
Production Scenario
How do you handle Dynamic Database Connection Routing (Read/Write Splitting) and Replica Lag in Laravel?
Real-World Scenario / Incident Description
After a user updated their profile, they were redirected back to the profile page and saw their old data because the read query hit a MySQL replica with 400ms replication lag.
448 views
•
Updated 1 month ago
Sticky Connections in Laravel config/database.php
Enable the sticky option:
'mysql' => [
'read' => [
'host' => ['192.168.1.10', '192.168.1.11'],
],
'write' => [
'host' => ['192.168.1.5'],
],
'sticky' => true, // Reads in the same request/lifecycle immediately route to Write DB
],
Force Primary Read on Critical Reads:
$user = User::onWriteConnection()->find($id);
Related Concepts:
#Read Write Splitting
#Replication Lag
#Database
#High Availability
More Laravel Advanced & Architecture Interview Questions
How does Laravel Octane handle state persistence across requests, and how do you prevent catastrophic memory leaks and data bleeding in singletons?
Read →
How do you prevent Deadlocks when processing concurrent financial transactions or inventory deductions across multiple queue workers in Laravel?
Read →
How does the Laravel Service Container resolve dependencies using Reflection, and what are the performance trade-offs in large applications?
Read →
How do you execute Zero-Downtime database migrations on tables with 50+ million rows without locking writes in production Laravel?
Read →
How do you architect Multi-Tenant database isolation in Laravel (Single DB vs Multi DB vs Schema Separation)?
Read →