Systems and protocols · Intermediate

Raft replicated logs: separating storage, commitment and application

Use a five-node example to connect log matching, current-term commitment and state-machine application under crash failures.

Ask Logeia about this articleTry the three questionsReview your answers

Updated

On this page
  1. Agree on an ordered command history
  2. Work the majority threshold carefully
  3. Repair inconsistent suffixes
  4. Liveness and client semantics remain
  5. Trace terms and indexes separately
  6. Connect retries to business commands
  7. Exercise: replay identity in the state machine
  8. Ask Logeia about this articleCheck your understanding

Agree on an ordered command history

Raft is a consensus algorithm for agreeing on an ordered log whose commands are applied by replicated state machines. A leader accepts commands and replicates log entries to followers. A term is a logical election-related number, not a count of wall-clock seconds. Entries carry an index and term, helping identify the history to which they belong. The usual model concerns crash failures and recovery under the required storage assumptions. It is not a Byzantine-fault protocol for arbitrary participants that intentionally lie about their state or forge protocol behavior.

A replicated log is not permission for every node to execute the same commands in an arbitrary local order. Adding ten and doubling a balance can produce different results depending on order. State-machine replication assumes consistent initial state and deterministic application of the agreed commands. If each node independently samples an external clock, random source, or third-party API, identical logs need not yield identical results. Decide where nondeterministic inputs become fixed data included in the replicated command. Agreement over order and deterministic execution are complementary responsibilities.

Work the majority threshold carefully

A five-node cluster has a majority of three. A current-term entry stored under the protocol’s requirements on the leader and two followers can advance commitment; the leader and one follower alone are insufficient. However, simply finding an arbitrary older-term entry on three machines is not the complete commitment rule. Raft’s treatment of current-term entries and log properties matters to safety. Also distinguish storage of an entry, commitment of that entry and application to the state machine. Those are related transitions, not synonyms for one instantaneous event.

The storage represented by an acknowledgement must satisfy Raft’s persistent-state rules. An entry briefly present in volatile memory is not automatically retained after restart. After the leader advances its commit index, followers can still lag in learning the commit position and applying it. The client acknowledgement boundary must also fit the service’s state-machine contract. Draw network receipt, persistent storage, commitment, and application as distinct events. Conflating them can make a system appear safe on a diagram while its implementation acknowledges work before the state required by the protocol actually survives failure.

Repair inconsistent suffixes

AppendEntries checks whether the preceding index and term match the follower’s log. When they do not, the leader finds a suitable shared prefix and sends a consistent suffix. A returning node’s uncommitted tail is not automatically the cluster’s final history. Election restrictions also consider how up-to-date a candidate’s log is; the first process that asks is not necessarily entitled to lead. Wall-clock timestamps are not the deciding evidence. Reason about terms, indexes, log matching and election rules together to understand why a conflicting uncommitted suffix can be replaced without discarding committed history.

Log freshness is compared by the last entry’s term first and its index second. A candidate with more entries is not necessarily the eligible choice when another candidate has a later last-log term. An old leader’s long uncommitted suffix does not override the election restriction. Persisted voting state and the rule restricting each term’s vote connect elections to log safety. Treating leader selection as an unrelated majority poll misses the reason committed history remains available across leadership changes. Explain the comparison order explicitly when tracing an election.

Liveness and client semantics remain

A partition without access to a majority cannot safely keep committing new writes under the normal protocol. Safety and availability must be discussed separately. Reading from a node that calls itself leader is also not sufficient by itself for a linearizable read: the read path must establish the relevant leadership and commit/application conditions. Lost client replies introduce another issue, because resending the same command can require request identifiers and deduplication. Agreement on a log is a powerful building block, but it is not automatic resolution of every application-level failure and retry outcome.

An isolated old leader cannot immediately learn that another group elected a new leader. Returning a read merely because its local role field still says leader can expose stale state. A linearizable-read procedure must establish the relevant current authority and wait for required committed state to be applied, according to the chosen protocol. Lease-based optimizations introduce timing assumptions that must be stated separately. The basic log replication argument does not automatically justify every read optimization. Distinguish which assumptions protect write commitment and which protect the particular read path used by the service.

Trace terms and indexes separately

Let A lead term four in a five-node cluster. Suppose an entry at index eight, term four, is replicated to A, B, and C under the required persistence rules. The current-term majority condition can allow commitment even while D and E lag. A returning D receives missing history using previous-index and previous-term checks. Now change only the entry’s term to three: it is not valid to apply the same direct commitment argument merely because three nodes store it. Raft distinguishes older-term entries and can commit them indirectly when a suitable current-term entry commits. Trace those conditions rather than using the largest index or replica count as the entire proof. This is an illustrative protocol execution, not a production failure-test result.

Connect retries to business commands

Suppose a client asks to add ten to a balance, the command is applied, and only the reply is lost. A retry may be indistinguishable from another request to add ten unless the application provides identity. Raft agrees on log order; it does not invent the business meaning of repeated messages. A client identifier and request sequence can be included in commands, with processed identifiers and results maintained by the replicated state machine. Retention limits and identifier reuse must be defined. Snapshots must preserve deduplication state still needed by the contract. Even then, effects on an external API need their own integration design. Agreement, duplicate suppression, and external exactly-once effects must not be collapsed into one unqualified promise.

Exercise: replay identity in the state machine

Assume three commands already committed under the required protocol and an initial balance of five. Position one adds ten under (client-A,request-1), position two repeats that identity and payload, and position three doubles under (client-A,request-2). Including processed identity and saved results in state gives balances fifteen, fifteen and thirty. Treating position two as another independent addition incorrectly produces twenty-five before the final multiplication.

Three log records do not imply two independent business additions. Every replica needs the same identity state and deterministic apply rule. A snapshot retaining only thirty but dropping replay protection can later reapply request-1. Align identity retention with the retry contract. This is a state-machine exercise over assumed committed input, not an election or real-cluster failure test.

PositionCommandBalance afterward
1request-1: +1015
2request-1: same retry15
3request-2: ×230

Reading and practice are recorded separately in this browser, for each language.

Learn this in order

Understand the systems behind the web · STEP 18

Choose a learning path