Skip to content

Agree on one history with consensus

15 min reading

Explain quorum, leader changes and the limits of a replicated log

Page details

System Design · Preserve correctness

15 min reading · 28 min with practice

Sources

Published · Last edited:

Prerequisite: Coordinate changes across service boundaries. For basics, use the independent System Design foundations.

More
20px

Hypothetical scenario. Northwind’s ownership service loses contact with a machine. Dev lets each surviving monitor choose an owner. Priya draws two monitors with different observations, each confidently assigning the same work to someone else.

Dev: “Both monitors believe the earlier owner stopped.”

Maya: “Which shared history makes either reassignment authoritative?”

Trace a proposal through agreement and application, then carry its ownership generation to the protected effect. Keep this selected coordination group separate from the earlier key-value replica example. A machine’s silence remains an observation, not proof that it cannot resume.

Who may turn a failure observation into one history?

If a worker stops responding, it may have crashed, paused or become unreachable through one path. Declaring it dead does not physically prevent it from running. Independent monitors can therefore elect different owners if they each treat local silence as conclusive authority.

Consensus protocols distinguish preserving a safe decision history from making progress. Under sufficiently poor communication, a system can preserve safety by refusing to decide. Availability depends on communication and surviving participants under the protocol's assumptions; no timeout setting removes that dependency.

Do not invent a leader election protocol from a shared timestamp and a periodic cleanup task. The application may need only a conditional change in an existing authoritative database. If it does need a replicated coordination service, use a documented, maintained implementation and understand its failure and recovery model.

Order commands before applying them

A replicated state machine applies the same deterministic commands in the same agreed order. The log position identifies that order; the state machine defines what a command does. An application must make external inputs explicit rather than letting each replica independently read a different clock or external balance.

Hypothetical scenario. Take a fixed five-member group A, B, C, D and E. Every member has the same committed log through index 20, whose last entry has term 7. Leader A becomes unreachable. B, C and D can communicate; E is slow. These are chosen starting conditions, not a recorded outage.

C's election timer expires. It increments its term from 7 to 8, records its vote for itself and requests votes. B and D compare C's last-log term and index with their own. Here the logs are equally up to date, and neither has voted for another candidate in term 8. After persisting their term and vote, they grant C their votes. C has itself, B and D: three votes out of five, so it becomes leader in term 8.

Those votes choose a leader; they do not commit the settlement command. C first establishes contact as leader. A client then requests “assign settlement job to generation 8” under operation identity K. C durably appends that command at index 21 with term 8 and sends AppendEntries referring to predecessor index 20, term 7. B and D verify that predecessor, persist the new entry and acknowledge. C now knows that C, B and D hold index 21.

An election and a command are separate agreement stepsIn a five-member group, candidate C receives its own vote plus votes from B and D in term 8. After election C replicates index 21 term 8 to B and D. A majority with that current-term entry lets C advance commitIndex, apply the command and later tell followers the committed position. A and E are not required for this illustrated majority.B: followerC: candidate → leaderD: followerRequest vote, term 8 ←→ Request vote, term 8Vote →← VoteAppend (21, 8) ←→ Append (21, 8)Persist + acknowledge →← Persist + acknowledgeCommit 21, then applyTell followers commitIndex = 21C + B + D form the illustrated 3-of-5 majority.
Messages and durable state have distinct roles. Line placement shows sequence, not measured latency.

Because index 21 belongs to C's current term and is replicated to a majority, C advances its commit index to 21. It applies committed commands in order, recording the operation result. Later replication or heartbeat messages carry the advanced commit position so followers can apply it too. A follower holding the entry may therefore be behind in knowledge of commitment without holding different command bytes. These rules follow Raft, Figure 2 and Sections 5.2–5.4.

Now lose C's response after application. The client sees a timeout, although assignment K is recorded. A retry must preserve K, and the application's replicated state must remember K's result if it promises deduplicated execution. Consensus alone does not recognize that two independently submitted commands describe one business intention. The protected-effect lesson explains that additional contract.

Carry one agreed log through failover

Hypothetical scenario. Inspect a Raft-style group with members A, B and C and committed entries through index 20. A majority is two. Any two majorities intersect, but that arithmetic alone does not define the vote rules, log matching, current-term commit condition or safe membership transition.

Carry one agreed log through failover — selected design evidence
Log evidenceExampleReview question
Entry positionindex 20, term tWhich prefix is committed?
ConfigurationA, B, CWhich voters count?
External generationassignment epoch eDoes target reject e−1?
Majority counts need the complete election and log protocol; an external target needs a separate fence.Majority counts need the complete election and log protocol; an external target needs a separate fence. A log through 20 to B log through 20: majority evidence; B log through 20 to C catching up: replicate / install coherent prefix.A log through 20B log through 20C catching upmajority evidencereplicate / install coherent prefix
Majority counts need the complete election and log protocol; an external target needs a separate fence. Enlarge the diagram to inspect its labels; the surrounding records and explanation give the text alternative.

Store a command with term and index. A new leader's election must satisfy the protocol's log restrictions before it can extend the authoritative history. A snapshot replaces an included prefix only together with the position, term and configuration evidence needed to continue recovery. Installing arbitrary application state is not a substitute for a coherent replicated snapshot.

Membership changes must preserve the protocol's quorum argument; disjoint old and new groups cannot each decide independently. Even a correct agreed log does not automatically fence a remote effect target. Carry a committed generation to that target and require stale-generation rejection there. Distinguish agreement about assignment from enforcement of the assigned work.

Count a majority and enforce a generation

Hypothetical scenario. Choose a fixed five-member coordination group. A majority contains floor(5 ÷ 2) + 1 = 3 members. Two majorities have at least 3 + 3 − 5 = 1 member in common. If two members are unavailable, three remain; if three are unavailable, only two remain and cannot form this majority.

This demonstrates set intersection under fixed membership. It does not prove safety for an arbitrary voting implementation or establish progress during unbounded delays. Durable protocol state and valid transition rules still matter.

Now suppose the agreed authority generation changes from 7 to 8 when ownership moves. The protected resource has durably recorded 8. A resumed worker presents 7; checking 7 < 8 allows the resource to reject that obsolete action. A current worker presenting 8 must still pass authorization and business validation.

If the resource never checks the generation, recording 8 in the coordinator alone cannot stop the old worker's call. If independent resources each remember different generations, the application must define how authority reaches and is enforced by all required effect boundaries.

For command storage, choose 100 accepted commands per second and 200 bytes of logical command payload. The arrival payload is 100 × 200 = 20,000 bytes per second. Retaining one hour without compaction yields 20,000 × 3,600 = 72,000,000 bytes, or 72 MB. Protocol metadata, replicas and snapshots are excluded. Snapshotting reduces retained history only under a correct state and recovery procedure.

Try a variation before continuing: only C and B acknowledge index 21, while D becomes unreachable. May C apply this new entry as committed? The chosen majority is three; C and B supply only two. C must not treat this new current-term entry as committed from those acknowledgements. A separate database conditional update may be simpler if the application already has a suitable authoritative store; a custom two-vote shortcut is not an equivalent alternative.

If A later returns with term 7 messages, members already in term 8 reject stale-term requests. A steps down when it learns the higher term. Any conflicting uncommitted suffix must be reconciled through Raft's predecessor and log-matching rules. This still does not stop a previously prepared call to an external payment provider: the provider needs an enforcing action identity or authority boundary of its own.

Carry the agreement proof through membership and recovery

The earlier walkthrough already separates votes, durable log replication, commitment and application. Its fixed membership is essential. Hypothetical scenario. Replace old members A, B, C, D, E with new members C, D, E, F, G. A, B, C form an old majority; E, F, G form a new majority. Both have three members, yet those two sets have no member in common. The earlier fixed-set intersection calculation cannot justify an instantaneous switch between these configurations.

Raft’s joint-consensus configuration procedure, section 6 uses a transition requiring separate majorities of old and new configurations. It then commits the new configuration under the protocol’s rules. Implementations can use other documented reconfiguration protocols; copying the joint quorum count alone is not enough. Configuration state, log position and election rules must agree about which participants may decide.

A restarted leader faces another distinction: holding an old-term entry on several replicas is not permission to commit it merely by counting copies. Raft’s current-term commit rule, leader election restriction and log matching jointly protect committed history. Committing a current-term entry also establishes commitment of its preceding log entries. Removing one of those conditions turns a worked protocol into an unsafe imitation.

A leadership label is also insufficient for a current read after a possible partition. The service must establish the required current authority, apply the necessary committed state and use the chosen protocol’s read mechanism. A quorum-confirmed read path and a lease-based optimization have different obligations; a lease adds timing assumptions that must be justified. A stale former leader cannot make a read linearizable by remembering that it once won an election.

Finally, a snapshot replaces retained log history with a state-machine image at a specific included index and term. It must include application state needed for the promised semantics, such as retained deduplication results. Snapshotting only the visible assignment while forgetting operation K can let a retry appear new after restore. Snapshot installation, later log replay and compaction need a coherent boundary, as described in Raft’s section 7.

Can the two disjoint majorities safely approve unrelated histories after an immediate membership swap?

The fixed-membership intersection argument supplies no such guarantee: {A, B, C} and {E, F, G} are disjoint. Use the complete reconfiguration protocol and its transitional authority rules. Preserving a majority count while changing the population does not preserve the proof.

In the wild: inspect the complete protocol

Ongaro and Ousterhout’s Raft paper specifies election, replication and safety rules. The illustrated current-term entry requires the stated majority before application; an election alone does not commit the new command. Use the complete protocol rather than extracting an acknowledgement count as a replacement algorithm.

Agreement has a deliberate scope

Consensus over a configuration key does not make all business records globally transactional. Likewise, a replicated log does not establish the correctness of commands written into it. Invalid business rules can be applied consistently by every replica.

Limit coordinated state to what needs ordered authority. Large payloads and expensive commands can increase the work on the agreement path. A design can agree on a reference to separately stored content, but then must define the content's identity, durability and availability before treating the reference as usable.

Raft and Paxos belong to the same broad problem family, but a course comparison should not imply that their implementation details are interchangeable. This lesson uses Raft's replicated-log model for orientation. A correct production implementation requires the complete chosen protocol, including membership changes and restart behavior.

A paused owner finishes after reassignment

Hypothetical failure scenario. The coordinator correctly assigns the settlement job to a new worker. The previous worker resumes and sends a previously prepared provider request. The provider accepts it because it knows neither the authority generation nor a shared operation identity.

The coordination service may have behaved correctly while the application still produced a duplicate effect. The repair belongs at the external action boundary: provider-supported operation identity, an enforcing gateway, or an explicit reconciliation protocol under the provider's actual capabilities.

Do not describe a lease as a universal solution. Its safety depends on its timing assumptions and the resource's enforcement. A paused process can act after its lease has expired unless some boundary rejects that action. Review the complete path from agreement to effect.

Three shortcuts that break the model

“A majority acknowledged, so any command is committed.” The selected protocol's term, log and commit rules determine that conclusion. Arbitrary acknowledgement counting is insufficient.

“A leader election prevents duplicate business actions.” It establishes authority within its scope. Retries and stale external actions still need effect-level protection.

“More members always improve availability.” More members change the required quorum, communication and operational obligations. Failure domains and implementation behavior matter alongside the count.

Coordinate only the decision that needs it

Use consensus-backed state for a shared authority, configuration or ordered state transition that must survive specified node failures. Define the behavior when the service cannot establish the required agreement. Preserve explicit unavailable outcomes instead of bypassing coordination during the very condition it exists to handle.

Avoid running an extra coordination cluster when a suitable existing transactional authority already satisfies the requirement. Operational ownership, backup, membership changes, version upgrades and failure recovery are part of the design cost.

The preceding transaction lesson asked whether participants share one commit decision or expose intermediate business states. This lesson supplies the narrower agreement mechanism. Next, the module checkpoint reconciles an uncertain operation without confusing those contracts.

Limits of this model

The five-member example assumes the Raft crash-failure model and fixed membership. Configuration changes and Byzantine behavior require different treatment. Consensus orders agreed commands; it does not independently validate business policy, authorize external effects or guarantee progress during arbitrary communication failures.

Challenge the proposed mechanism

An operator replaces all three voters at once and keeps serving writes on the old group. What is missing?

Reveal the design reasoning

A safe configuration transition and exclusive authority. Use the selected consensus protocol membership procedure, retain committed evidence and fence obsolete owners. Quorum arithmetic over two disjoint configurations proves no overlap.

Decision and reconsideration: Use a complete consensus protocol for the small history that needs agreement. Keep its membership, snapshot and external-effect boundaries explicit.

Follow authority to its effect

  1. Three of five nodes remain, but their messages are indefinitely delayed. Does the count guarantee prompt progress?

    Check the agreement boundary

    No. The arithmetic identifies a possible majority, not successful communication or bounded decision time. State the protocol's progress assumptions separately from safety.

  2. The coordinator has generation 8, while the protected resource accepts every caller. Is the old worker excluded?

    Check the agreement boundary

    No. The resource must enforce authority or the application needs another effective exclusion mechanism. An agreed number stored elsewhere cannot reject a call by itself.

  3. Every replica applies the same invalid settlement rule. Has consensus validated the business policy?

    Check the agreement boundary

    No. Agreement preserves command ordering and state under its model. Authorization, business invariants and external effect handling remain application responsibilities.

Northwind now has tools for identity, transactions, workflow state and agreed ownership. Page 31 combines them around an unanswered payment request and asks what the buyer can truthfully be told.

Northwind decision log

Require a quorum-backed term before exercising leadership.
Record the term that authorizes each coordinator.
Stop minority writers when quorum is unavailable.

Go deeper: models and agreement