Designing Distributed Consensus

1. Understanding Consensus Algorithms

AlgorithmPropertiesUsed By
PaxosCrash fault-tolerant; foundational; complexChubby, Spanner
RaftSame guarantees, easier to understandetcd, Consul, CockroachDB
ZabAtomic broadcast for primary-backupZooKeeper
VR (Viewstamped)State-machine replicationResearch; influenced Raft
EPaxosLeaderless, low latencyNiche

2. Understanding Multi-Paxos and Fast Paxos

VariantProperty
Basic PaxosOne value; 2 round trips
Multi-PaxosStable leader skips Phase 1; pipelines
Fast Paxos1 RTT in fast path; needs larger quorum
Cheap PaxosAuxiliary acceptors
GeneralizedCommutative ops can commit out of order

3. Designing Leader Election System

MechanismDetail
Bully algorithmHighest ID wins
RingToken passed in ring
Lease-basedTime-bounded leadership; renew before expiry
Coordinator-backedetcd/ZK ephemeral node
Raft-internalTerm + vote

4. Designing Distributed Locks

SystemApproachCaveat
Redis SETNX + TTLSimple lockNot safe under failover
RedlockQuorum across N RedisDisputed safety; Martin Kleppmann critique
ZooKeeper / etcdEphemeral sequential nodesStrong; higher latency
DB row lockSELECT FOR UPDATETied to DB availability
Fencing tokenMonotonic ID validated by resourceRequired for correctness

5. Designing Coordination Services

ServiceStrength
ZooKeeperMature, used by Kafka (legacy), HBase
etcdRaft-based, Kubernetes backing store
ConsulService discovery + KV + health
ChubbyGoogle internal

6. Designing Split-Brain Prevention

TechniqueDescription
Quorum (majority)Only majority side proceeds
Fencing tokensOlder leader's writes rejected
STONITH"Shoot The Other Node In The Head"
Witness/arbiter nodeTiebreaker in 2-DC setup

7. Designing Quorum-Based Systems

ParamMeaning
NReplica count
WWrites ack required
RReads ack required
StrongR + W > N
ExamplesCassandra QUORUM, Dynamo

8. Designing Conflict Resolution with CRDT

CRDT TypeOperationUse
G-CounterIncrement-onlyStats, likes
PN-CounterInc + DecVote counts
G-SetAdd onlyAppend-only logs
OR-SetAdd/removeTags, members
LWW-Element-SetTimestamp-basedSimple sets
RGA / LogootSequencesCollab text editing
Used inRiak, Redis CRDTs, Automerge, Yjs

9. Designing Distributed Configuration Management

ToolStrength
etcd / ConsulStrong consistency, watchers
Spring Cloud ConfigGit-backed config
AWS AppConfig / Parameter StoreManaged
LaunchDarkly / UnleashFeature flags

10. Designing Service Registration and Discovery

PatternTool
Client-side discoveryEureka + Ribbon, Consul agent
Server-side discoveryK8s Service, AWS ALB
DNS-basedK8s CoreDNS, SRV records
Service meshEnvoy/Istio sidecar discovery

11. Designing Fencing Tokens

Example: Fencing token in storage write

// Lock service returns monotonically increasing token
long token = lockService.acquire("resource-A");
storage.write(payload, token); // storage rejects if token < lastSeenToken
PropertyDetail
MonotonicStrictly increasing per resource
Validated by resourceStorage stores last seen token
Use casePrevents stale leader from writing

12. Designing Lease-Based Coordination

ElementDetail
LeaseTime-bounded grant of authority
RenewalHeartbeat before expiry
ClockUse monotonic clocks; account for skew
Use casesLeader election, cache leases, file locks