Database Replication Topologies and Replication Lag
Analyze Single-Leader, Multi-Leader, and Leaderless quorum replication models alongside replication lag anomalies and mitigation tactics.
Storing your data on a single physical server creates an immediate single point of failure and severely limits read throughput. If that machine suffers a hardware failure, your entire business goes dark.
Replication solves this by keeping a synchronized copy of the same dataset across multiple interconnected machines. However, propagating data modifications over unreliable physical networks introduces one of the most pervasive challenges in distributed systems: replication lag.
Choosing the right replication topology and managing lag anomalies is essential to ensuring high availability without corrupting user experience.
01Single-Leader (Primary-Replica) Replication
Single-leader replication is the most common topology across relational and document databases (including PostgreSQL, MySQL, and MongoDB).
In this model, one node is designated as the Primary (Leader), while all other nodes operate as Replicas (Followers).
- Writes: Clients must send all write requests (
INSERT,UPDATE,DELETE) exclusively to the primary. The primary commits the modification to its local disk and writes the change to its Write-Ahead Log (WAL). - Replication Stream: The primary streams its WAL entries to all follower replicas, which apply the changes in exact commit order.
- Reads: Clients can query either the primary or any of the read-only replicas, enabling horizontal read scaling.
Synchronous vs Asynchronous Replication
When the primary receives a write, how does it decide when to send an acknowledgment back to the client?
- Synchronous Replication: The primary waits until all replicas have confirmed writing the change to disk before acknowledging the client. This guarantees zero data loss if the primary crashes. However, write latency equals the slowest replica, and if even one replica experiences a network hiccup, all system writes block.
- Asynchronous Replication: The primary acknowledges the write immediately after committing it locally, streaming changes to replicas in the background. Write latency is exceptionally low. However, if the primary crashes before WAL entries reach replicas, uncommitted data is permanently lost during failover.
- Semi-Synchronous Replication: In practice, production systems configure semi-synchronous replication (such as MySQL semi-sync). The primary waits for exactly one replica to acknowledge the write before responding to the caller.
Loading the rest of the lesson…