CAP Theorem, PACELC & Consistency Models
Busting the "Pick 2 of 3" myth from first principles. Master CP vs AP trade-offs, the PACELC theorem, Linearizability, Causal Consistency, Eventual Consistency, and Quorum Consensus ($R + W > N$).
๐ป 1. The Core Intuition: The Walkie-Talkie Twins
Imagine two twin students, Alice (sitting in Classroom A) and Bob (sitting in Classroom B). They are connected by a walkie-talkie wire.
A teacher walks into Classroom A and tells Alice: "Your new score is 95". Suddenly, a student trips and cuts the walkie-talkie wire (Network Partition $P$).
A moment later, a second teacher walks into Classroom B and asks Bob: "What is the test score?". What should Bob do?
Bob says: "My walkie-talkie wire is broken. I cannot verify if Alice received an update, so I refuse to answer right now (Error 503)".
๐ก๏ธ Result: Zero stale answers given (Consistency preserved), but the teacher gets an error (Availability sacrificed).Bob immediately answers with the old score (80) written in his notebook.
โก Result: The system always answers immediately (Availability preserved), but the teacher gets outdated stale data (Consistency sacrificed).๐งฉ 2. The PACELC Theorem (What Happens When There Is No Partition?)
CAP theorem only describes what happens during a network failure. But what about the 99.9% of normal operating time? Computer scientist Daniel Abadi created PACELC:
Else (normal healthy operation): choose between Latency or Consistency.
| Database | PACELC Type | During Partition ($P$) | Normal Operation ($E$) | Typical Use Case |
|---|---|---|---|---|
| Google Spanner / CockroachDB | PC / EC | Stops writes (Prefers C) | Waits for sync consensus (Prefers C) | Banking, Ledger, Financial transactions |
| MongoDB / HBase | PC / EC | Majority partition only (Prefers C) | Reads from Primary (Prefers C) | Document storage with strict consistency |
| Apache Cassandra / DynamoDB | PA / EL | Accepts writes anywhere (Prefers A) | Returns local replica data (Prefers Low Latency) | Shopping carts, Metrics, Chat messages |
๐ 3. The Spectrum of Distributed Consistency Models
"Consistency" is not a binary choice. It is a spectrum spanning from strongest to weakest:
The system acts as if there is only one single copy of data in the entire universe. As soon as a write finishes at wall-clock time $t_1$, all reads anywhere on Earth at $t_2 > t_1$ MUST see the new value. (Etcd, ZooKeeper, Spanner TrueTime).
All nodes agree on the exact same chronological sequence of operations, although the sequence may lag slightly behind real-world wall-clock time.
If Event A causes Event B (e.g. a question on Reddit causes a reply), every user sees A before B. Independent concurrent events may be observed in different orders.
If no new updates are made, all replicas will eventually converge to the same value via Read Repair or Merkle Tree Anti-Entropy.
๐งฎ 4. Quorum Math: Guaranteeing Strong Consistency ($R + W > N$)
How do distributed databases (DynamoDB, Cassandra) provide configurable consistency without locking all nodes?
๐ป 5. Tunable Quorum Client Implementation (Multi-Language)
Here is the production implementation of a Quorum Coordinator with version resolution across 5 languages:
# Python Tunable Quorum Read Coordinator import asyncio async def quorum_read(key: str, nodes: list, R: int, N: int) -> dict: # Dispatch parallel reads to all N nodes tasks = [asyncio.create_task(node.get(key)) for node in nodes] responses = [] for completed in asyncio.as_completed(tasks): resp = await completed if resp.is_success: responses.append(resp) if len(responses) >= R: break # Quorum threshold met! if len(responses) < R: raise Exception("HTTP 503: Read Quorum Not Met") # Pick highest timestamp version (Last-Write-Wins / Vector Clock) latest = max(responses, key=lambda r: r.version) return latest.value
๐งช 6. Interactive Lab: Live 3-Node Network Partition Simulator
CAP Partition & Quorum Simulator
๐ฏ 7. Test Your Understanding
You are designing an ATM withdrawal system. A user has $100 in their account. If a network partition isolates the ATM from the central bank database, should the system choose CP or AP?
On a global social network, Alice posts a question: "Who won the game?", and Bob replies: "Arsenal won!". Which consistency model is strictly required to guarantee NO user sees Bob's reply before Alice's question?