System Design: Everything About CAP Theorem

Ankush kunwar
3 min readJan 29, 2024

--

The CAP theorem, also known as Brewer’s theorem, states that in a distributed computer system, you can only guarantee two out of three properties: consistency, availability, and partition tolerance.

Consistency: All nodes in the system have the same data at the same time. In other words, when a data update is performed, all subsequent access to that data will return the updated value.

Availability: Every request made to the system receives a response, either success or failure. This means the system is always operational and responsive, even if some of its parts are failing.

Partition tolerance: The system continues to operate despite network partitions (communication failures) between nodes. Even if some nodes are unable to communicate with each other due to network failures, the system still functions.

According to the CAP theorem, in the presence of a network partition (P), you must choose between consistency (C) and availability (A). You can’t have both. However, it’s essential to note that this doesn’t mean you have to sacrifice one entirely for the other. Rather, it implies a trade-off where you prioritize one over the other based on your system’s requirements and design principles.

For example:

  • In a distributed database system designed for financial transactions, consistency might be a higher priority to ensure that all nodes agree on the account balances, even if it means sacrificing availability during network partitions.
  • On the other hand, in a web application where users expect continuous availability, sacrificing consistency during network partitions might be acceptable, allowing the system to continue serving requests even if it means some users might see slightly outdated data.

Understanding and managing this trade-off is crucial to designing and implementing distributed systems. Various distributed databases and systems employ different strategies to balance these properties based on their specific use cases and requirements.

Now, let’s see how the CAP theorem applies in different scenarios:

  • Scenario 1: Prioritizing Consistency and Partition Tolerance: In this scenario, the system prioritizes consistency and partition tolerance over availability. When a user posts an update, the system ensures that all users immediately see the updated post. However, during network partitions, the system may become unavailable until the partition is resolved to maintain consistency.
  • Scenario 2: Prioritizing Availability and Partition Tolerance: Here, the system prioritizes availability and partition tolerance over consistency. When a user posts an update, the system immediately makes it available to other users, even if some users might see slightly outdated data due to eventual consistency. During network partitions, the system remains available, allowing users to continue interacting with the available parts of the system, even if there’s a temporary inconsistency in the data.

These scenarios illustrate how the CAP theorem applies to designing and implementing distributed systems like a social media platform, where the trade-off between consistency, availability, and partition tolerance depends on the specific requirements and priorities of the system.

Observation: In a distributed system, partition tolerance (P) is a fundamental requirement because network partitions are inevitable in distributed environments. Therefore, when discussing the CAP theorem, we typically focus on the trade-off between consistency (C) and availability (A) in the presence of partition tolerance.

So, when designing a distributed system, you are indeed left with the trade-off between consistency and availability. You must decide how to balance these two properties based on your system’s requirements, priorities, and constraints.

Thank you for reading !!!

If you enjoy this article and would like to Buy Me a Coffee, please click here.

you can connect with me on Linkedin.

--

--

Ankush kunwar
Ankush kunwar

Written by Ankush kunwar

Experienced Software Engineer Skilled in Microservices, Backend Development, System Design, Python, Java, Kubernetes, Docker, AWS, and Problem Solving

No responses yet