Single-Leader Replication

One node takes every write and streams its changes to read-only copies. How the log is shipped, what synchronous and asynchronous replication cost, and how failover goes wrong.

Overview

Replication means keeping a copy of the same data on several machines. The copies let you survive a machine failure, serve reads from more servers, and put data closer to users. The hard part is keeping the copies the same while the data changes.

Single-leader replication is the most common answer. One node, the leader (also called the primary), accepts every write. The other nodes, the followers (replicas or standbys), only apply the changes the leader sends them, in the same order. PostgreSQL streaming replication, MySQL source-replica replication and most managed read replicas work this way.

One node decides the order

Because only the leader accepts writes, there is one order of changes and no write conflicts between nodes. Clients send writes to the leader. Reads can go to the leader or to any follower. Adding followers adds read capacity, but never write capacity: every write still lands on one machine.

Key concepts

  • Leader (primary)
  • Follower (replica, standby)
  • Replication log
  • Synchronous replication
  • Asynchronous replication
  • Log position (LSN)
  • Failover
  • Split brain
  • Fencing

Interview questions

  • Walk through failover for a PostgreSQL primary with two replicas.
  • Synchronous or asynchronous replication: how do you choose?
  • What is split brain, and how do you prevent it?
  • Your read replicas cannot keep up. What are your options?
  • Why not just use statement-based replication? It sends less data.

Sign up free to read the full topic

More DDIA topics

Start free on Cariara · Coding problems · System design studio