Course map

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

Replication topologies and replication lag Three replication topologies. Leader-follower: all writes go to the leader, which ships its log to followers F1, F2, and F3, which serve reads and handle failover; a write confirmed at the leader at t=0ms takes time to reach a follower, so a read reaching that follower at t=20ms still returns the old value until the write applies there at t=50ms. Multi-leader: each of two leaders accepts writes, and conflicts are resolved. Leaderless: nodes read and write each other directly, using quorums. Leader–follower LEADER write t=0ms F1 F2 F3 (reads + failover) t=20ms read: old value t=50ms applied: fresh value Multi-leader LEADER LEADER (each accepts writes, conflicts resolved) Leaderless node node node node (quorum reads/writes, → 130)
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). Leaderless nodes use quorum reads/writes and anti-entropy to repair nodes that fell behind. Failure scenarios: split brain (a partition leaves two nodes each believing they're the leader) and lost writes on leader failover under asynchronous replication.
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

  1. Leader-follower: all writes go to the leader, which ships its write-ahead log (→ 68) to followers; followers apply it and may serve reads.
  2. 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).
  3. 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).
  4. Multi-leader: more than one node accepts writes; conflicting writes to the same data must be detected and resolved (→ 134, taught later).
  5. 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.

Shared lab setup

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.

Source directory

Search the guide