CAP Theorem
Tags: systemdesign, beginners, learning
Published: 2023-05-02 08:57:18


Let have brief understanding about, What is a CAP Theorem? The CAP theorem is a **concept in computer science that describes the trade-offs that exist in building [distributed systems](https://www.geeksforgeeks.org/what-is-a-distributed-system/).**

The CAP Theorem revolve around the three concept which is from the CAP acronym, i.e. **Consistency**, **Availability** and **Partition Tolerance.**

--- 

### Consistency

When a request is made. The system node always returns up-to-date information. This ensures that the information provided is accurate and reliable whenever a request is made. This makes the system trustworthy and reduces error risks. As a result, users are always provided with the latest version of the data.

### Availability

The system always returns information, regardless of whether it is stable or not. It ensures that no matter what the circumstances are, users always have access to their data. Even though it provides inaccurate data, it is available to the public. But if it cannot handle any query it is unavailable.

### Partition Tolerance

The system can continue operating during network partition. This means that the system can keep functioning even if there is a disruption in the network. For example, a server is down or a link may be down. This ensures that the system can continue to serve its users during a network partition.

---

## What is CAP Theorem?
Now, We have basic idea about the CAP acronym. Let comeback to the CAP Theorem.

> The CAP theorem states that you have **only two properties in a distributed system but not all three.**

In other words, it is not possible to achieve consistency, availability, and partition tolerance in a distributed system simultaneously. However, it is possible to prioritise two of these properties while sacrificing the third.

![CAP Theorem](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cfokalu34ikaj7rtz5j3.png)

### Consistency and availability

It is possible for a system to ensure consistency and availability, but no partition tolerance is possible. In other words, if the network has no partition, we can guarantee consistency and availability. This generally makes sense because when a network partition isn't happening the system is running normally and thus can have both consistency and availability because there's no need to make trade-offs. This means it can always respond to requests with the latest information.

### Consistency and partition tolerances 

Achieving both consistency and partition tolerance can be challenging, as these properties can sometimes be at odds with each other. Consistency requires that all nodes in a distributed system see the same data at the same time. Partition tolerance allows the system to remain operational in the event that some nodes fail or become inaccessible. Balancing the two is key to ensuring successful distributed operations. To maintain this balance, distributed systems must have rules in place to prevent data inconsistency and maintain availability in the event of a failure. Additionally, they must have mechanisms in place to ensure nodes stay in sync.

## Availability and partition tolerances 

Availability refers to the percentage of time a system is operational. Partition tolerance refers to the ability of a system to continue functioning despite the failure of one or more of its components. High availability and partition tolerance are important characteristics of distributed systems. Together, they enable systems to remain available even in the face of component or network failure. This ensures that distributed systems are robust and reliable, and can continue to function even in the event of an outage or disruption. This makes distributed systems more resilient and better able to handle large workloads.

---

The CAP theorem states that it is impossible for a distributed system to simultaneously guarantee all three of the following characteristics: Consistency, Availability, and Partition tolerance. This theorem implies that a distributed system can only achieve two of the three characteristics, as one must be sacrificed in order to meet the other two. This has important implications for the design of distributed systems, as developers must choose the two characteristics they wish to prioritise.