When a single bottleneck threatens end-to-end availability, never commit log entries from a previous term by counting replicas alone (Figure 8 bug); a leader may only advance `commitIndex` for entries from its `currentTerm`, and linearizable reads require `ReadIndex` quorum heartbeats.
TL;DR30-second simplified summary▾
Core Mechanics: A 5-node metadata store (etcd/Consul-style) across 3 AZs (15 ms cross-AZ RTT) relies on Raft for linearizable consensus.
Visual & Structural Flow: Depicts Why Previous-Term Entries Cannot Commit by Replica Count Alone (S1 (Term 2) -> S2 & S3 -> S5 (Term 3 log)); Follower, Candidate, and Leader Safety Transitions (FOLLOWER -> CANDIDATE -> LEADER).
Trade-Offs & Production: Skipping fsync() on currentTerm, votedFor, and log[] before replying to RequestVote or AppendEntries allows a rebooted node to vote twice in the same term (two leaders in one term). Production Raft engines (etcd/tikv) enforce strict safety with bounded 150–300 ms randomized election timeouts and 50 ms heartbeats: Log Up-to-Date Election Restriction.
Failure Modes & Application: Raft is the standard consensus protocol for strongly consistent control planes and sharded storage: Kubernetes etcd & Consul: Stores cluster state, service discovery, and leader leases.
Why subtle Raft bugs silently corrupt distributed locks and metadata.
Why this matters: The mechanism: understand how Why subtle Raft bugs silently corrupt distributed locks and metadata works from first principles.
Think of Why subtle Raft bugs silently corrupt distributed locks and metadata as the intuition layer for Raft Consensus & Leader Election Safety: The mechanism: understand how Why subtle Raft bugs silently corrupt distributed locks and metadata works from first principles.
A 5-node metadata store (etcd/Consul-style) across 3 AZs (15 ms cross-AZ RTT) relies on Raft for linearizable consensus. If a leader commits entries from a previous term merely by counting replicas, or serves reads without verifying it is still the active quorum leader, a crash or asymmetric partition causes split-brain and lost committed keys.
The Raft Figure 8 anomaly and stale reads from deposed leaders.
Why this matters: The problem: see what breaks without The Raft Figure 8 anomaly and stale reads from deposed leaders under real concurrency or scale.
Two classic bugs break Raft State Machine Safety:
- Figure 8 Overwrite: Leader
S1(term 2) replicates entrykto{S1, S2, S3}and crashes before committing it.S5(which missedk, but had a higher-term entry from term 3) wins election in term 4 and overwritesk—even ifkwas already on a majority! - Deposed Leader Stale Reads: A partitioned old leader continues serving its local in-memory state machine before its heartbeat timeout fires.
Why Previous-Term Entries Cannot Commit by Replica Count Alone
A leader may only advance commitIndex for entries from its currentTerm; older-term entries commit only indirectly.
- Problem
- How to implement Raft Consensus & Leader Election Safety without hitting unbounded tail latency, state divergence, or operational fragility.
- Naive Solution
- Use a single unpartitioned path with unbounded retries and synchronous global coordination.
- Why It Fails
- Global coordination serializes throughput under load, while unbounded retries amplify transient stalls into full outages.
- Alternatives
- Compare synchronous strict-consistency paths against bounded asynchronous replication, local caching, or partitioned ownership.
- Engineering Decision
- When a single bottleneck threatens end-to-end availability, never commit log entries from a previous term by counting replicas alone (Figure 8 bug); a leader may only advance
commitIndexfor entries from itscurrentTerm, and linearizable reads requireReadIndexquorum heartbeats. - Trade-Offs
- Gains predictable p99 latency and fault isolation in exchange for explicit bounding, observability, and slightly higher implementation complexity.
When Raft Consensus & Leader Election Safety experiences a sudden 5x traffic spike alongside a slow downstream dependency, which engineering choice best protects system stability?
Why async WAL writes and unverified leader reads break linearizability.
Why this matters: The problem: see what breaks without Why async WAL writes and unverified leader reads break linearizability under real concurrency or scale.
Think of Why async WAL writes and unverified leader reads break linearizability as the intuition layer for Raft Consensus & Leader Election Safety: The problem: see what breaks without Why async WAL writes and unverified leader reads break linearizability under real concurrency or scale.
Skipping fsync() on currentTerm, votedFor, and log[] before replying to RequestVote or AppendEntries allows a rebooted node to vote twice in the same term (two leaders in one term). Likewise, serving GET directly from the leader's in-memory map without a quorum check returns stale data during asymmetric partitions.
Why async WAL writes and unverified leader reads break linearizability
- Stale leader overwrites state
- Clock skew breaks lease order
- Full-table sync on reboot
- Majority quorum prevents split-brain
- Fencing tokens reject zombies
- Merkle/WAL catch-up in O(log N)
Enterprise solution: Five Raft Invariants, currentTerm Commit, and ReadIndex.
Why this matters: Operational control: configure bounds, failure handling, and safe production defaults.
Production Raft engines (etcd/tikv) enforce strict safety with bounded 150–300 ms randomized election timeouts and 50 ms heartbeats:
- Log Up-to-Date Election Restriction:
RequestVoterejects any candidate whose(lastLogTerm, lastLogIndex)is behind the voter's. - currentTerm Commit Rule: Leader advances
commitIndexonly whenlog[N].term == currentTermand a majority (3/5) has persisted it (fsync2–5 msbatched). - Linearizable
ReadIndex(Zero Disk Writes): Leader recordsreadIndex = commitIndex, sends a heartbeat round to confirm quorum, waits forlastApplied >= readIndex, and serves the read locally.
Follower, Candidate, and Leader Safety Transitions
Randomized 150-300ms election timeouts avoid split votes while ReadIndex guarantees linearizable reads without disk writes.
Where Raft consensus powers critical infrastructure.
Why this matters: Scope boundary: identify where this pattern fits best and where simpler or async alternatives win.
Raft is the standard consensus protocol for strongly consistent control planes and sharded storage:
- Kubernetes
etcd& Consul: Stores cluster state, service discovery, and leader leases. - Distributed Locks & Fencing Tokens: Generates monotonically increasing
term+indexfencing tokens. - TiKV / CockroachDB (Multi-Raft): Runs thousands of independent Raft groups per node for horizontal scale.
Practice: Make the trade-off yourself.
How does Raft's `ReadIndex` protocol serve linearizable reads without writing a no-op entry to the disk WAL on every read?