LearnastraTHE DESIGN COURSE

Understand the building blocks / CHAPTER 20

Leader and follower replication

Put a group’s writes in order while checking what followers have applied and how a replacement safely takes over.

Advanced1h of deliberate study10 questions

Builds on Read and write quorums, Redundancy and replication.

By the end, you can
  • Trace a write from leader acceptance to follower application.
  • Distinguish appended, committed, and applied state.
  • Explain why elections and read authority need a complete protocol.

01One writer orders a replicated history

Two shoppers try to reserve inventory. A three-machine group A, B, and C sends changes through its current leader A, which puts proposed commands in an order. The followers copy that history. The arrangement is leader–follower replication for this group’s records, such as one shard.

Follow what a saved command means before assuming a query can see it:

  1. A node appends a command to its log: an ordered list of proposed changes saved at that node.
  2. The replication protocol decides when the command is committed, meaning it is accepted into the group’s history under the protocol’s safety rules.
  3. A node applies the command by changing the records its ordinary queries read.

These steps can occur at different times. A follower can have a command saved in its log while its queries still see earlier records. A committed command also need not have been applied by every follower.

The leader coordinates order, but can fail or become isolated. A consensus protocol is the complete set of rules by which the group agrees on accepted history and changes leaders safely. Raft is one example. Calling a server “primary” does not automatically give it those rules or specify its acknowledgment settings.

02Trace a command and a freshness-sensitive read

Suppose a client wants to reserve seat S7 under reservation R12. These names identify the particular seat and reservation in this trace. The proposed command receives log position 22.

  1. The client sends its stable request identifier to leader A. A retry of this same intended reservation retains that identifier.
  2. A checks the request and appends the command at position 22.
  3. A sends log entries to followers. The group follows the protocol’s required saved-progress and commitment rules.
  4. Once the protocol permits commitment, apply the command by first checking the group’s saved request results. If the same request already completed, reuse its saved result without claiming another seat. Otherwise apply the inventory change and save its request identifier and result together as one state-machine step. Reject an identifier reused for a different intended request.
  5. Return the saved result only after the required commitment and application have completed. A lost response can then be recovered through the same request identifier.

Now a read asks follower C for state at least as new as position 22. C has saved the log through 22 but applied only through 21:

  1. Check C’s applied position, not just its saved-log position.
  2. Wait until it applies the required state, use an eligible source that has applied it, or return the documented failure.
  3. Do not successfully return state 21 for a request explicitly requiring 22.

A current-value read needs another check: is its source still allowed to act as leader? A can remain running while isolated from B and C, which may establish a replacement through the valid protocol. A’s local leader flag is no longer sufficient.

In a quorum-based strong-read procedure, the leader asks the required followers to confirm its leadership, then waits until its own records include the committed position chosen for that read. The complete protocol supplies the voting, history and read-position rules; ordinary health replies do not. Another supported approach uses a lease: permission that expires at a stated time under specific clock and delay assumptions. If confirming authority or applying commands through that read position cannot finish before the caller’s deadline, redirect to an eligible source or return unavailable instead of waiting indefinitely.

03Change leaders without inventing history

A safe replacement must preserve accepted history as well as establish permission to lead. In Raft, an increasing term number identifies a newer leadership attempt or authority period; it is not wall-clock time. Voting also checks candidate logs under the full protocol so required accepted history is preserved.

Separate the takeover tasks:

  1. Suspect the current leader when required contact is missing. Silence starts investigation or an election attempt; it does not prove the old process stopped.
  2. Establish a replacement through the implementation’s voting and history-preservation rules. A timeout does not make the candidate’s own uncommitted entries accepted.
  3. Make a returning old leader recognize newer authority, stop leading, and reconcile its history through the protocol.
  4. Direct clients to the current authority. Recover uncertain requests by looking up their retained identifiers and results, rather than inventing another reservation.

An ordered command still does not atomically control an outside service. For a payment:

  1. The group can agree to execute an instruction to charge.
  2. The payment provider may charge before its response is lost.
  3. A retry must reuse the provider’s supported payment identifier or query its retained outcome.
  4. A newly generated payment identity could charge again. Agreement on the instruction did not include the provider inside the database commit.

Measure saved-log progress separately from applied progress, commitment waiting, election frequency, and rejection of obsolete authority. Test a lagging follower, an isolated leader, a lost response, and the old leader’s return. Use a proven consensus implementation; a few majority counts do not reproduce its complete safety rules.

Retained history and visible state are different

Retained history and visible state are differentThe client sends the same identifier for the same intended command. The leader follows the full replication and commitment procedure. C’s saved log reaches 22 while its applied records remain at 21. A minimum-22 read checks applied state, and a strong read separately checks current authority. Numbered arrows correspond to the connection key below. All connections are directional.Client commandStable request IDCurrent leader AProposes log position22Command acceptedby protocolRequired copies havesaved progressFollower CLog 22; applied 21Minimum 22 readCannot return state21 as sufficientAuthority checkOld isolated leadermay be stale12345Retained history and visible state are differentThe client sends the same identifier for the same intended command. The leader follows the full replication and commitment procedure. C’s saved log reaches 22 while its applied records remain at 21. A minimum-22 read checks applied state, and a strong read separately checks current authority. Numbered arrows correspond to the connection key below. All connections are directional.Client commandStable request IDCurrent leader AProposes log position22Command acceptedby protocolRequired copies havesaved progressFollower CLog 22; applied 21Minimum 22 readCannot return state21 as sufficientAuthority checkOld isolated leadermay be stale12345Retained history and visible state are differentThe client sends the same identifier for the same intended command. The leader follows the full replication and commitment procedure. C’s saved log reaches 22 while its applied records remain at 21. A minimum-22 read checks applied state, and a strong read separately checks current authority. Numbered arrows correspond to the connection key below. All connections are directional.Client commandStable request IDCurrent leader AProposes log position22Command acceptedby protocolRequired copies havesaved progressFollower CLog 22; applied 21Minimum 22 readCannot return state21 as sufficientAuthority checkOld isolated leadermay be stale12345

Follow the numbered arrows

  1. Client command → Current leader Avalidate command identity
  2. Current leader A → Command accepted by protocolfollow complete protocol
  3. Command accepted by protocol → Follower Creplicate/commit progress
  4. Minimum 22 read → Follower Cwait or choose valid fallback
  5. Authority check → Current leader Astrong reads require current authority
C can have command 22 saved while its queryable records still reflect only commands through 21. A read requiring 22 must wait or use another eligible source. A current-value read must also establish that its source retains the required authority.
Trace the diagram in words
  1. The client sends the same identifier for the same intended command.
  2. The leader follows the full replication and commitment procedure.
  3. C’s saved log reaches 22 while its applied records remain at 21.
  4. A minimum-22 read checks applied state, and a strong read separately checks current authority.

DRAW. CALCULATE. EXPLAIN.

Handle position 22 during leader isolation

20 minutes · guided practice

A led a three-node group and committed log position 22. C has applied only through 21. A becomes isolated while B and C establish valid new authority through the complete protocol.

  1. Can A use its old leader flag to justify a current-value read?
  2. What must C do for a read requiring at least 22?
  3. How should a client retry a command whose response was lost?
Your self-check
Compare the worked solution
  1. A may still run, but its old flag does not establish current authority. It must satisfy the supported read procedure or wait, redirect, or refuse the request.
  2. C must apply the valid committed history through at least 22 before satisfying that minimum. Saved log position 22 alone is insufficient.
  3. The client retains the original logical request identifier. The current authority checks its saved result; if the command committed, return that result rather than create a second reservation.
  4. This example assumes B and C obtain authority using the implementation’s full voting, accepted-history, and commitment rules. Having two reachable machines is not itself an implementation of those rules.

The checkboxes record your own assessment. The worked solution gives you a way to check the reasoning.

RETRIEVE BEFORE YOU REVEAL

Keep the important ideas.

Say the answer aloud, then turn the card over.

CARD 01How do appended, committed and applied log entries differ?
  1. Appended means a node has the proposed entry in its log.
  2. Committed means the group accepted it under the protocol.
  3. Applied means it changed the records this node’s queries read.

A follower can reach these stages at different times.

CARD 02Why check authority for a strong leader read?
  1. An isolated former leader can still run with an old leader flag.
  2. A current-value read must establish authority through the supported procedure.
  3. The source must also have applied the required state.

Permission to lead and query-ready data are separate checks.

CARD 03What does a replicated log not cover automatically?
  1. Replicas may agree on an instruction to charge a card.
  2. The provider may charge before its response disappears.
  3. Reuse the supported payment identifier or query its saved result.

A repeated instruction must not automatically become a fresh charge.

Practise this topic in the revision deck ↗

CHECK THE REASONING

Can you explain the choice?

Answer every question, then finish for an immediate score and an explanation for each answer. Aim for 80%, then revisit any gaps.

1 / 10Entry 22 is logged on C but C has applied only 21. What can a local query reflect?
2 / 10A client needs a result at least as new as 22; C is at 21. Which response fits?
3 / 10A is isolated but still running while a new leader is elected. Why can A’s local read be unsafe?
4 / 10A reservation commits but its response is lost during leader change. What should a retry retain?
5 / 10What is the leader’s principal responsibility here?
6 / 10What does a protocol term or epoch help identify?
7 / 10Does a local election timeout alone safely promote a follower?
8 / 10A committed command invokes an external payment. What remains necessary?
9 / 10Which measurement directly distinguishes retained log from query readiness?
10 / 10What makes a leader–follower design claim reviewable?
0 of 10 answered

CLOSE THE LOOP

Explain it without the page.

Summarise the mechanism, one trade-off, and one failure mode. Then mark this chapter complete.

Go to the source