What is the CAP theorem?

RAJESH KUMAR
rtkal
Published in
3 min readAug 12, 2023

The CAP theorem, also known as Brewer’s theorem, is a fundamental principle in computer science that explains the limitations of distributed systems. CAP stands for Consistency, Availability, and Partition tolerance, which are three properties that cannot all be achieved simultaneously in a distributed system.
A distributed database system can only guarantee two out of these three characteristics: Consistency, Availability, and Partition Tolerance.

  1. Consistency: Every read operation in the system will always return the most recent write or an error.
    In other words All the clients/nodes in the system see the same data at the same time, no matter which node they connect to. For this to happen, whenever data is written to one node, it must be instantly forwarded or replicated to all the other nodes in the system before the write is deemed successful.👍
  2. Availability: All reads contain the data, but it might not be the most recent or this does not guarantee that the response contains the most recent write or an error.
    Availability refers to the percentage of time that the infrastructure, system or a solution remains operational under normal circumstances(conditions) in order to serve its intended purpose.
    Refer this for more details with examples.
  3. Partition tolerance: The system continues to function even if network communication between nodes is unreliable or fails. It can tolerate network partitions, which occur when nodes are unable to communicate with each other.
    👉 Example : Imagine a messaging app where users can send messages to each other. Even if there is a temporary network issue, users should still be able to send and receive messages. The system is designed to handle network partitions, ensuring that messages can be sent and received independently on each side of the partition. Once the network issue is resolved, the system will reconcile the messages exchanged during the partition to maintain data consistency. In this example, partition tolerance allows the messaging app to maintain availability and keep serving its users even when there is a temporary network partition

What is a network partition 😊?
👉A network partition refers to a situation where the network that connects the nodes in the system is disrupted, causing a temporary loss of communication between them. During a partition, the nodes are unable to exchange information and synchronize their data.

According to the CAP theorem, when a distributed system faces a network partition (P), You have to make a choice between Consistency and Availability. It means that, in the presence of a network partition (P), you need to make a decision about which property is more important for your distributed system but at the same time you cannot have both properties guaranteed simultaneously.

If you prioritize Availability (A) over Consistency (C), it means that the system will try its best to keep responding to client requests, even during the partition.
→ High availability comes at the cost of lower consistency

However, since the nodes cannot communicate with each other, they may not have the most up-to-date data. In other words, some nodes might have newer data than others. When the system continues to serve requests in this situation, there is a possibility that it may return stale data, which means the information could be outdated or not reflect the latest changes.

On the other hand, When you prioritize Consistency(A) over Availability(A), it means that the system ensures all nodes have the same data and guarantees strong consistency even in the face of network partitions. In this case, the system may temporarily halt or become unavailable until the partition is resolved and all nodes are synchronized. This ensures that clients always receive the most up-to-date and consistent data.
→ High consistency comes at the cost of lower availability.

In summary, the CAP theorem helps us understand the fundamental trade-offs that exist in distributed systems, forcing us to make decisions about how to prioritize consistency and availability based on the system’s needs and the possibility of network partitions.

I hope this article would help you

Thank you, Guys! This article is over here and I think it’s pretty much about the CAP theorem. If you have any queries please let me know in the comments.

Recommended read: https://medium.com/rtkal/key-characteristics-of-distributed-system-system-design-f3a64d878814

For more posts like this, follow me at https://medium.com/rtkal

--

--

RAJESH KUMAR
rtkal
Editor for

A Full Stack Developer, Designer, Software Engineer, Distributed System Programmer, JavaScript Programmer and AWS Cloud Developer for 4 years.