> ## Documentation Index
> Fetch the complete documentation index at: https://rajanand.org/llms.txt
> Use this file to discover all available pages before exploring further.

# CAP Theorem

> Consistency, Availability and Partition Tolerance

If you're new to the world of **Big Data** and distributed systems, the **CAP Theorem** is a fundamental concept you need to understand. It helps you make informed decisions when designing or choosing distributed systems.

### **What is the CAP Theorem?**

The **CAP Theorem** states that in a distributed system, you can only guarantee two out of the following three properties at the same time:

1. <Icon icon="circle-c" iconType="duotone" size="25" /> **[Consistency](/glossary/consistency)**:
   * Every read receives the **most recent write** or an error.
   * All nodes in the system see the same data at the same time.

2. <Icon icon="circle-a" iconType="duotone" size="25" /> **[Availability](/glossary/availability)**:
   * Every request receives a **response**, even if some nodes are down.
   * The system remains operational and responsive.

3. <Icon icon="circle-p" iconType="duotone" size="25" /> **[Partition Tolerance](/glossary/partition-tolerance)**:
   * The system continues to function even if there is a **network partition** (communication breakdown between nodes).

<img className="block dark:hidden" src="https://mintcdn.com/rajanand/jl6WdKP_dLzq_MPC/assets/notes/cap-theorem.png?fit=max&auto=format&n=jl6WdKP_dLzq_MPC&q=85&s=5a72b5668e1f461ad5c6f872e2645412" width="938" height="518" data-path="assets/notes/cap-theorem.png" />

<img className="hidden dark:block" src="https://mintcdn.com/rajanand/jl6WdKP_dLzq_MPC/assets/notes/cap-theorem-dark.png?fit=max&auto=format&n=jl6WdKP_dLzq_MPC&q=85&s=52d7a2669273706b9a8743ba20a851c3" width="938" height="518" data-path="assets/notes/cap-theorem-dark.png" />

### **Why is the CAP Theorem Important?**

In distributed systems, **network partitions** (e.g., delays, failures) are inevitable. The CAP theorem helps you decide how your system should behave in such scenarios. You must choose between **Consistency** and **Availability** when a partition occurs.

### **The Three Combinations**

1. **CA (Consistency + Availability)**:
   * The system ensures **Consistency** and **Availability** but sacrifices **Partition Tolerance**.
   * **Example**: Traditional relational databases like MySQL or SQL Server.
   * **Analogy**: A library where every book is always in its correct place (consistent), but the library closes during maintenance (not partition-tolerant).
   * **Use Case**: Systems where data integrity is critical, and network partitions are rare (e.g., single data center setups).
   * A **banking system** where data consistency is critical. If a network partition occurs, the system may become unavailable to ensure data accuracy.

2. **CP (Consistency + Partition Tolerance)**:
   * The system ensures **Consistency** and **Partition Tolerance** but sacrifices **Availability**.
   * **Example**: Distributed databases like MongoDB (with strong consistency) or Apache HBase.
   * **Analogy**: A library where books are always in the correct place (consistent), but you can’t borrow books during maintenance (not available).
   * **Use Case**: Systems where data accuracy is more important than availability (e.g., financial systems).
   * A **stock trading platform** where real-time data accuracy is crucial. If a partition occurs, the system may reject requests to avoid inconsistencies.

3. **AP (Availability + Partition Tolerance)**:
   * The system ensures **Availability** and **Partition Tolerance** but sacrifices **Consistency**.
   * **Example**: [NoSQL](/glossary/nosql) databases like Cassandra or DynamoDB.
   * **Analogy**: A library where you can always borrow books (available), but sometimes the books are misplaced (inconsistent).
   * **Use Case**: Systems where high availability is critical, and temporary inconsistencies are acceptable (e.g., social media platforms).
   * **Social media platform**: If a partition occurs, the system remains available, but users may see slightly outdated data.

### **Key Takeaways**

1. **Trade-Offs**:
   * You cannot have all three properties (Consistency, Availability, Partition Tolerance) simultaneously in a distributed system.
   * You must choose based on your system’s requirements.
2. **Partition Tolerance is Non-Negotiable**: In distributed systems, network partitions are inevitable. Therefore, you must choose between **Consistency** and **Availability**.
3. **No One-Size-Fits-All Solution**: The choice between CA, CP, or AP depends on your use case and priorities.

[https://www.youtube.com/watch?v=BHqjEjzAicA](https://www.youtube.com/watch?v=BHqjEjzAicA)
