CAP Theorem

The CAP theorem states that a distributed data store can only provide two of three guarantees: Consistency, Availability, and Partition Tolerance.

The Three Guarantees: 1. Consistency (C): All nodes see the same data at the same time. 2. Availability (A): Every request gets a response, even if it's not the latest. 3. Partition Tolerance (P): System works despite network splits. Classifications: CP (Strong consistency), AP (High availability), CA (Single-node databases - not distributed).

        graph LR
  Center["CAP Theorem"]:::main
  classDef main fill:#7c3aed,stroke:#8b5cf6,stroke-width:2px,color:white,font-weight:bold,rx:5,ry:5;
  classDef pre fill:#0f172a,stroke:#3b82f6,color:#94a3b8,rx:5,ry:5;
  classDef child fill:#0f172a,stroke:#10b981,color:#94a3b8,rx:5,ry:5;
  classDef related fill:#0f172a,stroke:#8b5cf6,stroke-dasharray: 5 5,color:#94a3b8,rx:5,ry:5;
  linkStyle default stroke:#4b5563,stroke-width:2px;

      

🧒 Explain Like I'm 5

Imagine a shared bank account for you and your friend. If the phone lines go down and you can't talk to each other, you have two choices: 1. Stop letting anyone take out money (Availability fails, but Consistency is kept). 2. Let everyone take out money, but risks spending more than you have (Consistency fails, but Availability is kept). You can't have both perfectly if the connection is broken.

🤓 Expert Deep Dive

The simplicity of the 'pick any two' model is often criticized because, in practice, Partition Tolerance (P) is not optional for a distributed system. Therefore, the choice is usually between AP (Available and Partition Tolerant, favoring eventually consistent models like Cassandra) and CP (Consistent and Partition Tolerant, favoring strong consistency via consensus protocols like Raft or Paxos, as seen in Zookeeper or etcd). The PACELC theorem extends this by stating that even when the system is NOT partitioned (E - else), there is a trade-off between Latency (L) and Consistency (C). This explains why many modern systems choose 'Tunable Consistency', allowing developers to decide how rigid the rules should be for different types of data.

📚 Sources