Distributed Systems Foundations & CAP Theorem
Networks are never reliable, latency is never zero, and bandwidth is never infinite. The CAP theorem proves that when a network partition (P) inevitably strikes, a distributed cluster must pick between Consistency (C) or Availability (A) 🌐⚖️.
class ClusterNode:
def __init__(self, node_id):
self.node_id = node_id
self.store = {}
self.is_partitioned = False
def write(self, key, val):
if self.is_partitioned:
# CP system prioritizes consistency over availability:
raise ConnectionError(f"Node {self.node_id} isolated by partition 🛑")
self.store[key] = val
return True
node = ClusterNode("Node_1")
node.write("status", "ONLINE")
node.is_partitioned = True
try:
node.write("status", "FAIL")
except ConnectionError as err:
print(f"Network Partition Handled: {err} 🛡️")
# 31. Distributed Systems Foundations & CAP Theorem Junior developers build systems assuming the local network is an invisible, infallible highway. Senior engineers design systems assuming the router will burst into flames at 3 AM on Black Friday 🛑. ```python # The Fallacies of Distributed Computing: # 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. ``` ### The CAP Trade-Off Invariant - **Consistency (C)**: Every read receives the most recent write or an error. - **Availability (A)**: Every non-failing node returns a response (without guarantee that it contains the most recent write). - **Partition Tolerance (P)**: The system continues to operate despite arbitrary dropped or delayed messages between nodes. When a network split occurs ($P$ is guaranteed by physics): 1. **Choose CP**: Reject writes on isolated nodes to preserve single-truth consistency (e.g. Raft, ZooKeeper, etcd). 2. **Choose AP**: Allow writes on isolated nodes, risking divergent state that must be reconciled later (e.g. DynamoDB, Cassandra, CRDTs) 🌐. --- ### Interactive Pop-Quiz 🧠 #### Under the CAP theorem, why can a distributed system never achieve both Consistency and Availability during a network partition? - [ ] Because partitioned nodes cannot synchronize updates across the broken link without either serving stale data or rejecting writes 🌐 - [ ] Because the Linux kernel automatically kills TCP sockets after 3 milliseconds of partition - [ ] Because asymmetric RSA keys fail to decrypt data during high packet latency - [ ] Because disk I/O buffers become corrupt when network ping times vary --- ### Practical Challenge 🎯 Implement `CPNode` with `write(k, v)` and `read(k)`. If `self.is_partitioned` is True, `write` must raise `ConnectionError('Write rejected: partition active')` and `read` must return `'STALE_READ_ERROR'`. In normal state, it stores and retrieves values. Test normal read/write, enable partition, verify both guard behaviors, and print `'CAP_VERIFIED'`. <details> <summary>Solution</summary> ```python class CPNode: def __init__(self): self.data = {} self.is_partitioned = False def write(self, k, v): if self.is_partitioned: raise ConnectionError("Write rejected: partition active") self.data[k] = v def read(self, k): if self.is_partitioned: return "STALE_READ_ERROR" return self.data.get(k) node = CPNode() node.write("balance", 100) assert node.read("balance") == 100 node.is_partitioned = True try: node.write("balance", 200) except ConnectionError: pass assert node.read("balance") == "STALE_READ_ERROR" print("CAP_VERIFIED") ``` </details>