Advanced
12 min forge
CAP Theorem
The fundamental trade-off in distributed systems: Consistency, Availability, and Partition Tolerance.
π CAP Theorem
The CAP theorem states that a distributed system can only provide two of the following three guarantees at the same time.
1. The Three Pillars
- Consistency (C): Every read receives the most recent write or an error.
- Availability (A): Every request receives a (non-error) response, without the guarantee that it contains the most recent write.
- Partition Tolerance (P): The system continues to operate despite an arbitrary number of messages being dropped (or delayed) by the network between nodes.
2. Why Partition Tolerance (P) is Mandatory
In a distributed system, network failures are inevitable. Therefore, a distributed system must choose between Consistency and Availability when a partition occurs.
3. The Trade-offs
- CP (Consistency + Partition Tolerance): If a partition occurs, the system stops accepting writes to ensure consistency. Use case: Banking systems, distributed locks (Etcd, ZooKeeper).
- AP (Availability + Partition Tolerance): If a partition occurs, nodes continue to accept writes, possibly leading to inconsistent data. Use case: Social media feeds, DynamoDB, Cassandra.
- CA (Consistency + Availability): This only exists in single-node systems or non-partitioned networks (which don't exist in the real world of distributed systems).
4. Conclusion
CAP is a useful mental model, but real-world systems often fall on a spectrum rather than being strictly one or the other. For a more nuanced view, see the PACELC Theorem.