Open In App

CAP Theorem in System Design

As we know the CAP Theorem is a part of System Design. During the design, we face the common challenges that have to be considered while designing a distributed system. In this article, we will discuss the CAP Theorem, and why it helps design an efficient System.



1. What is the CAP Theorem in System Design?

CAP Theorem

CAP theorem states that in networked shared-data system or distributed system can share/have only two of the three desired characteristics for a database: Consistency, Availability, and Partition tolerance.



CAP Theorem is also known as Brewer’s theorem and it was introduced by the computer scientist Eric Brewer at the Symposium on Principles of Distributed Computing in 2000.

2. Properties of CAP Theorem in System Design

The property of three distributed system characteristics to which CAP Theorem refers:

1. Consistency

Consistency defines that all clients see the same data simultaneously, no matter which node they connect to in a distributed system. For eventual consistency, the guarantees are a bit loose. Eventual consistency gurantees means client will eventually see the same data on all the nodes at some point of time in the future.

Consistency

Below is the explaination of the above Diagram:

2. Availability

Availabilty defines that all non-failing nodes in a distributed system return a response for all read and write requests in a bounded amount of time, even if one or more other nodes are down.

Availability

Below is the Explanation of the above Diagram:

3. Partition Tolerance

Partition Tolerance defines that the system continues to operate despite arbitrary message loss or failure in parts of the system. Distributed systems guranteeing partition tolerance can gracefuly recover from partitions once the partition heals.

Partition Tolerance

Below is the Explanation of the above Diagram:

3. Trade-Offs in the CAP Theorem

We can classify the systems into the following three categories:

Trade-off in the CAP Theorem

1. CA System

A CA System delivers consistency and availiability across all the nodes. It can’t do this if there is a partition between any two nodes in the system and therefore does’t supoort partition tolerance.

2. CP System

A CP System delivers consistency and partition tolerance at the expense of availability. When a partition occurs between two nodes, the systems shuts down the non-available node until the partition is resolved. Some of the examples of the databases are MongoDB, Redis, and HBase.

3. AP System

An AP System availabiiity and partition tolerance at the expense of consistency. When a partition occurs, all nodes remains available, but those at the wrong end of a partition might return an older version of data than others. (When the partition is resolved, the AP databases typically resync the nodes to repair all the inconsistencies in the system). Example: CouchDB, Cassandra and Dyanmo DB, etc.

4. Example to Understand the CAP Theorem

In the figure above,

5. Use Cases of the CAP Theorem in System Design

Here we will see how we can use all the trade-off system in real application:

5.1 Banking Transactions (CP System)

Problem Statement:

Imagine a bank teller updating your account balance on a secure computer system. This system prioritizes consistency (C) and partition tolerance (P).

Why we use CP System ?

5.2 Social Media Newsfeed (AP System)

Problem Statement:

Think of your newsfeed on a social media platform constantly updating with new posts and stories. This system prioritizes availability (A) and partition tolerance (P).

Why we use AP System ?

5.3 Online Shopping Cart (Hybrid System CAP System):

Problem Statement:

Imagine an online shopping cart, adding items, and checking out. This system might employ a hybrid approach balancing CAP trade-offs.

Why we use AP and CP System?

6. Advantages of CAP Theorem in System Design

7. Disadvantages of CAP Theorem in System Design

8. Conclusion

CAP theorem is a valuable tool, but it’s important to be aware of its limitations and apply it critically within the context of your specific system design challenges. Utilize its insights to make informed decisions, explore hybrid approaches when necessary, and stay open to adapting your solutions as needs and technologies evolve. The CAP theorem, despite highlighting inherent limitations, serves as a valuable guide and decision-making framework for designing reliable, efficient, and user-centric distributed systems. Its advantages lie in its ability to clarify trade-offs, inform technology choices, enhance system resilience, and even inspire further innovation in the field.


Article Tags :