Technopedia

The technologist’s encyclopedia

Consensus and Consistency

Consensus and consistency study how multiple nodes agree on a single value or a sequence of values, despite failures and network partitions. They are relevant whenever a distributed system must maintain a consistent state across replicas.

Authoritative sources

Impossibility results

The two generals problem (Akkoyunlu, Ekanadham & Huber, 1975) is the foundational intuition: two generals must agree to attack or retreat, but messages can be lost. No finite protocol guarantees agreement. This thought experiment motivates the formal result below.

FLP theorem (Fischer, Lynch, Paterson, 1985): no deterministic consensus algorithm can guarantee termination in an asynchronous system with even one crash failure. The proof uses a topological argument: the state space is connected, and a decision requires a discontinuity.

Details

Why FLP matters: it shows that consensus requires either (1) synchrony (bounded message delays), (2) randomization (probabilistic termination), or (3) a failure detector (a mechanism that detects crashes). In practice, systems use a combination: timeouts (approximate synchrony) + leader election (randomization via random timeouts).

Byzantine faults (malicious nodes) require n≥3f+1n \geq 3f + 1 nodes; see Adversarial-failure for the full treatment.

CAP theorem

CAP theorem (Brewer, 2000; Gilbert & Lynch, 2002): a distributed data store can provide at most two of three guarantees simultaneously:

Since network partitions are inevitable, the practical choice is between C and A during a partition.

Details

Consistency (C): all nodes see the same data at the same time. A read after a write returns the written value. This is the strongest guarantee (linearizability).

Availability (A): every request gets a response, even if the response is stale. The system does not block or return errors during a partition.

Partition tolerance (P): the system tolerates network partitions. Since real networks partition, P is non-negotiable for any distributed system. The choice is C vs. A.

Examples:

The CAP theorem is the organizing principle for the consistency models below: linearizability is the C extreme, eventual consistency is the A extreme.

Consensus algorithms

Paxos (Lamport, 1998): a two-phase protocol (prepare + accept). A proposer selects a value, sends a prepare request to a quorum (>n/2> n/2), then an accept request. The first value accepted by a quorum is the consensus value.

Raft (Ongaro & Ousterhout, 2014): a more understandable consensus algorithm. Three phases:

  1. Leader election: a node times out and becomes a candidate. It requests votes from peers. A quorum (>n/2> n/2) elects the leader.
  2. Log replication: the leader appends entries to its log and sends AppendEntry RPCs to followers. A quorum acknowledges; the leader commits the entry.
  3. Safety: a leader can only commit entries from its own term. A follower rejects a leader with a lower term.

2PC (Two-Phase Commit) (Garcia-Molina, 1987): an atomic commit protocol for distributed transactions. Phase 1 (prepare): the coordinator asks all participants if they can commit. Phase 2 (commit/abort): if all vote “yes,” the coordinator commits; otherwise it aborts. 2PC is the basis for distributed database transactions.

Details

Quorum: 2f+12f + 1 nodes tolerate ff crash failures. For 3 nodes: quorum = 2. For 5 nodes: quorum = 3. For 7 nodes: quorum = 4. See Fault-Tolerance for replication strategies and quorum-based read/write policies.

Quorum intersection property: any two quorums of nn nodes (size >n/2> n/2) must share at least one node. This is the safety argument: if a value is committed (accepted by a quorum), any future leader (elected by a quorum) must have heard of it.

Quorum intersection (n=5, quorum=3): Quorum 1: {A, B, C} Quorum 2: {B, D, E} B Any two 3-subsets of a 5-set share ≥ 1 element (pigeonhole: 3+3 > 5). The overlap guarantees a committed value is known to the new leader.

Leader election (Raft): each node has a random election timeout (150–300 ms). If no heartbeat is received within the timeout, the node becomes a candidate and increments its term. It votes for itself and requests votes from peers. The first candidate to get a quorum becomes the leader.

Raft state machine:

Follower Candidate Leader timeout fires new leader / term quorum votes discover higher term

Log compaction: the log grows unbounded. Snapshots: periodically, a node saves its state and truncates the log. A new follower that is too far behind receives a snapshot instead of the full log.

2PC details: 2PC is blocking: if the coordinator crashes after participants have voted “yes” but before sending the commit, participants are stuck in the “prepared” state. This is why 2PC is used for short-lived transactions, not for long-running consensus. Paxos and Raft solve this by making the log the source of truth.

Consistency models

The CAP theorem organizes the consistency spectrum: under a partition, a system chooses between consistency (C) and availability (A). The models below range from the strongest (C extreme) to the weakest (A extreme).

Linearizability (strongest, C): every operation appears to take effect instantaneously at some point between its invocation and response. All reads and writes are serialized in a total order consistent with real time. Raft’s committed log is linearizable.

Serializability: the result of concurrent transactions is equivalent to some serial execution. Weaker than linearizability (no real-time constraint).

Causal consistency: if operation A causally precedes operation B (A’s result is used by B), then all nodes observe A before B. Unrelated operations may be seen in different orders.

Read-your-writes: a reader always sees its own previous writes. A per-client guarantee.

Monotonic reads: once a client reads a value, subsequent reads never return an older value.

Eventual consistency (weakest, A): if writes stop, all replicas eventually converge to the same value. No bound on convergence time.

Details

Linearizability vs. serializability: linearizability adds a real-time constraint. If client X writes value 5 at time t₁ and client Y reads at time t₂ > t₁, a linearizable system must return 5 to Y. A serializable system might return an older value if the serial order places Y’s read before X’s write.

MVCC (Multi-Version Concurrency Control): readers see a snapshot; writers create new versions. Provides snapshot isolation (a weaker form of serializability). Used in PostgreSQL, Oracle, and many NoSQL stores.

Causal consistency in practice: implemented with vector clocks or hybrid logical clocks. DynamoDB’s “strong” consistency is linearizable; its “eventual” mode is eventual consistency.

Choosing a model:

The single-machine analog of consensus is the mutex: a lock ensures mutual exclusion among threads, just as a quorum ensures agreement among nodes. Message delivery semantics (at-least-once, exactly-once) and message queues are covered in Message-Passing.

Example problem

Problem: A 5-node Raft cluster (nodes A, B, C, D, E) has A as the leader. Node A crashes. Describe the leader election process and the safety guarantees.

Solution
  1. Detection: B, C, D, E stop receiving heartbeats from A. After their election timeouts (150–300 ms, random), they become candidates.

  2. Election: Suppose B’s timeout fires first. B increments its term to 2, votes for itself, and sends RequestVote to C, D, E. C, D, E have not voted in term 2, so they grant their votes. B has 4 votes (itself + C, D, E) > quorum (3). B becomes the leader.

  3. Safety: B’s log is at least as up-to-date as any other node’s log (Raft’s election restriction: a candidate must have a log at least as up-to-date as a quorum of peers). B cannot commit entries from a previous term that were not committed. Any entry that was committed before A’s crash is in B’s log.

    Quorum intersection guarantee: the committed entry was written to a quorum (3 of 5 nodes). B was elected by a quorum (3 of 5 nodes). By the pigeonhole principle, these two quorums share at least one node, so B (or a node that voted for B) has the committed entry.

    Committed entry in quorum {A, B, C}; B elected by quorum {B, D, E}: Wrote: {A, B, C} Elected: {B, D, E} B Overlap = {B} → B has the committed entry. Safety holds.
  4. Recovery: B sends heartbeats to C, D, E. They switch to follower mode and accept B’s term. If D’s log is behind B’s, B sends the missing entries via AppendEntry. D applies them and catches up.

Guarantee: no committed entry is lost. The new leader’s log contains all committed entries. The system continues to make progress as long as a quorum (3 of 5) is alive.