Topic 072 / Core
Replication: leader–follower, multi-leader, leaderless, replication lag
Chapter 5.5 · Distributing Data
What and why
One database node is a single point of failure and a read-throughput ceiling. Replication copies the same data onto multiple nodes, so a failure doesn't lose it and reads can be spread across copies — at the cost of those copies momentarily disagreeing. Builds on: → 68 (write-ahead logging and durability).
Diagram
Text version of this diagram
Leader-follower: Multi-leader: Leaderless:
LEADER LEADER ◄──► LEADER node ◄──► node
/ | \ (each accepts writes, ◄──► ◄──►
F1 F2 F3 conflicts resolved) node ◄──► node
(reads + failover) (quorum reads/writes, → 130)
How it works
- Leader-follower: all writes go to the leader, which ships its write-ahead log (→ 68) to followers; followers apply it and may serve reads.
- Synchronous replication waits for a follower's acknowledgment before confirming the write (safer, slower, unavailable if that follower is down); asynchronous confirms immediately (faster, risks losing the last few writes if the leader fails before shipping them).
- Replication lag: a follower is always some time behind the leader; a client reading its own just-written data from a lagging follower can see stale data unless the system provides read-your-own-writes (→ 129).
- Multi-leader: more than one node accepts writes; conflicting writes to the same data must be detected and resolved (→ 134, taught later).
- Leaderless: any node accepts a write; reads and writes use quorums (→ 130) to stay consistent, and anti-entropy (→ 131) repairs nodes that fell behind.
Practical example
(illustrative). A leader confirms a write at t=0ms under asynchronous replication; an async follower applies it at t=50ms. A read reaching that follower at t=20ms still returns the old value — the replication-lag window, not a bug.
When to use it
- Use when: read throughput or availability needs exceed one node — leader-follower for most read-heavy OLTP; leaderless when write availability during a partition matters more than instant consistency.
- Avoid when: multi-leader, unless multi-region or offline writes genuinely require it — conflict resolution is real, ongoing complexity.
- Failure scenarios: split brain — a network partition leaves two nodes each believing they're the leader, both accepting writes; lost writes on leader failover under asynchronous replication, when the last unacknowledged writes vanish with the failed leader.
- Simpler alternative: a single instance with backups (→ 80), until read throughput or availability targets actually require replicas.
Practice
(15–20 min, lab Postgres — single node, reason about the scenario).
Describe, on paper, what a SELECT immediately following an INSERT would return if it
were routed to an asynchronous follower instead of the leader, given a stated lag. Then
design the routing rule a "read-your-own-writes" session would need (e.g., route that
session's reads to the leader for N seconds after it writes). Done when the stale-read
scenario is written down and your routing rule closes it.
Key takeaway
Replication trades a single point of failure for a bounded window of disagreement between copies — name that window, don't ignore it. Done when you can: explain split brain and lost writes as the two concrete risks of leader failover. Used next in → 73; consistency guarantees → 129; quorums → 130.
References
Kleppmann and Riccomini, Designing Data-Intensive Applications — see
references.md.