Distributed Systems Theory
The fallacies of distributed computing, failure models, time and clocks (NTP, Lamport and vector clocks, hybrid logical clocks, TrueTime), consensus with Raft and Paxos, leader election and fencing tokens, quorums, gossip and failure detection, consistency models from linearizable to eventual, CRDTs, and distributed transactions (2PC vs sagas).
The eight fallacies of distributed computing
- The network is reliable. 2. Latency is zero. 3. Bandwidth is infinite. 4. The network is secure. 5. Topology doesn't change. 6. There is one administrator. 7. Transport cost is zero. 8. The network is homogeneous.
Every design decision in this chapter exists because these are false.
Failure models
| Model | Meaning |
|---|---|
| Crash-stop | a node fails and never returns |
| Crash-recovery | nodes crash and restart with durable state (most real systems) |
| Omission | messages lost |
| Byzantine | nodes behave arbitrarily or maliciously (blockchains, aerospace) |
| Network partition | groups of nodes can't communicate |
| Gray failure | partially working: slow disk, flapping NIC — hardest to detect |
You cannot distinguish a slow node from a dead one (FLP impossibility: deterministic consensus is impossible in a fully asynchronous system with even one crash) — practical systems use timeouts and accept occasional false suspicion.
Time and clocks
| Clock | Gives | Used by |
|---|---|---|
| Wall clock (NTP) | approx. real time, can jump | logs, TTLs (with care) |
| Monotonic clock | durations on one machine | timeouts, latency measurement |
| Lamport clock | total order consistent with causality | simple ordering |
| Vector clock | detects concurrent vs causally ordered | Dynamo, Riak conflict detection |
| Hybrid logical clock | wall-ish time + causality | CockroachDB, MongoDB |
| TrueTime | time interval with bounded uncertainty | Spanner (commit wait) |
Consensus: Raft
Key ideas: terms (logical epochs), randomised election timeouts (avoid split votes), majority quorums (any two majorities intersect, so a new leader always has every committed entry), log matching, snapshots for compaction, and joint consensus for membership changes.
Paxos solves the same problem (Multi-Paxos for a log); it's older and harder to understand; Raft was designed for understandability. ZAB powers ZooKeeper.
Leader election, leases and fencing
Use etcd/ZooKeeper/Consul for leader election (leases with keep-alives). Split brain — two nodes both acting as leader — is prevented by majority quorums plus fencing.
Quorums and leaderless replication
N = 3 replicas, W = 2, R = 2 → W + R = 4 > 3 → every read overlaps the latest write
write v2: [A: v2] [B: v2] [C: v1] (C missed it)
read: ask A + C → see v2 and v1 → return v2, repair C (read repair)
Gossip and failure detection
Gossip protocols: each node periodically exchanges state with a few random peers; information spreads epidemically in O(log n) rounds. Used for membership and failure detection (Cassandra, Consul's Serf/SWIM), and for disseminating cluster state. Phi accrual failure detector outputs a suspicion level instead of a binary alive/dead, adapting to network jitter. SWIM uses indirect pings via other members to reduce false positives.
Consistency models
Session guarantees (read-your-writes, monotonic reads, monotonic writes, writes-follow-reads) are often all users actually need.
CRDTs
class GCounter:
"""Grow-only counter: each node increments its own slot; merge = element-wise max."""
def __init__(self, node_id):
self.id, self.counts = node_id, {}
def increment(self, n=1):
self.counts[self.id] = self.counts.get(self.id, 0) + n
def value(self):
return sum(self.counts.values())
def merge(self, other):
for node, c in other.counts.items():
self.counts[node] = max(self.counts.get(node, 0), c)
Distributed transactions
| 2PC | Saga | |
|---|---|---|
| Atomicity | yes | eventual (via compensation) |
| Isolation | yes (locks) | no — intermediate states visible |
| Availability | blocked by any participant/coordinator failure | high |
| Latency | multiple round trips holding locks | each step local |
| Use | inside a DB / tightly coupled resources | microservices, long-running workflows |
Interview questions
Answer out loud first, then open the model answer. Mark a question confident only when you could explain it to an interviewer without notes.