🗳️ Distributed Consensus: Raft & Paxos Protocols
1️⃣ The Consensus Challenge & Replicated State Machines
Definition: Distributed Consensus wo fundamental process hai jisme multiple asynchronous, unreliable computing nodes ek single shared data value ya sequence of actions par agree karte hain—bina kisi central authority ke, aur unreliable network drops ya partial node crashes ke bawajood.
Deterministic state machines ka ek group agar identical initial state se start kare aur identical sequence of inputs (replicated log) execute kare, toh wo hamesha identical final output states produce karega. Consensus protocol ka primary goal isi Ordered Append-Only Log ko nodes ke beech replicate aur synchronize rakhna hota hai.
| Consensus Invariant | Formal Safety Definition |
|---|---|
| Agreement | Sabhi correct non-failing nodes ek hi value par decide karte hain (No conflicting decisions). |
| Validity | Decide ki gayi value kisi participant node dwara hi propose hui honi chahiye (No fabricated state). |
| Termination (Liveness) | Majority quorum nodes operational hone par consensus eventually complete hota hai. |
2️⃣ Paxos Demystified: Proposers & Acceptors
Leslie Lamport dwara formulate kiya gaya formal algorithm jisme nodes specific roles play karte hain:
| Role | Responsibilities in Cluster |
|---|---|
| Proposer | Clients se request receive karta hai, proposal number $N$ generate karta hai aur Acceptors ko propose karta hai. |
| Acceptor | Cluster state ki memory store karta hai, proposals par vote karta hai aur Quorum form karta hai. |
| Learner | Acceptors dwara finalize ho chuki committed values ko read karta hai (Zero voting power). |
# The Two Phases of Basic Paxos:
Phase 1a (Prepare): Proposer chooses proposal number n → Sends Prepare(n) to majority of Acceptors.
Phase 1b (Promise): Acceptors promise not to accept any future proposals with number < n.
Phase 2a (Accept): Proposer sends Accept(n, value) to majority of Acceptors.
Phase 2b (Accepted): Acceptors accept the value unless they promised a higher proposal number.
3️⃣ The Raft Consensus Algorithm: Core Decomposition
Paxos ki implementation complexity ko replace karne ke liye Stanford researchers (Ongaro & Ousterhout) ne Raft banaya, jo consensus ko 3 independent sub-problems me decompose karta hai:
| Node State in Raft | Role & Transition Conditions |
|---|---|
| Follower | Passive mode. Leader se AppendEntries heartbeats sunta hai. Timeout hone par Candidate banta hai. |
| Candidate | Election trigger karta hai, Term number increment karta hai aur RequestVote RPCs broadcast karta hai. |
| Leader | 100% client read/writes handle karta hai, log entries stream karta hai aur regular heartbeats send karta hai. |
4️⃣ Raft Leader Election & Election Timeouts
Split votes avoid karne ke liye Raft Randomized Election Timeouts (typically 150ms - 300ms) use karta hai:
# Raft Election Lifecycle:
1. Follower node's randomized heartbeat timer expires (e.g. 210ms without leader ping).
2. Node transitions: Follower → Candidate.
3. Increments currentTerm (e.g. Term 4 → Term 5) and votes for itself.
4. Broadcasts RequestVote(term=5, candidateId=Node-1, lastLogIndex=42, lastLogTerm=4) RPCs.
5. If majority quorum votes received (e.g. 3 out of 5 nodes):
→ Node becomes LEADER for Term 5 and instantly broadcasts heartbeat pings!
5️⃣ Raft Log Replication & Quorum Safety
• Agar do distinct nodes ke logs me same index aur same term ki entry hai, toh wo identical command hold karti hain.
• Agar do nodes ke log me ek entry match hoti hai, toh us index se pehle ki saari entries completely identical hongi.
• Leader Completeness: Ek candidate tabhi vote jeet sakta hai agar uska log baaki majority nodes ke logs se kam se kam equally up-to-date ho (Committed entries are never overwritten).
# AppendEntries RPC Payload structure:
AppendEntries(
term=1, # Leader's current term
leaderId="Node-1", # Client redirection identifier
prevLogIndex=104, # Index of log entry immediately preceding new ones
prevLogTerm=1, # Term of prevLogIndex entry
entries=[...], # Batch of new log records to append
leaderCommit=104 # Leader's commitIndex to notify followers
)
6️⃣ Network Partitions & Split-Brain Defenses
Agar 5-node cluster split ho jaye: Partition A (2 nodes) aur Partition B (3 nodes):
| Cluster Partition | Node Count | Can Form Quorum? | Operational Behavior |
|---|---|---|---|
| Minority (Nodes 1, 2) | 2 / 5 Nodes | NO (< 3 votes) | Leader 1 client writes reject karega ya uncommitted buffer me rakhega (No split-brain writes). |
| Majority (Nodes 3, 4, 5) | 3 / 5 Nodes | YES (≥ 3 votes) | Node 3 higher term me naya leader banega aur normal writes commit karta rahega. |
Network heal hone par purana Leader 1 naye Leader ka higher term dekhega aur automatically Follower bankar apna log reconcile kar lega.
7️⃣ Real-World Production Implementations
| System | Consensus Implementation | Core Responsibility |
|---|---|---|
| etcd (Kubernetes Core) | Raft Protocol (Go) | K8s cluster state, pod schedules, service endpoints coordination. |
| Apache Kafka (KRaft) | Custom Raft-based Event Log | Topic metadata aur partition leader election without ZooKeeper. |
| CockroachDB | Multi-Raft Groups | Har 64MB data range ka alag independent Raft consensus group. |
| Google Chubby / Spanner | Multi-Paxos + TrueTime | Global transaction serializability and distributed locks. |