Skip to main content

Welcome to a Grasp lesson

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

Lesson 58

CAP Theorem Explained

Recommended resources

Hello! In our last lesson, we established the fundamental difference between monolithic and microservices architectures. We concluded that choosing microservices means we are building a distributed system, and this choice comes with a new set of rules and challenges.

Today, we will explore one of the most important of those rules: the CAP theorem. This is a foundational concept in distributed systems and a frequent topic in system design interviews for senior roles. Mastering it will allow you to reason about and justify architectural trade-offs with confidence. Our goal is to explain the CAP theorem and its implications for distributed system design.

1. What is the CAP Theorem?

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 out of the following three guarantees:

  • Consistency (C): Every read request receives the most recent write or an error. In a consistent system, all nodes see the same data at the same time.
  • Availability (A): Every request receives a (non-error) response, without the guarantee that it contains the most recent write. The system is always up and running to serve requests.
  • Partition Tolerance (P): The system continues to operate despite an arbitrary number of messages being dropped (or delayed) by the network between nodes. In simple terms, the system works even if parts of it can't communicate with each other.

To get a clear and concise explanation of these three properties, let's start with a short video.

CAP Theorem in System Design Interviews

The following video from 'Hello Interview' provides an excellent definition of the CAP theorem and its components. It also introduces the single most important insight for applying it in practice.

Watch from the beginning (00:36) to 02:19. Pay close attention to the definitions of C, A, and P, and especially the argument for why Partition Tolerance is a 'must-have' in any modern distributed system.

The Real Choice: Consistency or Availability?

As the video explained, for any real-world distributed system that communicates over a network (like the internet), network failures will happen. This means Partition Tolerance (P) is not optional. A system that isn't partition-tolerant is effectively not a distributed system, because it would fail entirely the moment communication between nodes is disrupted.

Therefore, the CAP theorem in practice forces a trade-off. When a network partition occurs, you must choose between:

  1. Canceling the operation to ensure consistency (sacrificing availability). This leads to a CP system (Consistent and Partition-Tolerant).
  2. Proceeding with the operation and thus risking data inconsistency (sacrificing consistency). This leads to an AP system (Available and Partition-Tolerant).
CAP Theorem Venn Diagram with Database Examples
This diagram illustrates the core trade-off of the CAP Theorem. Since Partition Tolerance (P) is mandatory for distributed systems, the practical choice is between CP (Consistency/Partition Tolerance) and AP (Availability/Partition Tolerance). The examples show which database technologies typically fall into each category.

2. The Trade-Off in Action: Real-World Scenarios

The decision between a CP and an AP system is not a technical one, but a business one. It depends entirely on the requirements of the specific feature you are building. The key question to ask is: "What is the cost of showing a user stale or incorrect data?"

Let's watch the next segment of the same video to see this trade-off illustrated with concrete examples.

CAP Theorem in System Design Interviews

This part of the video explains the trade-off using a simple distributed setup and then applies it to real-world systems you'd encounter in an interview.

Watch from 02:19 to 06:43. Focus on understanding why a ticket booking system must choose consistency, while a social media feed can choose availability.

Let's summarize and solidify those examples:

When to Choose Consistency (CP Systems)

You must prioritize consistency when data inaccuracy could lead to catastrophic business failure, financial loss, or a broken user promise.

  • Financial Systems: Imagine a banking application where a user withdraws funds. If the system chose availability during a partition, the user might be able to withdraw the same money from two different ATMs, resulting in a negative balance. Here, it is far better to return an error (be unavailable) than to process a transaction with stale data.
  • E-commerce Inventory: When a user buys the last item in stock, the system must guarantee that no one else can buy it. An AP system might allow two users to purchase the same last item, leading to an oversold situation. A CP system would lock the item or return an error to the second user.
  • Booking and Reservation Systems: As the video highlighted, double-booking a flight seat or hotel room is a critical failure. The system must ensure a consistent view of available inventory.

In these CP systems, the user experience during a partition is an error message or a loading spinner, but never incorrect data.

When to Choose Availability (AP Systems)

You can prioritize availability when the business impact of temporary data inconsistency is low or negligible. This is the case for the vast majority of web applications.

  • Social Media Feeds: If you see your friend's old profile picture for a few seconds after they've updated it, the experience is not fundamentally broken. It's better to show the old picture than to show an error page.
  • Content Platforms (Netflix, YouTube): If a movie's description is updated, it's acceptable for some users to see the old version for a short while. The core function—watching the video—remains available.
  • E-commerce Product Views: The number of "likes" or views on a product can be eventually consistent. Showing a slightly outdated count does not prevent a user from browsing or buying the product.

In these AP systems, the data will eventually become consistent across all nodes. This model is called eventual consistency.

Test your understanding! (Interview Scenario)

An interviewer asks: "We are designing a system for real-time stock trading. During a network partition between our data centers in New York and London, a user in London places an order to sell 100 shares of a stock. How should the system respond, according to the CAP theorem, and why?"

Show answer

This scenario involves sensitive financial transactions where data integrity is paramount. The correct choice is to prioritize Consistency over Availability (a CP system).

Here's a strong answer:

"For a stock trading system, consistency is non-negotiable. Therefore, I would design it as a CP system.

During the network partition, the London data center cannot confirm the current, authoritative state of the user's portfolio or the stock's order book from the New York data center (which might be the primary).

If we chose Availability (AP), the London system might accept the sell order based on stale data. This could lead to severe consequences:

  1. Overselling: The user might not actually own 100 shares if a recent purchase in New York hasn't replicated yet.
  2. Incorrect Pricing: The trade might execute at a price that doesn't reflect the most recent trades happening in New York.

To prevent this, the system must choose Consistency. When the user in London attempts to sell, the system, unable to achieve a consistent state across the partition, must reject the request. It should return an error message like, 'Trading is temporarily unavailable. Please try again in a few moments.' This makes the service temporarily unavailable for that user but preserves the integrity of the financial records, which is the top priority."


3. Implications for System Design

Understanding the C vs. A trade-off is the first step. For a senior role, you must be able to explain how this choice influences your actual system architecture and technology selection.

CAP Theorem for System Design Interviews

Let's read a concise article that links the CAP theorem choice to concrete design decisions and technologies. This is the 'so what?' part that interviewers are looking for.

Read the section titled 'CAP Theorem in System Design Interviews'. Focus on the bullet points that describe design choices and technology choices for both consistency- and availability-prioritized systems.

Let's summarize the key design implications from the article:

If you prioritize... Your design might include... Technology choices could be...
Consistency (CP) - Synchronous replication
- Distributed transactions (e.g., Two-Phase Commit)
- Single master databases
- Traditional RDBMS (PostgreSQL, MySQL)
- Google Spanner
- NoSQL DBs in strong consistency mode (e.g., DynamoDB, MongoDB)
Availability (AP) - Asynchronous replication
- Eventual consistency patterns
- Multi-master or leaderless replication
- Client-side conflict resolution
- Cassandra
- Riak
- CouchDB
- DynamoDB (default eventual consistency mode)

Your experience with Java Spring Boot is relevant here. For example, if you build a CP system, you might use Spring's @Transactional annotation with a single PostgreSQL database. If you build an AP system, you might use asynchronous events with a message broker like Kafka to propagate changes between services, each backed by its own eventually consistent data store like Cassandra.


4. Advanced Considerations for Senior Interviews

To stand out in a senior-level interview, you need to show a nuanced understanding that goes beyond the basic CP vs. AP dichotomy.

  • It's Not All or Nothing: A complex system is rarely purely CP or purely AP. Different features within the same application can have different requirements. For example, in an app like Ticketmaster:
    • Booking a ticket must be CP to prevent double-booking.
    • Browsing events can be AP; showing a slightly outdated event description is acceptable.
  • The Spectrum of Consistency: Consistency is not a binary choice. There are many levels between "strong" and "eventual." Briefly knowing about these shows depth:
    • Read-your-own-writes: After a user updates their data, they should always see their own update immediately, even if other users see stale data for a while. This is very common in user-facing applications.
    • Causal Consistency: If event B happens because of event A, no one should see event B without also seeing event A. (e.g., you shouldn't see a reply to a comment before you see the original comment).
  • Beyond CAP: The PACELC Theorem: For a truly advanced discussion, you can mention the PACELC theorem. It extends CAP by stating that in a distributed system:
    • if there is a Partition, one must choose between Availability and Consistency;
    • Else (if there is no partition), one must choose between Latency and Consistency.
PACELC Theorem Flowchart
This flowchart shows how PACELC extends CAP. When a partition occurs (the 'Yes' path), you face the classic CAP trade-off (A vs. C). When the system is operating normally (the 'No' path), you face a different trade-off: lower latency or stronger consistency.

Mentioning PACELC shows that you understand that even during normal operation, there's a constant tension between how fast you respond (Latency) and how up-to-date your data is (Consistency).


Conclusion

Today we dissected the CAP theorem, one of the most important principles in distributed systems design. You are now equipped to discuss it confidently in an interview setting.

Key Takeaways:

  • The CAP theorem states a distributed system can only provide two of three guarantees: Consistency, Availability, and Partition Tolerance.
  • Since Partition Tolerance (P) is non-negotiable in modern systems, the real choice is between Consistency (CP) and Availability (AP).
  • The choice is a business decision: prioritize consistency when data correctness is critical (e.g., finance); prioritize availability when temporary staleness is acceptable (e.g., social media).
  • This choice has direct implications for your architecture and technology stack (e.g., SQL vs. NoSQL, synchronous vs. asynchronous communication).
  • For senior-level discussions, demonstrate nuance by discussing how CAP applies differently to various parts of a system and by showing awareness of concepts like the consistency spectrum and the PACELC theorem.

Preview of the Next Lesson:
We've just learned about the high-level trade-offs we must make when designing distributed systems. But how do we decide what goes into each microservice in the first place? In our next lesson, we will explore this by learning how to apply core Domain-Driven Design (DDD) principles (Bounded Context, Aggregate) to define service boundaries. This will give you a powerful framework for breaking down a complex domain into logical, independent microservices.