Lesson 3 of 17
14. Replication

14.2 How does leader-follower replication work?

3 min read+10 XP

At the bank: Only head office writes notices. It numbers them in order (notice 501, 502, 503...) and sends them to every branch. Branches never write their own; they file each copy in order, so every branch ends up with the same set.

In the system: In leader-follower replication (also called primary-replica, or historically master-slave):

writes go to the leader; it streams an ordered log of changes to read-only followers.

Figure: one place takes writes; followers apply the same changes in the same order.

  1. All writes go to the leader.
  2. The leader records each change in its replication log (in PostgreSQL, the write-ahead log; in MySQL, the binary log) and streams it to followers.
  3. Each follower applies the changes in the same order, so it ends up with the same data, a little later.
  4. Reads can go to the leader or to any follower.

Why one leader: with a single place deciding the order of writes, there are no conflicts. Two writes to the same row are simply applied one after the other, in the leader's order. It's the default setup for PostgreSQL, MySQL, SQL Server, MongoDB replica sets and many others.

Memory code: "One office writes, every branch files the copies in order."