Distributed Consistency, Consensus Protocols & Logical Clocks
Scaling beyond a single server machine introduces distributed network partitions, unpredictable message latency, clock drift, and split-brain risks. Achieving reliable state across clusters requires formal mathematical consensus primitives.
1. The CAP Theorem & PACELC Refinement
- • PC/EC (e.g. Spanner, HBase): Chooses Consistency under both partition and normal operation.
- • PA/EL (e.g. Cassandra, DynamoDB): Chooses Availability during partitions, low Latency during normal operations.
2. Leaderless Quorum Replication (\(R + W > N\))
Dynamo-style databases (Cassandra, Amazon Dynamo) use leaderless replication configured by three parameters:
Replication Factor (e.g. 3 copies)
Write Quorum ACK count
Read Quorum Node count
If \(N=3, W=2, R=2\), the read set and write set must overlap by at least one node containing the newest version. Read Repair and Anti-Entropy (using Merkle Trees) synchronize out-of-date nodes in the background.
3. Consistent Hashing with Virtual Nodes
Maps keys and server nodes to a circular hash ring (\(2^{32}-1\)). When a server node is added or removed, only \(\frac{K}{N}\) keys need remapping, eliminating total cache flush.
4. The Raft Consensus Protocol
Raft enforces state machine replication across a cluster by decomposing consensus into three distinct sub-problems:
RequestVote RPCs. A leader requires majority votes (\(\lfloor N/2 \rfloor + 1\)).
AppendEntries RPCs. Once entries are replicated on majority nodes, the leader commits and notifies followers.
5. Logical Clocks & Google Spanner TrueTime
NTP (Network Time Protocol) clock drift across servers makes wall-clock timestamps unsafe for transaction ordering.
Monotonically increasing logical counters. Vector clocks track causality across nodes: \(V_i[j]\) represents Node \(i\)'s knowledge of Node \(j\)'s clock.
Uses GPS receivers and atomic clocks in datacenters to bound physical clock uncertainty: \(\text{TT.now}() \implies [t_{earliest}, t_{latest}]\) with \(\epsilon \le 7\text{ms}\). Enables external consistency without inter-datacenter communication locking.