database-replicationDatabase Replication — Leader-Follower & Read Scaling
Most real-world applications are 95% Read-Heavy. Scaling a database requires decoupling writes from reads using a single Primary Leader and multiple Read Replicas.
Primary-Replica Architecture
Understand how data replication works, why read replicas scale query throughput, and the challenges of replication lag.
1. The Core Architecture: Primary (Leader) + Replicas (Followers)
When millions of users read profiles, posts, or product catalogs simultaneously, a single database server will exhaust its CPU and disk I/O.
The solution is Leader-Follower (Master-Slave) Replication:
2. Visualizing the Replication Data Flow
Watch the live query routing below: Write traffic (Orange) goes exclusively to the Primary DB, which asynchronously streams its binlog sync (Green) to Replicas, while Read traffic (Blue) is distributed across Replicas.
3. Synchronous vs Asynchronous Replication & Replication Lag
How quickly should changes from the Primary reach the Replicas? This introduces a classic distributed systems trade-off between latency and consistency.
Risk — Replication Lag: Replicas may be milliseconds behind. If a user updates their profile and instantly reloads the page, a replica might serve stale data!
Risk — Latency & Availability: If any replica is slow or has a network hiccup, all write queries freeze!
4. Failover & Disaster Recovery: What If a Server Dies?
High availability means your website never crashes even during hardware failure or power loss. Here is how both Primary and Replica failures are handled:
• Multiple Replicas Available: The connection pool instantly detects the dead replica and redistributes read queries to the surviving healthy replicas. A new replacement replica is spun up in the background.
• Only 1 Replica Existed: Read queries temporarily fall back directly to the Primary DB so users experience zero downtime while a replacement replica provisions and syncs.
Consensus monitoring detects the Primary is offline. The system promotes the most up-to-date Read Replica to become the New Primary Leader.
All app servers are immediately notified to route writes to the new Leader, and a new follower replica is spawned to restore redundancy.
If a network partition cuts communication between data centers, the old Primary might still think it's the Leader while the other side promotes a new one. To prevent two conflicting leaders from corrupting data, servers use Quorum Consensus:
• Isolated side with 1 server: Demoted automatically (refuses writes).
• Connected side with 2 servers: Continues as the valid Leader.
An odd number can never produce a 50/50 tie, guaranteeing that exactly ONE side can hold the majority vote.
5. Putting It All Together: The End-to-End Request Journey
Let's trace how everything we've learned (DNS → Load Balancer → Stateless Web Tier → Replicated Databases) works together in production:
The app server doesn't pick replicas manually. In production, one of these three patterns handles it automatically:
SELECT queries across all active replicas internally.api.example.com. DNS returns the public IP address of the Load Balancer.INSERT/UPDATE), the web server routes the query to the Primary Database (Master).SELECT), the web server routes the query to any available Read Replica (Slave).6. Interactive Replication Scenario Quiz
Test your architecture knowledge with real-world scenarios:
7. System Design Interview Comparison Matrix
Replication strategies quick reference table:
| Pattern | Synchronous Replication | Asynchronous Replication |
|---|---|---|
| Write Latency | Higher (Waits for replica acknowledgment) | Lowest (Primary responds immediately) |
| Data Consistency | Strong (Zero data loss on leader crash) | Eventual (Small replication lag window) |
| System Availability | Lower (Slow replica halts write traffic) | High (Primary operates independently) |
| Best Used For | Financial, Payments, Critical Inventory | Social Media, Feeds, Analytics, Catalogs |
Build Your Replication Architecture
Design a high-availability Primary-Replica cluster that separates writes from reads.
Task: Primary-Replica Database Cluster
Drag components to arrange them freely, and click two nodes to connect them.