Distributed Systems: Replication, CAP/PACELC, Logical Clocks, and Raft Consensus
Leslie Lamport famously remarked that a distributed system is one in which the failure of a computer you did not even know existed can render your own computer unusable. Whenever data and state span multiple servers across racks or geographic regions, engineers confront three unbreakable physical realities: networks drop and delay packets arbitrarily (asynchrony), individual nodes crash or pause during garbage collection (partial failure), and physical quartz clocks drift apart across machines.
Key Highlights
- Eric Brewer’s CAP Theorem (proven by Gilbert & Lynch) states that during a Network Partition (`P`), a replicated store must choose between Linearizable Consistency (`CP` — rejecting minority-side writes) and 100% Availability (`AP` — accepting divergent writes resolved later).
- Daniel Abadi’s `PACELC` extends CAP to normal healthy operation: even without partitions (`E`), systems trade off cross-region replication Latency (`L`) against strong Consistency (`C`).
- Consensus protocols (`Paxos` and Ongaro & Ousterhout’s `Raft` inside `etcd`, CockroachDB, TiKV, and Kafka KRaft) elect a single term-numbered Leader and commit log entries once a strict majority quorum (`⌊N/2⌋ + 1`, e.g., 2 of 3 or 3 of 5 nodes) persists them.
- Avoid distributed Two-Phase Commit (`2PC`) locks across external microservices; use Transactional Outbox events + Idempotency Keys + compensating Sagas instead.
CAP/PACELC Trade-Offs and How Raft Prevents Split-Brain Corruption
Why do Raft and Paxos clusters always deploy **odd** node counts (3 or 5 nodes, never 4)? A 3-node cluster tolerates 1 failed node (quorum = 2), and a 4-node cluster ALSO tolerates only 1 failed node (quorum = 3)—meaning a 4th node slows down majority writes without improving fault tolerance! Requiring `⌊N/2⌋ + 1` votes guarantees mathematically by pigeonhole intersection that two separated network islands can never simultaneously elect two valid leaders ("split brain") and corrupt cluster state.
Even after electing a leader, network delays or JVM/Go garbage-collection pauses can freeze an old leader for five seconds while the remaining quorum elects a new leader; **fencing tokens** (monotonically increasing epoch/term numbers verified by downstream storage) block zombie writes from stale leaders.
| Consistency Tier | Example Systems | Read / Write Behavior | Best Production Fit |
|---|---|---|---|
| Linearizable Raft/Paxos (CP) | etcd, ZooKeeper, Spanner, CockroachDB | Majority quorum write; real-time global order | Kubernetes cluster state, distributed locks, financial ledgers |
| Causal / Tunable Quorum | MongoDB (majority), DynamoDB (strong) | R + W > N overlapping read/write quorums | Session-consistent user profiles & shopping carts |
| Eventual + CRDTs (AP) | Cassandra (ONE), Riak, Collaborative Docs | Accepts local writes offline; deterministic merge | Collaborative text editors, edge metrics, high-availability caches |
Idempotency Keys, Transactional Outbox, and Retry Storm Defense
When an HTTP timeout occurs after calling `charge_card()`, the caller **cannot know** whether the network dropped the request on the way there or dropped the `200 OK` receipt on the way back. Passing a client-generated UUID `Idempotency-Key` stored atomically alongside the ledger row allows safe retries without double-charging customers—and adding exponential backoff with full random jitter prevents synchronized client retries ("thundering herds") from knocking recovering servers back offline.
- Never compare raw `System.currentTimeMillis()` wall-clock timestamps across two different servers to order concurrent events—NTP clock jumps can move wall time backward! Use monotonic clocks locally and Hybrid Logical Clocks / sequence IDs across nodes.
- Use the Transactional Outbox pattern (writing business row + outbox event row inside ONE local SQL transaction, relayed to Kafka/queues) to eliminate dual-write bugs.
- Test distributed failure modes deliberately in staging (Jepsen-style network partitions, packet delay, and leader kills).
References & Sources
- Lamport, Leslie (1978) — Time, Clocks, and the Ordering of Events in a Distributed System, Communications of the ACM.
- Ongaro, Diego; Ousterhout, John (2014) — In Search of an Understandable Consensus Algorithm (Raft), USENIX ATC.
- Kleppmann, Martin — Designing Data-Intensive Applications, O’Reilly Media.