Skip to main content

Welcome to a Grasp lesson

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

Lesson 69

Understanding the CAP Theorem

Recommended resources

Hello! Welcome to the final lesson of our module on Database Fundamentals.

In our last lesson, we explored the practical scenarios where you might choose a NoSQL database over a traditional relational one. We discussed the trade-offs between the strict consistency and data integrity of SQL databases and the immense scalability and flexibility of NoSQL systems. You learned that for large-scale distributed applications, prioritizing availability might be more important than ensuring every user sees the exact same data at the exact same millisecond.

Today, we'll explore the fundamental principle that governs these trade-offs. This isn't just a design preference; it's a core concept in distributed systems. Our goal is to explain the CAP theorem (Consistency, Availability, Partition tolerance) at a high level. Understanding this theorem will give you the "why" behind the architectural choices of distributed databases like MongoDB and Cassandra, providing a solid theoretical foundation for the practical skills you're building.

What is the CAP Theorem?

The CAP theorem, also known as Brewer's theorem, states that it is impossible for a distributed data store to simultaneously provide more than two out of the following three guarantees:

  1. Consistency (C): All clients see the same data at the same time, no matter which node they connect to. In simpler terms, every read receives the most recent write.
  2. Availability (A): Every request receives a (non-error) response – without the guarantee that it contains the most recent write.
  3. Partition Tolerance (P): The system continues to operate despite an arbitrary number of messages being dropped (or delayed) by the network between nodes. A "partition" is a communication break within the system.

The core idea is that you have to pick two.

CAP Theorem Explained with Venn Diagram
This Venn diagram illustrates the core principle of the CAP theorem: a distributed system can be Consistent and Available (CA), Consistent and Partition Tolerant (CP), or Available and Partition Tolerant (AP), but it cannot be all three at once.

To start, let's get a clear, concise definition of these three terms with a simple animated explanation.

CAP Theorem Simplified

This short video from ByteByteGo, 'CAP Theorem Simplified', does an excellent job of introducing the three components of the theorem.

Please watch the first minute of the video (from 00:07 to 00:57). Focus on grasping the basic definition of Consistency, Availability, and Partition Tolerance.

To summarize and reinforce those points:

  • Consistency: Imagine you update your username on a website. If the system is consistent, you and all other users will immediately see the new username, regardless of where you are in the world.
  • Availability: The system is always ready to respond to a request. Even if part of the system is down or slow, it will still give you an answer, though it might be slightly out-of-date.
  • Partition Tolerance: Given your experience with network troubleshooting, you know that networks are not perfectly reliable. Packets get dropped, routers fail, and data centers can lose connectivity. A partition-tolerant system is designed to keep functioning even when these network failures split the system into isolated groups (partitions).

The Real-World Choice: P is a Must-Have

In theory, you can choose any two of the three. However, for any system that is distributed—meaning it runs on multiple computers connected by a network—you cannot avoid partitions. Network failures will happen. Therefore, any real-world distributed system must have Partition Tolerance (P).

This means the actual, practical trade-off for a distributed database is between Consistency and Availability. When a network partition occurs, a system designer must make a choice:

  1. Cancel the operation to ensure consistency, thus making the system unavailable. (Choose C over A)
  2. Proceed with the operation, thus making the system available, but risk data inconsistency. (Choose A over C)

The following clip uses a great analogy of a banking system with two ATMs to make this choice tangible.

CAP Theorem Simplified

Let's return to the 'CAP Theorem Simplified' video to see the core trade-off in action.

Please watch from 00:57 to 02:48. Notice how the system must decide what to do when the two ATMs can't communicate. Does it stop all transactions (prioritizing Consistency) or allow them to proceed and risk an overdraft (prioritizing Availability)?


The Two Architectures: CP and AP

Since any distributed system must handle partitions (P), two main system types emerge:

1. CP (Consistency and Partition Tolerance)

  • What it is: When a partition occurs, the system sacrifices availability to maintain consistency. To prevent conflicting data, the system may refuse to accept writes or may even stop responding to requests on the affected part of the system until the partition is resolved.
  • When to use it: In systems where data correctness is non-negotiable. Think banking systems, financial trading platforms, and other transactional systems where you cannot tolerate ambiguity.
  • Example: MongoDB is often configured as a CP system. It uses a primary-secondary architecture. Writes go to a single primary node, which then replicates them to secondary nodes. If a partition happens and the primary node becomes unreachable, the system must hold an "election" to promote a secondary to be the new primary. During this election period, the system is unavailable for writes to guarantee no inconsistent data is written.

2. AP (Availability and Partition Tolerance)

  • What it is: When a partition occurs, the system sacrifices consistency to maintain availability. All nodes remain able to accept reads and writes. This means different users might see slightly different data for a short time. The system will eventually resolve these inconsistencies once the partition ends, a model known as eventual consistency.
  • When to use it: In systems where uptime is critical and slight, temporary inconsistencies are acceptable. Think of social media "likes," e-commerce shopping carts, or high-volume logging systems.
  • Example: Apache Cassandra is a classic AP system. It uses a masterless architecture where you can write to any node. If a partition occurs, nodes on both sides of the split can still accept writes. When the network is restored, the nodes communicate to reconcile the conflicting data.

The following video provides a great walkthrough of how MongoDB and Cassandra exemplify these two choices.

What is CAP Theorem?

The video 'What is CAP Theorem?' from IBM Technology gives clear examples of databases that fall into the CP and AP categories.

Watch from 03:13 to 07:00. Focus on how the architecture of MongoDB (primary/secondary) leads it to be a CP system, and how the architecture of Cassandra (no primary server) leads it to be an AP system.

The sketchnote below provides a fantastic visual summary of the different architectural choices. Notice how the CP and AP diagrams reflect the MongoDB and Cassandra architectures discussed in the video.

CAP Theorem Sketchnote
This sketchnote visually contrasts the CA (non-distributed), AP (replicated), and CP (primary-secondary) architectural models, linking them to the CAP theorem's trade-offs.

Consolidating Your Understanding

To bring all these concepts together and see how they apply to making real-world database choices, let's turn to a comprehensive article.

The CAP Theorem: Navigating the Trade-offs in Distributed ...

The article 'The CAP Theorem: Navigating the Trade-offs' provides a clear, written explanation of the concepts we've just watched, connecting them directly to use cases and specific database examples.

Please read the following sections of the article: Start with 'Breaking Down the CAP Theorem' for a quick recap of the definitions. Read the section 'The Three Core Scenarios', focusing on the descriptions and use cases for CP and AP systems. (You can skim the 'CA' section, as it's less relevant for distributed systems). Finally, read 'Making the Right Database Choice' to see how these principles guide the selection of a database for a specific job. This reading will help solidify the connection between the abstract theorem and the practical decisions developers make.

Test your understanding!

You are designing the backend for a large e-commerce platform. The system needs to handle two main functions:

  1. Product Inventory Management: Tracking the exact stock count of every item in warehouses across the world.
  2. User Shopping Carts: Allowing millions of users to add and remove items from their carts.

A network partition occurs between your US and European data centers. Based on the CAP theorem, which model (CP or AP) would you choose for each function, and why?

Show answer
  1. Product Inventory Management: CP (Consistency over Availability).

    • Reasoning: Data integrity is paramount. You cannot risk selling the same item twice because two different data centers think it's still in stock. If a partition occurs, it's better to make the inventory system temporarily unavailable for updates in one region than to sell products you don't have. The cost of inconsistency (overselling, customer dissatisfaction) is very high.
  2. User Shopping Carts: AP (Availability over Consistency).

    • Reasoning: Uptime is the priority here. You want users to always be able to add items to their cart, even if the network is having issues. The cost of inconsistency is low. If a user adds an item to their cart in the US, and it takes a few seconds to sync with their session in Europe, the user experience is not significantly impacted. The system can achieve eventual consistency once the partition is resolved. Denying a user the ability to add to their cart (making the system unavailable) is a much worse outcome.

Conclusion

You have now reached the end of the course and our first module on database fundamentals. By understanding the CAP theorem, you've moved beyond just knowing different database types to grasping the core principles that dictate their design and behavior in the real, imperfect world of distributed networks.

Key Takeaways:

  • The CAP Theorem states that a distributed system can only guarantee two of three properties: Consistency, Availability, and Partition Tolerance.
  • Because network Partitions (P) are a fact of life in distributed systems, the essential trade-off is between Consistency (C) and Availability (A).
  • CP systems (e.g., MongoDB, most relational DBs in a distributed setup) prioritize data correctness. During a partition, they will sacrifice availability to prevent inconsistent data.
  • AP systems (e.g., Cassandra, DynamoDB) prioritize uptime. During a partition, they will sacrifice immediate consistency to remain available, relying on an eventual consistency model to sync up later.

Preview of the Next Module:
With this strong theoretical foundation in place, we are ready to dive deep into the practical skills of SQL. The next module, "The Relational Model and SQL Fundamentals," will switch gears back to hands-on practice. We will begin by learning how the relational model organizes data and how to write your very first SELECT statements to retrieve data from a table.