Fundamentals

Distributed Consensus

Understanding consensus algorithms like Raft and Paxos.

3 min readConcepts · System Design

What is Distributed Consensus?

Distributed Consensus is the process of getting multiple nodes to agree on a single value or decision, even in the presence of failures. It's the foundation of reliable distributed systems.


Why Consensus is Hard

Challenges:

  • Nodes can crash at any time
  • Messages can be lost, delayed, or reordered
  • Network partitions split the cluster

Key Properties

PropertyMeaning
AgreementAll correct nodes decide the same value
ValidityDecided value was proposed by some node
TerminationAll correct nodes eventually decide
IntegrityEach node decides at most once

Raft Consensus

Raft is designed to be understandable. It breaks consensus into three sub-problems.

Leader Election

Normal Operation

Log Replication

Leader Log:   [1:SET x=1] [2:SET y=2] [3:SET x=3]
Follower 1:   [1:SET x=1] [2:SET y=2] [3:SET x=3] ✓
Follower 2:   [1:SET x=1] [2:SET y=2]              (catching up)

Followers replicate the leader's log in order. Committed entries are never lost.


Paxos

The original consensus algorithm. More general but harder to understand.

Roles

RoleFunction
ProposerProposes values
AcceptorVotes on proposals
LearnerLearns decided values

Two Phases


Raft vs Paxos

AspectRaftPaxos
UnderstandabilityDesigned to be simpleNotoriously complex
LeaderStrong leader requiredOptional
Log orderingStrict sequentialFlexible
ImplementationMany production systemsFewer implementations
PerformanceGood for most casesBetter in some edge cases

Real-World Usage

SystemAlgorithmUsed For
etcdRaftKubernetes state, config
CockroachDBRaftDistributed SQL
ConsulRaftService discovery
Google SpannerPaxosGlobal SQL database
Apache ZooKeeperZAB (Paxos-like)Coordination

Quorum Requirements

Cluster size: 2f + 1 nodes
Tolerates: f failures

3 nodes → tolerates 1 failure
5 nodes → tolerates 2 failures
7 nodes → tolerates 3 failures
Cluster SizeMajorityFailures Tolerated
321
532
743

Interview Tips

  • Explain why consensus is hard (FLP impossibility)
  • Know Raft's three sub-problems: leader election, log replication, safety
  • Understand quorum: majority of 2f+1 nodes
  • Compare Raft vs Paxos (Raft is simpler)
  • Give examples: etcd, CockroachDB, ZooKeeper

Finished reading?

Mark it done to track your progress in Concepts.