30-10-2025 - CAP Theorem

In distributed systems, there is something called CAP Theorem.

Before looking at what CAP Theorem is about, let's try to understand what distributed systems means on a high-level.

Distributed computing, according to wiki:


Distributed computing is a field of computer science that studies distributed systems, defined as computer systems whose inter-communicating components are located on different networked computers

The main idea (at its simplest) is this - we have multiple nodes that are distributed across various regions. The only way these nodes can talk to each other is via the network.

So, what happens when the network is down? These distributed nodes can no longer talk to each other! This also means that the latest piece of information or data cannot be shared with the other nodes. So the question now - is it OK to continue, or no?

CAP Theorem

There are many detailed articles about CAP Theorem that probably explains it better than I do. So, I will keep it simple here.

Consistency: All reads must be the most recent write or error.

This means that all nodes must know that the data it is reading is the most recent. If network is down, the nodes cannot know for sure that the data is the most recent. Thus, we stop processing until network is up again.

Example: booking a flight seat. It is preferred to ensure no seats are double booked!

Availability: Every request received by a non-failing node in the system must result in a response.

This means that we must at least give some response. Even if it is outdated, it is OK.

Example: posting a new photo on Instagram. Users cannot see the most recent photo uploaded, but can still see the older posts up ~5mins ago. It is OK.

(p.s.: this is also where eventually consistency comes in!)

Partition Tolerance: The system continues to operate despite an arbitrary number of messages being dropped (or delayed) by the network between nodes.

In distributed systems, partition tolerance is guaranteed as network failures are inevitable. Since we have to design for partition tolerance, we are forced to choose between guaranteeing Consistency (C) or Availability (A) when a network partition occurs.

Conclusion

CAP Theorem shows us the importance of deciding between high-availability and high-consistency. And since distributed systems must be designed to tolerate network partitions, we are forced to choose between Consistency (CP) and Availability (AP).

Deciding to go AP vs CP will ultimately affect various components of the system design.