Northwind: Consensus: a Raft election, replication and enforced authority
Outcome. After this lesson, you can follow a Raft entry from election to application.
Hypothetical scenario. At Northwind, Two workers disagree about which coordinator may assign work.
In short. Require a quorum-backed term before exercising leadership. Record the term that authorizes each coordinator.
Two workers both believe they own the job
Hypothetical scenario. A scheduled settlement job should have one active owner. The old worker pauses, another takes over, and the old worker resumes with its original credentials. Both now try to submit the settlement. A heartbeat dashboard cannot decide which action the receiving resource should accept.
Consensus establishes agreement among participating nodes under a specified failure model. In a replicated service it can maintain an ordered history of authority and state transitions. The practical question is which decision needs shared agreement and how the application enforces that decision beyond the coordination service.
The earlier chapters explained replica lag, safe promotion and retry identity. This chapter connects them: agreement about an owner does not automatically exclude a stale worker from an external system, and an agreed command can still be delivered to an application more than once.
A timeout is evidence, not a verdict
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 42” 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.
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 idempotency chapter explains that additional contract.
Follow a Raft entry from election to application
Northwind's coordinators need an agreed order for assignments, not a vote about whether a network timeout proves a worker dead. In Raft, a term identifies an election generation. Voting constraints and the replicated log work together so a new leader preserves committed history. The majority calculation is part of that protocol, not a substitute for its log-matching and commitment rules.
For a fixed five-member group, three is a majority because floor(5 / 2) + 1 = 3. Any two groups of three overlap in at least one member. With durable voting and the protocol's other conditions, that overlap contributes to election safety. Two unavailable members leave three that can potentially communicate and progress; a partition that prevents those three from exchanging the required messages still prevents progress. Node count alone is not a latency or availability guarantee.
A leader replicates entries and advances commitment under Raft's rules before applying committed commands to its state machine. Receiving an entry is different from committing it, and committing a command is different from successfully carrying out an unrelated remote action. Keep external effects behind their own identity, authorization and reconciliation boundary. Consensus makes replicas agree on an invalid business rule just as effectively as on a valid one.
An old worker illustrates the last boundary. Suppose generation forty-two replaces generation forty-one. A protected resource must reject generation forty-one where the action is accepted, or another effective exclusion mechanism must do that job. Merely writing forty-two into a coordinator store does not stop the paused older process when it resumes. Use consensus for the narrow shared decision that needs ordering, and enforce the resulting authority at the resource it protects.
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 41 to 42 when ownership moves. The protected resource has durably recorded 42. A resumed worker presents 41; checking 41 < 42 allows the resource to reject that obsolete action. A current worker presenting 42 must still pass authorization and business validation.
If the resource never checks the generation, recording 42 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.
In the wild: Follow a Raft entry from election to application
The primary reference establishes this concrete distinction: raft Figure 2 and Sections 5.2–5.4 specify persisted votes/logs, up-to-date voting, predecessor checks and current-term majority commitment before application..
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 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 transaction lesson next asks a different question: whether several participants must reach one commit decision, and what to do when an application instead permits intermediate states followed by compensation. Shared agreement is a building block, not a complete workflow specification.
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.
Check yourself: follow authority to the effect
Three of five nodes remain, but their messages are indefinitely delayed. Does the count guarantee prompt progress?
No. The arithmetic identifies a possible majority, not successful communication or bounded decision time. State the protocol's progress assumptions separately from safety.
The coordinator has generation 42, while the protected resource accepts every caller. Is the old worker excluded?
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.
Every replica applies the same invalid settlement rule. Has consensus validated the business policy?
No. Agreement preserves command ordering and state under its model. Authorization, business invariants and external effect handling remain application responsibilities.
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: Follow a Raft entry from election to application
- Video lecture: Raft — Martin Kleppmann. Follow the protocol rules beyond the majority calculation.
- Book chapter: Think Distributed Systems, chapter 10: Distributed consensus — Dominik Tornow. Read Distributed consensus while considering how to follow a Raft entry from election to application.
- Book chapter: Designing Data-Intensive Applications, 1st edition, chapter 9: Consistency and Consensus — Martin Kleppmann. Read Consistency and Consensus while considering how to follow a Raft entry from election to application.
- Primary source: In Search of an Understandable Consensus Algorithm (Extended Version) — Diego Ongaro and John Ousterhout. Stop minority writers when quorum is unavailable.
Related blog: Operation authority during an uncertain retry.