Leader-Follower Replication

How one leader orders every write and followers copy its log: what the log carries, how followers join and lag, how a failed leader is detected and replaced, and how fencing stops two leaders at once.

Overview

In leader-follower replication (also called primary-replica), one node, the leader, accepts every write. It records each change in a replication log, in the order it committed them, and sends the log to the other nodes, the followers, which apply the same changes in the same order. Clients may read from followers, but only the leader takes writes.

It is the default design for relational databases and many others: PostgreSQL, MySQL, MongoDB replica sets and Redis all work this way. It is popular because one writer makes ordering trivial: there are never two conflicting versions of a row to reconcile.

The hard parts come from time and failure. Followers are always a little behind, so a read from one can return old data. And when the leader dies, the system must notice, pick a follower that has every committed write, point clients at it, and make sure the old leader cannot keep accepting writes if it wakes up. Interviewers probe exactly these points.

What you will be able to do

  • Explain what a replication log carries and how a new follower catches up.
  • Name the anomalies replication lag causes and route reads to avoid them.
  • Walk through failover: detection, election and reconfiguration, with realistic timings.
  • Explain split brain and prevent it with majority election, terms and fencing tokens.
  • Compare how PostgreSQL, MySQL, MongoDB and Redis do it.

Key concepts

  • Leader and followers
  • Replication log
  • Statement, physical and logical replication
  • Snapshots and catch-up
  • Replication lag
  • Read-your-writes
  • Failure detection
  • Leader election and terms
  • Split brain and fencing
  • Leader leases

Interview questions

  • Walk me through what happens when the leader of a replicated database fails.
  • What is split brain, and how do you prevent it?
  • A user updates their profile and the page still shows the old value. Why, and how do you fix it?
  • Why do consensus groups use 3 or 5 nodes, and not 2 or 4?
  • How does Raft elect a leader, and why can it not lose committed data?
  • Synchronous or asynchronous followers: which would you use for a payments database?
  • What is a fencing token, and why is a lease alone not enough?
  • How do PostgreSQL, MySQL, MongoDB and Redis handle leader-follower replication?

Sign up free to read the full topic

More System Design topics

Start free on Cariara · Coding problems · System design studio