Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 6

Understanding the CAP Theorem

Recommended resources

Hello! Welcome to the final lesson of our first module on the foundations of distributed systems.

In our previous lesson, we established the crucial distinction between safety ("nothing bad ever happens") and liveness ("something good eventually happens"). We saw that robust systems prioritize safety, sometimes at the expense of conditional liveness.

This lesson builds directly on that foundation to tackle one of the most famous, and often misunderstood, principles in our field. We will address the learning outcome: Explain the CAP theorem (Consistency, Availability, Partition Tolerance) and its practical implications for system design.

The CAP theorem is essentially a formal statement about the trade-off between a specific safety property (Consistency) and a specific liveness property (Availability) when faced with the reality of network failures. For a senior engineer, understanding not just the theorem but also its nuances and limitations is key to making sound architectural decisions.

The Core Idea: Consistency, Availability, and Partition Tolerance

The CAP theorem, first conjectured by Eric Brewer, states that a distributed data store can only provide two of the following three guarantees:

  1. Consistency (C): All nodes see the same data at the same time. Technically, this refers to linearizability, a very strong form of consistency. It means that once a write completes, any subsequent read, regardless of which node it hits, will return that new value. This is a safety property: the "bad thing" of reading stale data after a write has completed must not happen.

  2. Availability (A): Every request receives a non-error response, without the guarantee that it contains the most recent write. Essentially, the system remains operational for both reads and writes, even if some nodes are down or can't communicate. This is a liveness property: the system "eventually" (in this case, immediately) does something good (responds).

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

In any real-world distributed system that communicates over a network, partitions are a fact of life. Networks fail. This means Partition Tolerance (P) is not a choice; it's a requirement.

Therefore, the CAP theorem forces a critical choice upon us: when a network partition occurs, we must choose between Consistency and Availability.

Let's watch a short video that clearly explains this fundamental trade-off.

CAP Theorem in System Design Interviews

This video from Hello Interview provides a clear, practical overview of the CAP theorem. It defines the three properties and uses a simple, intuitive example to show why a partition forces a choice between C and A.

Watch from 00:36 to 03:53. Focus on the definitions of Consistency, Availability, and Partition Tolerance, and how the network failure scenario illustrates the core dilemma.

As the video illustrates, during a partition between two nodes (say, in US and EU datacenters):

  • To choose Consistency (CP), if the US node accepts a write but cannot replicate it to the EU node, the EU node must stop serving requests (or at least, return an error) to avoid serving stale data. Consistency is preserved, but availability is sacrificed.
  • To choose Availability (AP), the EU node continues to serve requests, even though it may have stale data. The system is available, but consistency is sacrificed.

This trade-off is at the heart of many system design decisions.

CAP Theorem Venn Diagram with Database Examples
This Venn diagram illustrates the trade-off. Since Partition Tolerance (P) is a given in distributed systems, the real choice is between a CP system (like HBase) and an AP system (like DynamoDB or Cassandra). A single-node RDBMS can be CA, but it is not a distributed system and thus not partition-tolerant.

Practical Implications and System Archetypes

This choice between CP and AP directly maps to business requirements and influences your choice of technology.

CAP Theorem in System Design Interviews

Now, let's explore which kinds of systems prioritize Consistency versus Availability and see how those decisions shape their architecture.

Watch from 03:53 to 09:24. Pay close attention to the use cases for CP systems (finance, booking) and AP systems (social media), and how these choices influence technology decisions (e.g., using distributed transactions vs. multiple replicas).

Let's summarize the two main archetypes:

System Type Priority Behavior During Partition Use Cases Technology Examples
CP Consistency over Availability Returns an error or times out for requests that cannot be guaranteed to be consistent. Financial ledgers, payment processing, inventory management, user authentication. etcd, Consul, ZooKeeper, Relational Databases (e.g., PostgreSQL) in a default single-master setup.
AP Availability over Consistency Responds with the best data it has, which might be stale. The system converges to consistency later (eventual consistency). Social media feeds, post likes, view counters, real-time analytics dashboards. Amazon DynamoDB, Apache Cassandra, Riak, CouchDB.
Test your understanding!

You are designing the backend for a large e-commerce platform. Consider two distinct features:

  1. The shopping cart service, which allows users to add and remove items.
  2. The final payment processing and inventory deduction service.

For each service, would you lean towards a CP or AP design? Justify your choice based on the business impact of inconsistency vs. unavailability.

Show answer
  1. Shopping Cart Service (AP): It's generally better to lean towards Availability. If a network partition occurs and a user adds an item to their cart, it's acceptable for that update to take a few seconds to sync across all replicas. The cost of unavailability (a user can't add items to their cart) is high, while the cost of temporary inconsistency (the cart count is briefly out of sync between two devices) is low. The final check can happen at the payment stage.

  2. Payment and Inventory Service (CP): This service demands Consistency. You absolutely cannot have a situation where two customers buy the last item in stock due to a partition (a "bad thing," violating safety). It is far better to return an error to one customer (sacrificing availability) than to sell an item you don't have. This ensures data correctness, which is paramount for financial transactions and inventory management.


Beyond the Dichotomy: A Senior-Level Critique

As with many models, the simple CP/AP classification, while useful, can be misleading. For your role, it's crucial to understand the limitations and nuances of the CAP theorem.

First, the choice isn't always system-wide. Different components of a single application can make different choices, as we just saw with the e-commerce example.

Second, "Consistency" itself is not a binary concept. The "C" in CAP specifically means linearizability, the strongest model. But there are many weaker, useful consistency models.

Let's watch a final clip that introduces these more advanced ideas.

CAP Theorem in System Design Interviews

For a senior-level discussion, it's important to see CAP not as a single choice for an entire system, but as a nuanced decision. This clip, marked as 'Advanced' in the video, explores that nuance.

Watch from 09:24 to 13:07. Focus on two key ideas: 1) Different parts of a system can have different CAP priorities. 2) 'Consistency' is a spectrum, with models weaker than linearizability, like causal consistency and read-your-own-writes.

This brings us to a critical perspective from Martin Kleppmann, a leading author in distributed systems. He argues that the CP/AP labels are a "false dichotomy" because most real-world systems don't neatly fit into either bucket under the theorem's strict, formal definitions.

Please stop calling databases CP or AP

Martin Kleppmann argues that we should move beyond the simple CP/AP classification. His article provides a critical, in-depth perspective that's essential for a senior engineer's understanding.

This is a detailed read. Focus on these key sections and arguments: Introduction & Narrow Definitions: Understand Kleppmann's main thesis and the very specific definitions of C (linearizability) and A (any non-failing node must respond) that the theorem uses. Linearizability: Grasp the football world cup example to get an intuition for what CAP-Consistency really means. CAP-Availability & Proof: Read the two-datacenter example, which is a simple walk-through of the theorem's proof. Critique: Read the sections 'Many systems are neither linearizable nor CAP-available' and 'CP/AP: a false dichotomy'. Focus on his examples (e.g., single-leader databases, MongoDB) and why they don't fit neatly into the CP or AP buckets.

Kleppmann's core argument is that because the formal definitions are so strict, many common systems are technically neither CP nor AP. For example, a standard single-leader database with asynchronous replication:

  • It's not CAP-Consistent (CP) because a read from a follower can be stale.
  • It's not CAP-Available (AP) because if a client is partitioned from the leader, it cannot perform writes.

This doesn't mean these systems are bad; it just means the CAP model is too simple to describe them fully. The real world is a spectrum of trade-offs, not a binary choice.


A More Complete Model: PACELC

To address the shortcomings of CAP, the PACELC theorem was proposed. It provides a more complete picture by considering the trade-offs during normal operation as well.

PACELC states:

  • If there is a Partition, a system must choose between Availability and Consistency.
  • Else (when operating normally), a system must choose between Latency and Consistency.

The "Else" part is the key extension. Even without a partition, to achieve low Latency (L), a system might read from the nearest replica, risking stale data. To guarantee Consistency (C), a read might need to coordinate with other nodes, which increases latency.

CAP vs. PACELC Theorem and System Approaches
This diagram illustrates both the CAP theorem (top) and the PACELC extension (bottom). PACELC acknowledges the trade-off between Latency and Consistency that exists even when the network is healthy.

This gives us a more nuanced classification:

  • PA/EL (e.g., DynamoDB, Cassandra): Chooses Availability during partitions and Latency during normal operation. Optimized for being always on and fast, at the cost of consistency.
  • PC/EC (e.g., PostgreSQL, BigTable): Chooses Consistency during partitions and Consistency during normal operation. Optimized for correctness above all else.

Conclusion

This lesson took you from the basic definition of the CAP theorem to a more nuanced, senior-level critique. While CAP is a foundational concept, it's the beginning of the conversation, not the end.

Key Takeaways:

  • The CAP theorem states that during a network partition, a distributed system must choose between Consistency (a safety property) and Availability (a liveness property).
  • Because Partition Tolerance is mandatory, the choice is between building a CP system (prioritizing correctness) or an AP system (prioritizing uptime).
  • This choice has direct practical implications for application use cases and technology selection.
  • The CP/AP labels are a simplification. The formal definitions are strict (C = linearizability), and many real-world systems don't fit neatly into either category.
  • More advanced models like PACELC provide a fuller picture by also considering the trade-off between Latency and Consistency during normal operation.

Preview of the next lesson:

This lesson concludes our foundational module. You are now equipped with the core concepts of partial failures, time, ordering, system guarantees, and the CAP theorem.

In the next module, Data Consistency and Replication, we will dive much deeper into the "C" of CAP. We will explore the full spectrum of consistency models, from the strong linearizability we've discussed here to weaker but highly practical models like eventual and causal consistency. We will see how these models are implemented using different replication strategies.