CAP Theorem
Why distributed systems can't have Consistency, Availability, and Partition Tolerance all at once during network failures.
Three Desirable Properties
The CAP theorem (proven by Gilbert and Lynch in 2002) states that distributed systems cannot simultaneously guarantee all three properties:
- Consistency: All nodes see the same data at the same time
- Availability: Every request receives a response
- Partition Tolerance: The system works despite network failures
CAP doesn't constrain trade-offs when there is no partition. The tension only emerges when the network splits.
When Everything Works
During normal operation, a distributed system can provide all three properties. Write to one node, replicate to others, and every node returns the latest value.
The magic breaks when the network breaks.
- Replication keeps nodes in sync
- Reads return consistent data from any node
- No trade-off required when network is healthy
Network Partition Strikes
A network partition splits your cluster into groups that cannot communicate. This happens due to router failures, cable cuts, datacenter issues, or cloud provider problems.
During a partition, Node 1 accepts a write (x=6) but cannot
replicate to Node 2 (still has x=5). Now the nodes disagree
— this is called split-brain.
- Partitions are inevitable, not exceptional
- Split-brain: nodes have different views of data
- The system must continue operating during partition
Choose: Consistency or Availability
When a client reads from Node 2 during the partition, the system faces an impossible choice:
- Choose Consistency (CP): Refuse to respond until partition heals. Client gets an error, but never sees wrong data.
- Choose Availability (AP): Return stale value
x=0. Client gets a response, but it may be outdated.
You cannot have both — this is the CAP theorem. The partition forces the choice.
Systems Make Different Choices
The right choice depends on your domain:
- CP (Consistency): When wrong data causes real harm — financial transactions, inventory counts, distributed locks.
- AP (Availability): When stale data is acceptable — social media feeds, shopping carts, DNS, CDNs.
Ask yourself: "What's worse — showing old data or showing nothing?"
Note: Different parts of a system can make different choices. Your checkout might be CP while your product catalog is AP.