CAP Theorem: One Theorem to Rule Them All

Mahantesh Ambali
5 min readAug 2, 2024

--

Photo by 2H Media on Unsplash

The CAP theorem, a foundational concept in theoretical computer science, applies to distributed data stores and the applications that utilize them. It posits that in the event of a network failure, a distributed data store can either provide Consistency or Availability, but not both. Before diving into CAP, it’s essential to understand the 8 Fallacies of Distributed Computing, which you can find here.

Due to changing user behaviors and growing demands, it’s no longer feasible to store and retrieve information efficiently from a single DB server. Applications must work with data stores running on different hosts, interconnected by networks. Since networks can be unreliable, Partition Tolerance (P) is a necessity. Consequently, a distributed data store must either be available (compromising consistency) or consistent (compromising availability). Applications utilizing these data stores must make informed decisions regarding these properties.

Defining the Terms
Before discussing the theorem, let’s define Consistency, Availability, and Partition Tolerance:

Consistency: Every read request returns the most recent write or an error.
Availability: Every request receives a response, but it might not reflect the most recent write.
Partition Tolerance: The system continues to operate despite network failures.

CAP Theorem Explained

CP Database: Offers Consistency and Partition Tolerance but sacrifices Availability. During a partition, the system must make the inconsistent node unavailable, leading to rejected requests.

AP Database: Offers Availability and Partition Tolerance but sacrifices Consistency. During a partition, the node remains available but may respond with outdated data.

CA Database: Offers Consistency and Availability but does not tolerate partitions. Practically, no distributed database system can avoid partitions entirely, making CA systems impossible.

Consistency in CAP Theorem vs. ACID
The term ‘Consistency’ in CAP differs from ‘Consistency’ in ACID. In CAP, it refers to serving up-to-date or stale data, whereas in ACID, it pertains to data leaving a database in a logically correct state. Traditional RDBMS systems are ACID compliant, choosing Consistency over Availability. In contrast, No-SQL databases follow the BASE (Basically Available, Soft State, Eventually Consistent) philosophy, prioritizing Availability over Consistency.

Misleading “2 of 3” Concept
In 2000, during a keynote address, Eric Brewer introduced the “2 of 3” rule to explore a broader design space. Over time, this rule has been misunderstood as a strict choice among Consistency, Availability, and Partition Tolerance. In reality, trade-offs between these properties are not binary decisions.

Availability can vary from 0% to 100%. Technically, choosing 0% availability during a partition translates to lost revenue and damaged reputation. Therefore, some systems opt for partial availability rather than none.

Achieving Partial Availability-
To achieve partial availability, consider the following options:

Read and Write Operations: Compromise on consistency to allow read and write operations.
Quorum-Based Voting: A subset of systems must agree for a read or write operation to be successful.
Asynchronous Replication: Data can be asynchronously replicated to other nodes without waiting for an acknowledgment, keeping the system available during partitions.
Fallback Mechanism: Serve data from a cache when the primary data source is unreachable.

Consistency Levels
Consistency need not be a binary choice. Applications can adhere to various levels of consistency during a partition:

Strong Consistency: After a write completes, any reads will return the most recent write. Used when accuracy is crucial, e.g., financial systems.

Eventual Consistency: All replicas will converge to the same state for a record given enough time. Used when immediate consistency isn’t required, e.g., e-commerce systems.

Causal Consistency: Ensures that all nodes see data in the same order causally related. Weaker than strong but stronger than eventual. Example: chat applications.

Read Your Writes: Writes made by a client are always visible to read requests from the same client.

Adjusting Consistency
Consistency can be adjusted based on:

Replication Factor: The number of copies stored across the data store. A higher number ensures fault tolerance but increases latency.

Quorum-Based Voting: The number of nodes that must acknowledge a read or write operation for it to be successful.

To achieve strong consistency,
𝑅 + 𝑊 > 𝑁 (where R is the number of nodes that must respond to a read request,
W is the number of nodes that must respond to a write request, and
N is the number of replicas).

For weaker consistency, set R or W lower.

CAP-Latency Connection
Latency, the time taken for the system to respond to a request, is not directly addressed by the CAP theorem. However, decisions balancing Consistency and Availability significantly impact latency.

Consistency & Latency: Achieving strong consistency requires data to be replicated across all nodes, possibly worldwide, before responding to the caller, increasing latency. Systems with eventual consistency have lower latency, as writes can be acknowledged quickly without immediate synchronization.

Availability & Latency: Highly available systems often compromise on consistency, responding with potentially stale data.

PACELC (Partition Tolerance, Availability, Consistency Else Latency Consistency)
While CAP deals with consistency versus availability during a partition, PACELC adds considerations for latency versus consistency when there is no partition.

During normal operation without a partition, a distributed data store must either:

Sacrifice Consistency for low latency, or Sacrifice Latency to be consistent.

For instance, achieving strong consistency may require nodes to communicate and confirm data changes, increasing response times. Conversely, a system might reduce latency by providing weaker consistency, such as eventual consistency, where updates propagate to all nodes eventually but not immediately.

Conclusion
The CAP Theorem and its extended concept, PACELC, are critical frameworks for understanding the trade-offs in distributed systems. As distributed data stores become more prevalent, it’s essential for developers, architects, and engineers to grasp these concepts to design robust, efficient, and scalable applications.

By carefully considering the balance between Consistency, Availability, and Partition Tolerance, and factoring in latency, one can make informed decisions that align with the specific needs and priorities of their applications. Embracing these principles allows us to navigate the complexities of distributed systems, ultimately leading to better performance, reliability, and user satisfaction.

Thank you for reaching this far on this article. If you have liked the article, give it a ‘clap’ and consider following me. I keep publishing articles on distributed systems.

--

--

Mahantesh Ambali

I am working as a Principal Engineer at Landmark Group (Lifestyle).