Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Distributed Systems Cheat Sheet

CAP Theorem

Pick 2 of 3: Consistency, Availability, Partition Tolerance
Since P is inevitable → Choose CP or AP

CP: ZooKeeper, HBase, MongoDB (strong consistency, may reject requests)
AP: Cassandra, DynamoDB, CouchDB (always available, eventual consistency)

Consistency Models

ModelGuaranteeExample
LinearizableReal-time orderingZooKeeper
SequentialTotal order, no real-timeRaft
CausalCause-effect preservedMongoDB
Read-your-writesSee own writesUser profiles
EventualConverges eventuallyCassandra

Consensus Algorithms

AlgorithmFault ToleranceComplexityUse
Paxosf < n/2Hard to understandTheory
Raftf < n/2Understandableetcd, CockroachDB
ZABf < n/2Similar to RaftZooKeeper
PBFTf < n/3 (Byzantine)O(n²)Blockchain

Raft States

Follower → (timeout) → Candidate → (majority votes) → Leader
Leader → (term higher) → Follower
Candidate → (higher term seen) → Follower

Log Replication: Leader receives → appends → replicates → commits (majority ack)

Replication Strategies

StrategyLatencyConsistencyData Loss Risk
SynchronousHighStrongNone
AsynchronousLowEventualPossible
Semi-syncMediumHybridMinimal
ChainVariableStrongDepends on position
Quorum (NRW)ConfigurableTunableN-W+R > N ensures consistency

Quorum Formula

N = total replicas
W = write quorum
R = read quorum

Strong consistency if: W + R > N
Example: N=3, W=2, R=2 → guaranteed consistency

Partitioning

StrategyProsCons
HashEven distributionRange queries hard
RangeRange queries efficientHotspots possible
Consistent HashingMinimal redistributionComplexity

Consistent Hashing

Ring of 0 to 2^32 - 1
Server → hash → position on ring
Key → hash → walk clockwise → nearest server
Virtual nodes: multiple positions per server for balance

Failure Detection

Heartbeat: Periodic "I'm alive" messages
Timeout: No heartbeat → suspect failure
Gossip: Peer-to-peer state propagation
Phi Accrual: Adaptive failure detector (Cassandra)

Distributed Transactions

ProtocolBlockingRoundsUse
2PCYes (coordinator failure)2Traditional DB
3PCNo (theoretically)3Rarely used
SagaNoN (compensating)Microservices
TCCNo3Business transactions

Message Delivery Guarantees

GuaranteeMeaningImplementation
At-most-onceMay lose messagesFire and forget
At-least-onceMay duplicateRetry + ack
Exactly-onceNo loss, no dupIdempotent + dedup

Key Distributed Systems

SystemTypeConsensusCAP
KafkaLog/messagingISR (Raft-like)AP→CP
CassandraWide-columnGossipAP
ZooKeeperCoordinationZABCP
etcdKV storeRaftCP
CockroachDBSQLRaftCP
DynamoDBKV/documentVector clocksAP

Interview Quick Tips

  1. Always discuss failure modes (network partition, node crash, split brain)
  2. Explain consistency trade-offs clearly
  3. Know when to use CP vs AP
  4. Draw the architecture with data flow arrows
  5. Mention monitoring, alerting, and recovery