Implementing Consensus Algorithms

1. Understanding Consensus Problem

PropertyDefinition
AgreementAll correct nodes decide same value
ValidityDecided value was proposed by some node
TerminationEvery correct node eventually decides
IntegrityEach node decides at most once
FLP ResultImpossible deterministically in async with 1 crash

2. Implementing Paxos Algorithm (basic)

PhaseAction
1a Prepare(n)Proposer picks ballot n; sends to acceptors
1b Promise(n, v?)Acceptor promises not to accept < n; returns prior accepted
2a Accept(n, v)If majority promised, proposer sends value (highest prior or own)
2b Accepted(n, v)Acceptor accepts unless promised higher
DecideOnce majority accepted, value chosen
Warning: Basic Paxos decides one value; real systems need Multi-Paxos for log replication.

3. Implementing Multi-Paxos

OptimizationDetail
Stable leaderSkip Phase 1 once elected; reuse ballot
Per-slot decisionEach log index = one Paxos instance
PipeliningMultiple slots in flight
Used byChubby, Spanner, Megastore

4. Implementing Raft Consensus

ComponentDetail
RolesLeader, Follower, Candidate
TermMonotonic logical epoch
ElectionRandomized timeout (150-300ms) → candidate
RPCsRequestVote, AppendEntries
Used byetcd, Consul, CockroachDB, TiKV, RethinkDB

5. Implementing Raft Log Replication

Raft Append Entries Flow

  1. Leader receives client command, appends to local log
  2. Leader sends AppendEntries(term, prevIndex, prevTerm, entries[], leaderCommit)
  3. Follower verifies log matching property; replies success/failure
  4. Once majority ack, leader advances commit index
  5. Leader notifies followers via next AppendEntries
  6. State machines apply committed entries in order

6. Implementing Raft Membership Changes

MethodDetail
Joint consensusTransition Cold,new requires majority in both
Single-server changeAdd/remove one at a time (etcd default)
Learner roleCatches up before voting (etcd v3.4+)

7. Understanding Viewstamped Replication

PropertyDetail
Predates PaxosLiskov & Oki, 1988
ViewsEquivalent to Raft terms
Primary-backup with view changesSimilar to Raft
Used byInfluenced Harp, BFT variants

8. Understanding Zab Protocol (ZooKeeper)

PropertyDetail
GoalAtomic broadcast for primary-backup
PhasesDiscovery, synchronization, broadcast
zxid(epoch, counter) — total order
Order guaranteeFIFO from leader

9. Handling Split-Brain Scenarios

MechanismHow
Quorum requirementMajority needed; minority cannot commit
Fencing tokensMonotonic epoch invalidates stale leader
STONITH"Shoot The Other Node In The Head" — power off old leader
Lease + clock boundLeader lease expires before new election

10. Implementing Quorum-Based Consensus

NQuorum (⌊N/2⌋+1)Failures Tolerated
321
532
743
Note: Odd cluster sizes are preferred — adding an even node increases quorum without increasing fault tolerance.

11. Understanding Byzantine Fault Tolerant Consensus (PBFT)

PropertyValue
AssumptionUp to f byzantine nodes
Required nodes3f + 1 total
PhasesPre-prepare → prepare → commit
Use casesPermissioned blockchains, high-security systems
VariantsTendermint, HotStuff, LibraBFT