The CAP theorem (Eric Brewer, 2000; formally proven by Gilbert and Lynch in 2002) states that a distributed data system can guarantee at most two of these three properties at the same time when a network partition occurs: - Consistency (C): every read receives the most recent write (all nodes see the same data at the same time — linearizability). - Availability (A): every request to a non-failing node receives a response (not an error), even if it might not contain the latest data. - Partition tolerance (P): the system keeps working even when network messages between nodes are lost or delayed (a network partition). The key insight In real distributed systems, network partitions will happen, so partition tolerance is not optional. The practical choice is what to do during a partition: - CP (consistency + partition tolerance): refuse or delay some requests to avoid returning stale or conflicting data. Example: a banking ledger or inventory reservation system that rejects writes on the minority side of a partition. - AP (availability + partition tolerance): keep answering requests on all sides, accepting that some reads may be stale and writes may need reconciliation later (eventual consistency). Example: a social media feed or shopping cart. "CA" systems only exist when there is no partition — for example a single-node database — so CAP is about distributed trade-offs. Examples (with nuance — most databases are configurable) - CP-leaning: ZooKeeper, etcd and Consul (consensus-based coordination), HBase, MongoDB with majority write and read concerns, and relational databases with synchronous replication that refuse writes without a quorum. - AP-leaning: Cassandra and DynamoDB with eventual consistency settings, CouchDB, DNS. - Tunable: Cassandra (consistency levels such as ONE, QUORUM, ALL), Azure Cosmos DB (strong, bounded staleness, session, consistent prefix, eventual), DynamoDB (eventually consistent or strongly consistent reads). PACELC: a more complete model Daniel Abadi's PACELC extends CAP: if there is a Partition, choose Availability or Consistency; Else (normal operation), choose Latency or Consistency. Even without failures, stronger consistency requires coordination between replicas, which adds latency. For example, DynamoDB and Cassandra default to PA/EL (availability and low latency), while systems like Google Spanner favour consistency (PC/EC) using synchronised clocks. Common misunderstandings - CAP consistency is not the same as ACID consistency (which means respecting constraints and invariants). - Systems are not simply "CP" or "AP" overall; different operations or configurations can make different choices. - Availability in CAP means every non-failing node responds, which is stricter than the everyday meaning of uptime. How it applies in system design interviews - Payments, account balances, seat or inventory reservations: favour consistency (CP), possibly rejecting requests during failures. - Product catalogues, likes, view counts, recommendations, user feeds: favour availability and low latency (AP), with eventual consistency. - Many systems mix both: strongly consistent core transactions and eventually consistent read models, caches and search indexes. Interview summary: "CAP says that during a network partition a distributed system must choose between consistency and availability, because partition tolerance is required. Real systems tune this per use case, and PACELC adds that even without partitions there is a trade-off between latency and consistency."
Ravindra Wadile · · 0 likes