Lesson 01

Leaders, Followers and Everyone

Building block

A copy of the data on a second machine buys three things: the data survives the loss of a machine, more machines can answer reads, and a copy can sit near the people reading it. Every replication scheme then answers two questions, and an interview asks both: who may accept a write, and when is a write done.

One leader, many leaders or no leader answers the first question. Synchronous or asynchronous answers the second. The failure a design survives follows from those two answers, so you name them before you draw a second database on the board.

Two Questions per Scheme

The number of copies is the figure people quote, and it is the wrong place to start. Three copies with asynchronous followers can lose a write that two copies with a synchronous follower would keep. What a design survives is set by who takes writes and when a write counts; the number of copies only says how many failures that answer can absorb.

This chapter stays at the level of those two questions. How one engine ships its log, waits for a standby and fails over is the subject of PostgreSQL Deep Dive's replication chapter, and this lesson does not repeat it.

Three answers to who takes a write
One leaderfollowers apply its log
Takes a write: one node.
Counts when: the leader has it, plus one follower if that follower is synchronous.
Survives: a lost follower; a lost leader after a failover.
Costs: one node's write capacity, and the failover.
Many leaderseach region or device
Takes a write: the local leader.
Counts when: the local leader has it; the others hear later.
Survives: a region cut off from the rest, which keeps writing.
Costs: conflicting writes, and no unique rule across leaders.
No leaderwrites counted on replicas
Takes a write: several replicas at once.
Counts when: enough replicas have confirmed it.
Survives: a lost node, with no failover at all.
Costs: replicas drift and need repair; a read must find the newest copy.

One Leader

With one leader, every write goes to one node. The leader appends it to its log and ships the log to followers, which apply the entries in the same order. That single order is what the scheme buys. Two writes to one record cannot conflict, because one of them always came first, and a rule such as one account per username holds because one node checks it.

It costs two things. Write capacity is one node's, however many followers you add. And when the leader dies, nothing takes writes until a follower replaces it. Followers that serve reads, and the lag a reader sees on them, are Chapter 2's lesson on read replicas.

Failover has four steps, and an interviewer may ask about any of them. Something notices the leader is gone. A follower is chosen to lead, which is Chapter 5's lesson on election. The old leader is stopped from writing if it comes back, which is fencing, also in Chapter 5. Then the clients move to the new leader.

When a Write Is Done

With synchronous replication, the leader answers the client only after a follower has the write. A failover to that follower loses nothing that was acknowledged. In exchange, every write pays a round trip to the follower, and while that follower is down, writes stall.

With asynchronous replication, the leader answers at once and ships the write afterwards. Writes are as fast as the leader alone. A failover can lose every write the follower had not yet received, and the client was told that each of them succeeded.

The write an asynchronous failover loses
Write confirmedby the leader alone
→
Leader diesbefore shipping it
→
Follower promotedit never received it
→
Write goneno copy has it

The usual middle is one synchronous follower and the rest asynchronous. An acknowledged write is on two machines, and only one follower's round trip sits on the write path.

Where that synchronous follower sits sets the price. In another zone of the same region, it adds 1 to 2 milliseconds to each write. In western Europe, for a leader in US east, it adds about 85 milliseconds to every write. So keep the synchronous copy in another zone while losing a zone is the failure to survive. Pay the cross-region round trip on every write only once the requirement says that losing a region must lose nothing.

The layout that follows from this has three copies. The client writes to the leader, the leader waits for the follower in the next zone, and a third copy in another region receives the log asynchronously, with its 85 milliseconds never on the write path.

A synchronous follower in the next zone, an asynchronous one in another region
US east Clientwrites LeaderUS east, zone A Sync followerUS east, zone B Async followerwestern Europe write waits 1-2 ms 85 ms, not waited for writeasync

Many Leaders

With many leaders, each region, or each device, takes writes locally and replicates them to the others asynchronously. A user's write is answered at local latency wherever they are, and a region cut off from the rest keeps taking writes.

The price is that two leaders can accept conflicting writes to one record, and something has to decide which survives; that is the last lesson of this chapter. Any rule that needs one answer stops holding across leaders. Two users can claim the same username in two regions in the same second, and each leader approves its own.

An app that edits offline is the extreme case: every device is a leader. How whole regions are laid out, one active and one standing by or both active, is Chapter 6's lesson on regions.

No Leader

In a leaderless store, the client or a coordinator node sends each write to several replicas and counts the acknowledgements, and a read asks several replicas too. Amazon's Dynamo and Cassandra are the usual examples. No node is special, so there is no failover: when a node dies, requests go to the others and the counting goes on.

What it costs is drift. After failures the replicas disagree, and something has to repair them. A read that gets two different answers has to tell which copy is newest. The counting rule is the next lesson, and telling versions apart is the last.

Synchronous vs Asynchronous Replication

Synchronous replication answers after a follower has the write, so a promoted follower holds every acknowledged write. Use it when an acknowledged write must survive the leader's loss and the follower is close enough that the round trip is affordable.

Asynchronous replication answers before any follower has the write, so the leader's loss can take the last writes with it. Use it for followers far away or there only for reads, where losing the last few writes in a failover is acceptable.

How Interviewers Probe This

Every good answer in this area has one shape: the scheme, the moment a write counts, then the failure it survives and the one it does not.

The first probe is "Your primary dies. What did you lose?" Say synchronous or asynchronous first. With an asynchronous follower, name the window, the writes the follower had not received. Then say what stops the old primary writing when it comes back. You might say: "The zone follower is synchronous, so a failover loses no acknowledged write, and the old primary is fenced before the new one takes traffic."

The second is "Why not let every replica take writes?" A good answer names what many leaders buy, local writes and a cut-off region that keeps writing, and what they cost, conflicts and no unique constraint across leaders. Then it says which records of this design could live with that, and which could not.

The third is "Make replication synchronous. What does it cost?" Give the round trip per write for where the follower sits, 1 to 2 milliseconds in the next zone or about 85 across the Atlantic, and say what happens to writes while that follower is down.

Misconceptions
  • "Three copies means no data loss." With asynchronous followers, a write acknowledged a moment before the leader dies exists on no surviving copy.
  • "Multi-leader is single-leader with better availability." It trades the failover for conflicts, and every rule that needs one answer, such as a unique name or a stock count, breaks.
  • "A synchronous follower is consistent for reads." Synchronous can mean only that the follower has stored the write. Until it applies it, a read there returns the old value.
  • "Leaderless means no coordination." Each write still waits for a count of replicas, and repair runs in the background all the time to keep them close.
  • "Failover is automatic, so it is free." Deciding that the leader is dead takes time, and two nodes that both believe they lead will both accept writes. That is split brain, and Chapter 5's fencing is what stops it.
Why It Matters
  • Name who accepts writes and when a write counts before you add a second copy to the board.
  • Put the synchronous follower in another zone of the same region, and say the 1 to 2 milliseconds it adds to each write.
  • State what an asynchronous failover can lose, the writes the follower had not received, and say whether the product accepts that.
  • Choose many leaders only for a stated reason, such as local writes in each region or offline devices, and name the conflict rule in the same sentence.
RelatedPostgreSQL Deep Dive one engine's replication, synchronous dial and failoverRead Replicas and Lag followers on the read path and their lagQuorums the counting rule of a leaderless storeLeases, Locks and Fencing stopping the old leader

Knowledge Check

A leader replicates asynchronously to two followers and dies. One follower is promoted. What can be lost?

  • Writes the leader acknowledged that had not reached that follower yet
  • Nothing, because three copies of the data existed when the leader died
  • Only writes still in flight that no client was ever told had succeeded
  • Every write since the last backup, because followers hold no recent log

A design must survive the loss of a zone without losing an acknowledged write, at the least cost to each write. Where should the synchronous follower sit, and what does it add?

  • In western Europe, adding about 85 ms to every write the leader takes
  • In another zone of the same region, adding 1 to 2 ms to every write
  • In the leader's own zone, adding less to every write than another zone
  • In another zone of the same region, adding about 85 ms to every write

Two regions each run a leader and replicate to each other asynchronously. Which rule stops holding?

  • Every write is answered at the latency of the user's own region
  • A region cut off from the other keeps accepting its users' writes
  • Each username belongs to exactly one account across the whole system
  • Each region eventually receives the writes the other region accepted

A leaderless store keeps three copies of each key. One node dies. What happens?

  • Writes stop until the remaining replicas elect one of them to lead
  • Requests go to the remaining replicas and keep counting, with no failover
  • Reads of its keys stop until an operator restores the node from a backup
  • Every key moves to a new node at once, before any request is answered

Replication to a follower is synchronous. A read on that follower right after a write returns the old value. How?

  • The follower had stored the write but not yet applied it when the read came
  • Synchronous replication waits only for the leader's disk, never a follower's
  • The follower dropped the write, and the leader will resend it on the next sync
  • Reads on a synchronous follower always return the value before the last write

You got correct