CAP Theorem
One-liner: In a distributed system, you can only guarantee two of three properties โ Consistency, Availability, and Partition Tolerance โ at the same time. ๐ The Three Properties C โ Consistency Every read returns the most recent write (or an error). All nodes see the same data at the same time. Node A: Write x=5 Node B: Read x โ must return 5 (not an old value) A โ Availability Every requestโฆ
The CAP Theorem states that in a distributed system, you can only guarantee two out of the three properties - Consistency, Availability, and Partition Tolerance - at the same time. Consistency ensures every read returns the most recent write or an error, with all nodes seeing the same data simultaneously. Availability guarantees every request receives a non-error response, even if the data may be stale.
Partition Tolerance ensures the system keeps functioning when network partitions occur, preventing nodes from communicating.
In practice, partitions are inevitable, so the choice is between CP (Consistency and Partition Tolerance) or AP (Availability and Partition Tolerance). CP systems prioritize correctness over availability, refusing to respond during partitions to avoid serving stale data. Examples include HBase, Zookeeper, etcd, MongoDB (by default), and Google Spanner. AP systems prioritize availability over correctness, responding with potentially stale data during partitions. Examples include Cassandra, DynamoDB, CouchDB, DNS, and Riak.
Databases can be classified as CA (single node, consistent), CP (distributed, strong consistency), AP (availability and partition tolerance), or eventually consistent. Tunable consistency allows systems like Cassandra to balance consistency and availability based on the required level of consistency for each operation. The PACELC theorem extends CAP, considering latency alongside partitioning scenarios.
Ultimately, the choice between CP and AP depends on the specific business needs, with financial data requiring CP and social data being suited for AP.
Written by urgent.news from Dev.to's reporting โ not their text. Machine-written โ may contain errors; check the original before relying on it.