Lesson 3 · Scaling and Trade-offs
CAP Theorem & Trade-offs
When the network fails, you must choose — and there is no middle ground.
The CAP theorem states that in the presence of a network partition, a distributed system can only provide either Consistency or Availability — but not both. It defines the constraints every distributed data store must accept by forcing a choice about how the system behaves when its components cannot communicate.
This is the first theorem in distributed systems that architects internalize as a hard constraint, not a preference. Understanding it doesn't give you a recipe — it gives you a lens for evaluating trade-offs every time you choose a data store or design a failure mode.
The Framework
Defining the Three Pillars
The Trade-off
CP vs. AP — Choosing Your Failure Mode
When a network partition splits your nodes into isolated groups, they can no longer coordinate. At that point, you must decide: does the system stop serving requests to avoid returning incorrect data, or does it continue serving requests with whatever data it currently holds?
This is not an engineering oversight — it is a fundamental constraint of distributed computing. The choice you make defines your system's behavior under failure.
If a node cannot verify its data is current with the other side of the partition, it returns an error or times out rather than risk serving stale information.
If a node is partitioned, it continues accepting writes and serving reads using whatever data it has. Nodes may diverge and must reconcile once the partition heals.
Decision tree during a network partition
Refining the C
The Spectrum of Consistency Models
CP and AP describe a decision made at the moment a partition hits. But consistency itself is not a single property. It is the contract between a data store and its clients about the ordering and visibility of writes, and that contract comes in degrees.
The C in CAP names the strictest point on that gradient. Real systems rarely apply one point across an entire application — they run different guarantees for different workflows, paying for strictness only where the business actually needs it.
The strict end
The strongest single-object model. Every operation appears to take effect atomically at one instant between its invocation and its completion. Once a write completes, any later read — no matter which node serves it — returns that value or a newer one.
Relaxes real time. Operations do not have to line up with a global clock, but every client observes all operations in the same relative order. Cheaper to maintain, and enough for many coordination problems.
The loose end
Eventual consistency guarantees only that if writes stop, all replicas converge on the same value. Until then, concurrent reads sent to different nodes can return stale data, mutations out of order, or values that flatly contradict each other.
Between full linearizability and raw eventual convergence sit the client-centric guarantees. Each is far cheaper than linearizability, and each buys one specific property that users actually notice when it is missing.
A client always sees its own updates. Change your profile photo and a reload shows the new one, even while other users still see the old one.
A client never moves backwards in time. Once it has observed a value, later queries never hand back an older one.
Nobody sees a write without the writes it depends on. A reply never appears before the question it answers.
Replication behavior under network partition
Under the Hood
Quorum: How a Node Knows to Say No
A partitioned node cannot tell whether the other side is dead or merely unreachable, so it never tries to guess. Consensus protocols such as Raft and Paxos require a strict majority of the cluster, ⌊N/2⌋ + 1 out of N nodes, to agree before a write is committed. A node that cannot reach that many peers refuses to act.
This is what prevents split-brain, the failure mode where two disconnected halves of a cluster both believe they are in charge, both accept writes, and diverge into two irreconcilable versions of the truth. Since only one side of a partition can hold a majority, only one side is ever allowed to make progress.
A 5-node cluster split across two data centers
Quorum is ⌊5/2⌋ + 1 = 3 nodes. The link between the two sites goes down.
Elects a leader and keeps serving reads and writes. This side holds the authoritative state.
Every write and every linearizable read sent to N4 or N5 is rejected or blocked until the partition heals.
Real-World Example
A Banking Application Under Partition
A user has $100 in their account. The system replicates this balance across two data centers. A network partition cuts off communication between them. The user attempts a withdrawal.
The AP Side
Conflict Resolution When Replicas Diverge
Choosing AP is not the end of a decision — it is the start of a second one. Node B accepted the write while it was isolated, so once the link comes back there are two versions of the same record and no node that witnessed both.
Every highly available store therefore ships a reconciliation strategy. Which one it picks decides whether divergence costs you data or merely costs you code.
Approach 1 — Last-Write-Wins
Approach 2 — Vector Clocks and Version Vectors
// One version descends from the other
V1 = [ a:1, b:1 ] V2 = [ a:2, b:1 ]
every entry in V1 is ≤ V2, and one is strictly less
→ V1 happened before V2, so V2 wins and V1 is safe to drop
// Neither descends from the other
V1 = [ a:2, b:1 ] V2 = [ a:1, b:2 ]
neither vector dominates the other
→ concurrent write, unresolvable without domain knowledge
→ both versions surface to the application as siblingsApproach 3 — Conflict-Free Replicated Data Types
Arrival order does not change the result.
Grouping of merges does not change the result.
Applying the same update twice changes nothing.
A counter supporting increments and decrements, by keeping one grow-only tally per node for each direction and summing them.
A set supporting adds and removes, where each element carries a timestamp that settles membership conflicts.
An observed-removed set, where each add is tagged with a unique id so a concurrent add always beats a concurrent delete.
Quiz Review
Check your understanding
Question 1 of 14
What does the "C" in CAP mean, and what does it guarantee?
- ✓Consistency — every read receives the most recent write or an error. The system behaves as if there is only one copy of the data, even when replicated across nodes.