BytewizeJoin the waitlist

Leader-follower replication

One machine takes the writes and the others copy them: what that buys, and what it does not. Builds on Transactions and the write-ahead log.

5 minute lesson

One writes and the others copy

The most common way to keep copies of a database: one replica is the leader and takes every write. The others are followers. They receive the leader's changes and apply them in the same order. Reads can go to any of them.

Key terms

leader
The one replica that accepts writes and passes them on to the others. Also called the primary.
follower
A replica that receives the leader's writes and can answer reads. Also called a standby or a read replica.
replica
One of several copies of the same data, each kept on a different machine.

The way of a change

  1. Write

    The application sends an update to the leader. The leader writes it to its log and applies it.

    • Applicationupdate
    • Leaderprice 12
  2. Ship

    The leader sends its log to every follower, as a continuous stream. It is the same log that protects the leader against a crash.

    • Leadersends the log
    • Followersreceive it
  3. Replay

    Each follower replays the changes in order. A moment later it holds the same data as the leader. Because the order is the same, the result is the same.

    • Leaderprice 12
    • Followersprice 12
Check yourself

A database runs with one leader and three followers. An application sends an UPDATE to one of the followers.

What happens?

  • It is refused, as only the leader takes writes
  • It is applied there and sent on to the leader
  • It is applied there and stays on that follower
  • It is applied on all four machines at once
Show the answer
  1. It is refused, as only the leader takes writes

A follower is read-only. If it accepted writes of its own, its data would differ from the leader's, and nothing would bring the two together again. All changes enter at one point, the leader, and flow outwards in one order. That single order is what keeps the copies identical.

In leader-follower replication writes go to the leader only. Followers are read-only.

What followers are good for

Check yourself

A leader is at its limit: 95% of its work is writes. The team adds three followers.

What does that achieve?

  • Little: every write still goes through the leader
  • A lot: the writes are now spread over four machines
  • A lot: each follower takes over a quarter of the writes
  • Nothing at all: followers cannot serve any request
Show the answer
  1. Little: every write still goes through the leader

Followers take reads off the leader, and here reads are only 5% of its work. The writes stay where they were, and the leader now also has to stream its log to three more machines. A write-heavy load needs a different answer: fewer or cheaper writes, a bigger machine, or splitting the data across several leaders.

Read replicas help read-heavy loads. They do nothing for a leader that is busy writing.

Recall

How does leader-follower replication work, and which kind of load does it scale?

Show the answer

One replica, the leader, accepts all writes and records them in its log. It streams the log to the followers, which replay the changes in the same order and so arrive at the same data. Reads can be spread over all replicas, so reads scale with the number of followers. Writes do not: they all go through the leader.

Recap

  • The leader takes every write and streams its log. Followers replay it in order.
  • Followers add read capacity, a spare for failover and a place for heavy queries.
  • They add no write capacity: one leader handles every write.

Start with one byte

Join the waitlist and get the beta invite when it opens.

  1. Bit1U
  2. Nibble2U
  3. Byte4U
  4. Kilobyte8U
One unit more at every level