🎉 75% of content is free forever — Unlock Premium from $10/mo →
CW
đŸ’ŧ Servicesâ„šī¸ Aboutâœ‰ī¸ ContactView Pricing Plansfrom $10

Distributed Consensus

TheoryDistributed SystemsđŸŸĸ Free Lesson

Advertisement

Theory

Distributed Consensus

For a distributed system to function correctly, nodes must agree on a single value or sequence of values. Consensus algorithms ensure this agreement despite node failures and network partitions.

  • Agreement — All non-faulty nodes decide the same value
  • Validity — Decided values were proposed by some node
  • Termination — Algorithm eventually terminates

Consensus is the fundamental building block for replicated state machines.

The Consensus Problem

Multiple nodes must agree on a single value, even when some nodes fail or messages are delayed.

FLP Impossibility

Paxos

The foundational consensus algorithm, introduced by Leslie Lamport.

Paxos Phases

Paxos Two-Phase CommitPhase 1: PreparePProposerprepare(n)Acceptorspromise(n)Phase 2: AcceptPaccept(n,v)Acceptorsaccepted(n,v)Majority of acceptors must agree for consensus

Raft

A consensus algorithm designed for understandability.

Raft Leader Election

Raft Leader ElectionFollowerReceives heartbeatsReplicates log entriesVotes for candidatesCandidateTimeout triggers electionRequests votesWins majority → LeaderLeaderHandles all client requestsSends heartbeatsManages log replicationtimeoutmajorityState transitions: Follower → Candidate → Leader

Raft Properties

PropertyDescription
Election SafetyAt most one leader per term
Leader Append-OnlyLeader never overwrites/deletes entries
Log MatchingIf two logs contain entry with same index and term, all prior entries are identical
Leader CompletenessIf entry committed in term T, present in leader's log for all higher terms
State Machine SafetyIf server applied entry at index i, no other server applies different entry at i

Raft vs Paxos

AspectRaftPaxos
Design goalUnderstandabilityTheoretical optimality
LeaderStrong leader requiredCan work without stable leader
Log structureStrict orderingFlexible
ComplexityModerateHigh
Implementationsetcd, CockroachDBGoogle Chubby, Spanner

ZAB (ZooKeeper Atomic Broadcast)

Used by Apache ZooKeeper for coordination.

Leader Election

A critical subproblem in distributed systems.

Practice Exercises

  1. Conceptual: Explain why consensus requires a majority quorum. What happens with a 3-node cluster during a network partition?

  2. Raft: Trace through a Raft leader election with 5 nodes. Node 3's election timer fires first. Walk through the message exchanges.

  3. Comparison: Compare Raft and Paxos for a distributed key-value store. When would you choose one over the other?

  4. FLP Impossibility: The FLP result says deterministic consensus is impossible in asynchronous systems with crash failures. How do practical algorithms like Raft circumvent this?


What to Learn Next

-> CAP Theorem Consistency models, availability, and partition tolerance.

-> Data Replication Leader-follower, multi-leader, and conflict resolution.

-> Consistent Hashing Hash rings, virtual nodes, and load distribution.

-> Databases SQL vs NoSQL, indexing, replication, and sharding.

-> Service Mesh Envoy, Istio, and sidecar proxy patterns.

-> Event-Driven Architecture Event sourcing, CQRS, and saga patterns.

Need Expert System Design Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement