Leader-Based Replication

Single-leader (primary-replica) replication, synchronous vs asynchronous followers, failover, and replication lag — the default pattern behind Postgres, MySQL, Kafka, and many services.

3/5Overview: 25m

Leader-based replication

One leader (primary) accepts all writes and streams changes to followers (replicas). Clients may read from followers to scale reads — at the cost of staleness.

Client write → Leader → replicate log → Followers apply Client read → Follower (possibly stale) OR Leader (fresh)

Sync vs async replication

ModeDurabilityLatencyFailover risk
SyncWrite ack only after follower(s) persistHigherLower data loss
AsyncLeader acks after local writeLowerLost writes if leader dies before replicate

Semi-sync — wait for one follower, async to the rest — common compromise.

Replication lag

Followers fall behind under load. Symptoms:

  • User posts content, refresh shows old feed (read from stale replica)
  • Unique constraint appears to "flicker" across replicas
  • Read-your-writes violated unless you route reads to leader or track position

Fixes: sticky sessions, monotonic reads via version tokens, or read from leader for critical paths.

Failover

When leader dies, promote a follower. Hard parts:

  • Split brain — two nodes think they're leader
  • Lost writes — async lag at crash time
  • Fencing — stale leader must be prevented from accepting writes (Topic 6)

Tools: etcd, ZooKeeper, Raft-based systems elect the new leader with consensus.

Not covered here

Single-node Postgres transaction isolation — see Databases roadmap. This topic is multi-copy semantics only.

Further Reading

Hands-On Tasks (Optional)

Low-setup exercises — browser visualizers, paper drills, or optional Docker. No autograding; the goal is interview fluency.

  • Reason through a failover with lag

    Leader accepts write, async replica is 10s behind, leader crashes. What can be lost? What does "fencing" or "wait for N replicas" change? Write 5 sentences — no cluster required.

    15m