Hello! Welcome to the third module of our course, "Distributed Data Storage & Systems."
In our last module, we explored the diverse world of NoSQL databases, identifying which business scenarios are best suited for document, key-value, graph, or column-family stores. We saw that different features of a single application might require different database types.
Today, we delve into one of the most fundamental principles that explains why these databases have such different behaviors and trade-offs. We will tackle the CAP Theorem, a cornerstone of distributed systems theory. Your goal for this lesson is to explain the CAP theorem and its implications for distributed system design. Understanding this theorem is not just academic; it is a critical tool that will guide your architectural decisions when choosing and designing data platforms.
The Three Pillars of Distributed Systems
The CAP theorem, first proposed by computer scientist Eric Brewer, states that it is impossible for a distributed data store to simultaneously provide more than two of the following three guarantees:
- Consistency
- Availability
- Partition Tolerance
Let's look at a visual representation of this concept.

To build a solid foundation, let's get precise definitions for each of these terms.
The article 'CAP Theorem' from cycle.io provides clear, practical definitions for each of the three pillars. It also introduces different levels of consistency, which will be useful later.
Please read the section titled 'The Three Pillars of the CAP Theorem', focusing on the definitions and examples for Consistency, Availability, and Partition Tolerance.
The Inevitable Choice: P is a Given
A common misunderstanding of the CAP theorem is that you can simply "pick any two" of the three guarantees. However, in modern, large-scale systems that span multiple servers or data centers, network partitions are a fact of life. A network partition is a failure in communication between nodes in the system—a server might crash, or a network link might be severed.
Because you cannot wish away network failures, Partition Tolerance (P) is not optional. A system that is not partition-tolerant would fail entirely if a single network connection broke. Therefore, any practical distributed system must be partition-tolerant.
This simplifies the dilemma: when a network partition happens, you are forced to make a trade-off between Consistency and Availability.
- You can choose Consistency over Availability (a CP system).
- You can choose Availability over Consistency (an AP system).
Let's watch a short video that explains this core trade-off very effectively.
CAP Theorem in System Design Interviews
This video, 'CAP Theorem in System Design Interviews', provides a fantastic, practical walkthrough of the theorem. It clearly explains why Partition Tolerance is a must-have and what the resulting choice between Consistency and Availability means.
Watch the segment from 00:36 to 04:04. Pay close attention to the definition of the three terms and the example of the USA vs. Europe servers, which perfectly illustrates why you must sacrifice either C or A during a network failure.
As the video showed, when the link between the US and European servers is down:
- To ensure Consistency, the system must stop responding to requests that might return stale data (sacrificing Availability).
- To ensure Availability, the system must respond with the data it has, even if it's stale (sacrificing Consistency).
Scenarios and Use Cases: When to Choose C vs. A
As an architect, your main task is to decide which guarantee is more critical for a given business requirement. This choice has profound implications for your system's behavior and user experience.
Prioritizing Consistency (CP Systems)
When is it absolutely critical that every user sees the most up-to-date data, even if it means the system might be temporarily unavailable to some users?
- Financial Systems: A bank transfer must be reflected consistently across all parts of the system. Showing an outdated balance is unacceptable.
- E-commerce Inventory: If a customer buys the last item in stock, no other customer should be able to see that item as available. Double-selling is a catastrophic failure.
- Reservation Systems: Booking a specific seat on an airplane or a room in a hotel requires strong consistency to prevent double-booking.
Prioritizing Availability (AP Systems)
When is it more important that the system is always responsive, even if it means some users might see slightly outdated information for a short period?
- Social Media: If a user updates their profile picture, it's acceptable if their friends see the old picture for a few seconds or minutes. Uptime and responsiveness are more important.
- Product Catalogs: For an e-commerce site, it's better to show a slightly old product description than to return an error and lose a potential customer.
- Content Platforms (e.g., Netflix): Seeing a slightly out-of-date movie description is a minor inconvenience compared to the service being down.
This concept of accepting temporary inconsistencies for the sake of availability is known as eventual consistency. The system guarantees that, if no new updates are made, all replicas will eventually converge to the same value.
Test your understanding!
Scenario: You are designing a system that collects and displays votes for a live, televised talent show. Millions of votes are cast per minute via a mobile app.
Question: During a network partition, should the system prioritize Consistency (C) or Availability (A)? Justify your choice.
Show answer
Answer: Prioritize Availability (A).
- Justification: The most critical requirement for this system is to be able to accept every vote cast by a user. If the system chose Consistency, it might have to reject incoming votes during a partition to ensure its internal vote counts are perfectly synchronized. This would lead to a terrible user experience and inaccurate final results because many votes would be lost.
- It is far better to be available to accept all votes (an AP system) and tolerate minor, temporary discrepancies in the vote count displayed on screen. The system can reconcile the counts later. Eventual consistency is perfectly acceptable here; the final, correct tally is what matters, not a perfectly consistent real-time view every single millisecond.
Architectural Implications and Database Choices
The choice between CP and AP is not just a theoretical exercise; it directly dictates the architecture of your system and the technologies you choose. This is where the NoSQL databases we discussed in the previous module come back into focus. Their very designs are a reflection of these CAP trade-offs.
To see this in action, let's examine the architectures of two popular NoSQL databases: MongoDB (which prioritizes C) and Apache Cassandra (which prioritizes A).
The video 'What is CAP Theorem?' from IBM Technology gives a clear architectural breakdown of how CP and AP systems are implemented.
Please watch from 03:13 to 07:00. Notice the difference between MongoDB's primary-secondary model (all writes go to one place for consistency) and Cassandra's peer-to-peer model (all nodes can accept writes for availability).
This leads us to a crucial cheat sheet for any data architect. Different databases are built with different CAP priorities in mind. The following table, adapted from the cycle.io article, is a great starting point.
| Database | CAP Preference | Notes |
|---|---|---|
| Postgres | CP | A traditional relational database that prioritizes strong consistency. |
| MongoDB | CP (configurable) | Prioritizes consistency by default through its primary-replica architecture. |
| Google Spanner | CP | Achieves global consistency through specialized hardware (atomic clocks). |
| Apache Cassandra | AP | Designed for massive scalability and high availability; uses eventual consistency. |
| Amazon DynamoDB | AP | Always available by design, with eventual consistency as the default (though strong consistency is an option). |
It's also important to note, as you know from your experience as an enterprise architect, that a large-scale system is rarely monolithic. Different services within the same application can—and should—have different CAP priorities.
For example, an e-commerce platform might use:
- A CP database like PostgreSQL for its critical payment processing service.
- An AP database like Cassandra for its shopping cart and user session services, where uptime is paramount.
This practice of using the right tool for the job is a hallmark of modern data architecture.
Conclusion
In this lesson, we dissected the CAP theorem, one of the most important constraints in distributed system design. You now have a framework for analyzing the fundamental trade-offs involved in building resilient and scalable data platforms.
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 inevitable, the real architectural choice is between Consistency (CP) and Availability (AP).
- The choice depends on the business need: CP systems are for use cases where correctness is critical (e.g., banking, inventory), while AP systems are for use cases where uptime is paramount (e.g., social media, caching).
- This decision directly informs your choice of database technology (e.g., PostgreSQL/MongoDB for CP, Cassandra/DynamoDB for AP) and overall system architecture.
Our discussion of "strong" vs. "eventual" consistency naturally leads to our next topic. The choice between a CP and an AP system often aligns with a choice between two broader consistency models: ACID and BASE. In our next lesson, we will compare these two models to understand their specific guarantees and how they relate to the CAP theorem.