Notesreplication🌱 seedling
Leader-Based Replication
Also: Single-Leader Replication · Primary-Replica Replication · Master-Slave Replication · Active/Passive Replication
One replica, the leader, accepts every write and streams its [[Replication Log]] to followers, which apply it in the same order. Reads can go anywhere; writes go only to the leader.
Why it exists
Copies of data on several machines give availability, read throughput and locality. The hard part is changes: every write must reach every copy in the same order. Funnelling all writes through one node gives a single history with no write conflicts to resolve.
How it works
- Clients send writes to the leader, which applies them locally and appends them to its log.
- Followers stream the log and replay it in order. Each follower is simply a position in the leader's log.
- Followers may be synchronous or asynchronous: see Synchronous vs Asynchronous Replication.
- Adding a follower: take a consistent snapshot tied to a log position, copy it over, then stream every change after that position. This only works if the leader still retains the log from that point.
- When the leader dies, a follower is promoted: Failover.
Trade-offs
| Gains | Costs |
|---|---|
| one order of writes, no conflicts | all writes go through one node (and often one region) |
| read scaling by adding followers | asynchronous followers serve stale data (Replication Lag) |
| simple mental model | leader failure needs failover, with risks of lost writes and Split Brain |
In practice
- PostgreSQL streams its write-ahead log to standbys; MySQL replicates its binary log; Kafka replicates each partition from a leader to followers in an in-sync replica set.
- Most databases leave the decision to fail over to an external tool (Patroni, Orchestrator) or a consensus layer.
Pitfalls
- Read replicas behind a load balancer without session guarantees: users see their own edits vanish. See Read-Your-Writes Consistency.
- Assuming a write is durable because "it's replicated" while replication is asynchronous.
Connections
- Part of
- Replication
- Contains
- FailoverReplication Log
- Learn first
- Redundancy
- Builds on this
- ConsensusConsistency ModelTotal Order Broadcast
- Contrasts with
- Multi-Leader ReplicationLeaderless Replication
- In real systems
- PostgreSQLMySQLApache KafkaDynamoDBMongoDB
Recall
These cards are also in your review deck, scheduled with spaced repetition.