STACK
Ch 12 · Distributed Systems
Chapter 12 · The hard part

Distributed Systems — where intuition lies to you

Partial failure is the norm. Time is unreliable. Two events that look ordered to one node look concurrent to another. This chapter is the survival kit.

12.1 · The 8 fallacies (Deutsch / Gosling)

  1. 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.

12.2 · CAP & PACELC

CAP — pick 2 during partition Consistency Availability Partition tolerance CP — strong, may reject AP — always responds, may diverge

PACELC extends CAP: in the absence of partitions, you still trade Latency against Consistency. Spanner is CP/EC; Dynamo is AP/EL.

12.3 · Consistency models — strongest → weakest

ModelGuaranteeExample
LinearizableReal-time global order.Spanner, etcd, Zookeeper.
SequentialSingle global order, not necessarily real-time.Kafka per partition.
Causal"Happens-before" preserved everywhere.Bayou, Riak.
Read-your-writesSee your own writes.Sticky-session systems.
Monotonic readsNever see older data after newer.Many caches.
EventualReplicas converge if writes stop.DynamoDB default, Cassandra.

12.4 · Time & clocks

ClockNotes
Physical (wall)Drifts, skews, can jump backwards. Never trust for ordering.
Lamport logicalCounter incremented on send/receive; gives happens-before.
Vector clockPer-node counters; can detect concurrency.
HLC (Hybrid Logical Clock)Physical + logical; CockroachDB, YugabyteDB.
TrueTimeSpanner's bounded uncertainty via atomic clocks + GPS.

12.5 · Replication topologies

TopologyTrade-off
Single-leaderEasy reasoning; write bottleneck; failover needed.
Multi-leaderHigher write availability; conflict resolution (LWW, CRDTs).
Leaderless (Dynamo)Quorum reads/writes; tunable; eventual.
Sync vs asyncSync = durable, slower; async = fast, possible loss on failover.

12.6 · Consensus — Raft & Paxos

  • Paxos — the original; famously hard to implement.
  • Raft — designed for understandability. etcd, Consul, TiKV, RethinkDB.
  • Multi-Paxos / EPaxos / Raft variants for throughput.
  • ZAB — Zookeeper's atomic broadcast.
  • You usually don't implement consensus; you delegate to etcd / Zookeeper / Consul.

12.7 · Sharding strategies

  • Hash: shard = hash(key) % N. Uniform; painful to reshard.
  • Range: contiguous keys per shard. Range scans easy; hotspots if skewed.
  • Geo: per region. Low latency; cross-region queries slow.
  • Consistent hashing + virtual nodes: minimal data movement on scale changes.
  • Live resharding: dual-write → backfill → switch reads → drop. Days of careful work.

12.8 · Distributed transactions — and why we mostly avoid them

  • 2PC / 3PC — coordinator + participants; blocking on failure.
  • XA — JTA / JDBC distributed transactions; legacy enterprise.
  • Modern stacks prefer sagas, the outbox pattern, idempotency, and at-least-once + dedup.

12.9 · Sagas

StyleHow
ChoreographyEach service reacts to events; decoupled, harder to debug.
OrchestrationA coordinator (Temporal, Workflow) drives steps; easier to reason about.

Every step has a compensating action. Booking → payment fails → cancel booking.

12.10 · CDC & event-driven

  • Change Data Capture (Debezium, Postgres logical replication) emits DB row changes as events.
  • Enables outbox without polling, data warehouses, cache invalidation, search indexes.
  • Pairs with Kafka for replay.

12.11 · "Exactly-once" — what it really means

  1. Producer attaches a stable message ID.
  2. Broker supports idempotent producer + transactions (Kafka, Pub/Sub).
  3. Consumer records processed IDs to drop duplicates.

The effect happens once even when the network re-delivers many times. Networks don't actually deliver exactly once.

12.12 · Patterns to recognise

Leader election

etcd/Consul/Zookeeper; never DIY.

Quorum R/W

R + W > N for strong reads.

CRDTs

Mergeable types for offline-first / multi-leader.

Gossip

Epidemic-style membership & failure detection.

Vector clocks

Concurrency vs causality detection.

Hinted handoff

Survive transient failures of a replica.

Read repair

Fix stale replicas during reads.

Anti-entropy / Merkle trees

Background reconciliation.

12.13 · Self-check

Q1 · "Exactly-once" in a broker means…
Bytes are sent once on the network
The effect occurs once; dedup absorbs duplicates
Magical guarantee from TCP
Only possible with mTLS