Skill — Distributed Consensus
When this skill activates
Any task involving strong consistency requirements in distributed systems,
leader election, distributed locks, quorum-based reads/writes,
or preventing split-brain scenarios in clustered systems.
Mandatory actions when this skill is active
Before writing any code
- Confirm consensus is actually needed (not everything requires strong consistency).
- Identify what data requires linearizability vs what can be eventually consistent.
- Choose existing implementation (etcd, ZooKeeper, Consul) — never build your own.
- Size the cluster (odd numbers only: 3 for most, 5 for critical).
During implementation
- Use established consensus systems (etcd/ZooKeeper/Consul) — do NOT implement Raft/Paxos yourself.
- Implement fencing tokens for all distributed locks.
- Handle network partitions explicitly (what happens when consensus is lost?).
- Set appropriate timeouts for leader election (not too short = flapping, not too long = unavailability).
- Use consensus ONLY for metadata/coordination — never for high-throughput data plane.
After implementation
- Test split-brain scenarios (network partition between nodes).
- Verify leader election completes within acceptable time.
- Confirm fencing tokens prevent stale operations.
- Load test to ensure consensus doesn't become a bottleneck.
- Monitor cluster health (leader stability, replication lag).
Raft Consensus (Most Common)
How It Works
- Leader Election: Nodes start as followers. If no heartbeat from leader within timeout, a follower becomes a candidate and requests votes.
- Log Replication: Leader receives writes, appends to log, replicates to followers.
- Commitment: Entry is committed when majority (quorum) acknowledges.
- Safety: Only one leader per term. Committed entries are never lost.
Key Properties
- Strong leader (all writes go through leader).
- Leader elected by majority vote.
- Log entries committed when replicated to majority.
- Cluster tolerates (N-1)/2 failures (3 nodes tolerates 1, 5 tolerates 2).
Quorum Mathematics
Formulas
N = total nodes
W = write quorum (nodes that must acknowledge a write)
R = read quorum (nodes that must respond to a read)
Strong consistency: R + W > N
Write availability: W ≤ N (can tolerate N-W failures for writes)
Read availability: R ≤ N (can tolerate N-R failures for reads)
Common Configurations
| N |
W |
R |
Consistency |
Write Tolerance |
Read Tolerance |
| 3 |
2 |
2 |
Strong |
1 failure |
1 failure |
| 5 |
3 |
3 |
Strong |
2 failures |
2 failures |
| 5 |
3 |
1 |
Eventual reads |
2 failures |
4 failures |
Split-Brain Prevention
The Problem
Network partition can create two groups, each believing it's the leader.
Solutions
- Majority quorum: Only the partition with majority can elect a leader.
- Fencing tokens: Monotonically increasing token with every lock acquisition. Storage rejects operations with stale tokens.
- Epoch numbers: Leader increments epoch on election. Older epochs are rejected.
- External witness: Third-party arbiter breaks ties (but introduces dependency).
Fencing Token Pattern
Client A acquires lock → token=42
Client A pauses (GC, network)
Client B acquires lock → token=43
Client A resumes, sends write with token=42
Storage rejects: 42 < 43 (stale token)
When to Use Consensus
Good Use Cases
- Leader election for worker coordination.
- Distributed configuration management.
- Service discovery and membership.
- Distributed locks (with fencing tokens).
- Metadata storage (small, infrequently written).
Anti-Patterns (DON'T use consensus for)
- High-throughput data writes (consensus = bottleneck at ~10K writes/sec).
- Large data storage (consensus stores small metadata, not big data).
- Read-heavy workloads (use eventual consistency + caching instead).
- Every database write (use consensus for critical metadata only).
Practical Systems
etcd
- Raft-based, Kubernetes uses it for cluster state.
- Key-value store with watch capabilities.
- Best for: service discovery, config, leader election.
ZooKeeper
- ZAB protocol (similar to Raft).
- Hierarchical namespace with ephemeral nodes.
- Best for: distributed locks, barriers, leader election.
Consul
- Raft-based, service mesh integration.
- Service discovery + health checking + KV store.
- Best for: service mesh, multi-datacenter coordination.
Failure Scenarios to Test
- Leader crash: New leader elected within timeout. No committed data lost.
- Network partition (minority isolated): Majority continues. Minority becomes read-only or unavailable.
- Network partition (even split): Neither side has majority → cluster unavailable until partition heals.
- Slow node: Doesn't affect consensus (majority can proceed without it).
- Clock skew: Raft uses logical clocks — physical clock skew shouldn't matter.
- Disk full on leader: Leader steps down, new election.
Self-check
1---2name: distributed-consensus3description: Skill — Distributed Consensus4---56# Skill — Distributed Consensus78## When this skill activates9Any task involving strong consistency requirements in distributed systems,10leader election, distributed locks, quorum-based reads/writes,11or preventing split-brain scenarios in clustered systems.1213## Mandatory actions when this skill is active1415### Before writing any code161. Confirm consensus is actually needed (not everything requires strong consistency).172. Identify what data requires linearizability vs what can be eventually consistent.183. Choose existing implementation (etcd, ZooKeeper, Consul) — never build your own.194. Size the cluster (odd numbers only: 3 for most, 5 for critical).2021### During implementation22- Use established consensus systems (etcd/ZooKeeper/Consul) — do NOT implement Raft/Paxos yourself.23- Implement fencing tokens for all distributed locks.24- Handle network partitions explicitly (what happens when consensus is lost?).25- Set appropriate timeouts for leader election (not too short = flapping, not too long = unavailability).26- Use consensus ONLY for metadata/coordination — never for high-throughput data plane.2728### After implementation29- Test split-brain scenarios (network partition between nodes).30- Verify leader election completes within acceptable time.31- Confirm fencing tokens prevent stale operations.32- Load test to ensure consensus doesn't become a bottleneck.33- Monitor cluster health (leader stability, replication lag).3435## Raft Consensus (Most Common)3637### How It Works381. **Leader Election**: Nodes start as followers. If no heartbeat from leader within timeout, a follower becomes a candidate and requests votes.392. **Log Replication**: Leader receives writes, appends to log, replicates to followers.403. **Commitment**: Entry is committed when majority (quorum) acknowledges.414. **Safety**: Only one leader per term. Committed entries are never lost.4243### Key Properties44- Strong leader (all writes go through leader).45- Leader elected by majority vote.46- Log entries committed when replicated to majority.47- Cluster tolerates (N-1)/2 failures (3 nodes tolerates 1, 5 tolerates 2).4849## Quorum Mathematics5051### Formulas52```53N = total nodes54W = write quorum (nodes that must acknowledge a write)55R = read quorum (nodes that must respond to a read)5657Strong consistency: R + W > N58Write availability: W ≤ N (can tolerate N-W failures for writes)59Read availability: R ≤ N (can tolerate N-R failures for reads)60```6162### Common Configurations63| N | W | R | Consistency | Write Tolerance | Read Tolerance |64|---|---|---|-------------|-----------------|----------------|65| 3 | 2 | 2 | Strong | 1 failure | 1 failure |66| 5 | 3 | 3 | Strong | 2 failures | 2 failures |67| 5 | 3 | 1 | Eventual reads | 2 failures | 4 failures |6869## Split-Brain Prevention7071### The Problem72Network partition can create two groups, each believing it's the leader.7374### Solutions751. **Majority quorum**: Only the partition with majority can elect a leader.762. **Fencing tokens**: Monotonically increasing token with every lock acquisition. Storage rejects operations with stale tokens.773. **Epoch numbers**: Leader increments epoch on election. Older epochs are rejected.784. **External witness**: Third-party arbiter breaks ties (but introduces dependency).7980### Fencing Token Pattern81```82Client A acquires lock → token=4283Client A pauses (GC, network)84Client B acquires lock → token=4385Client A resumes, sends write with token=4286Storage rejects: 42 < 43 (stale token)87```8889## When to Use Consensus9091### Good Use Cases92- Leader election for worker coordination.93- Distributed configuration management.94- Service discovery and membership.95- Distributed locks (with fencing tokens).96- Metadata storage (small, infrequently written).9798### Anti-Patterns (DON'T use consensus for)99- High-throughput data writes (consensus = bottleneck at ~10K writes/sec).100- Large data storage (consensus stores small metadata, not big data).101- Read-heavy workloads (use eventual consistency + caching instead).102- Every database write (use consensus for critical metadata only).103104## Practical Systems105106### etcd107- Raft-based, Kubernetes uses it for cluster state.108- Key-value store with watch capabilities.109- Best for: service discovery, config, leader election.110111### ZooKeeper112- ZAB protocol (similar to Raft).113- Hierarchical namespace with ephemeral nodes.114- Best for: distributed locks, barriers, leader election.115116### Consul117- Raft-based, service mesh integration.118- Service discovery + health checking + KV store.119- Best for: service mesh, multi-datacenter coordination.120121## Failure Scenarios to Test1221231. **Leader crash**: New leader elected within timeout. No committed data lost.1242. **Network partition (minority isolated)**: Majority continues. Minority becomes read-only or unavailable.1253. **Network partition (even split)**: Neither side has majority → cluster unavailable until partition heals.1264. **Slow node**: Doesn't affect consensus (majority can proceed without it).1275. **Clock skew**: Raft uses logical clocks — physical clock skew shouldn't matter.1286. **Disk full on leader**: Leader steps down, new election.129130## Self-check131- [ ] Consensus is genuinely needed (not over-engineering eventual consistency).132- [ ] Using established system (etcd/ZooKeeper/Consul) — not custom implementation.133- [ ] Cluster size is odd (3 or 5).134- [ ] Fencing tokens implemented for distributed locks.135- [ ] Network partition behavior tested and documented.136- [ ] Consensus used only for coordination/metadata (not data plane).137- [ ] Leader election timeout tuned (not too short, not too long).138- [ ] Monitoring: leader stability, replication lag, cluster health.