Skip to main content

Welcome to a Grasp lesson

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

Lesson 21

CAP Theorem: Consistency vs. Availability

Recommended resources

Hello! Welcome back to your interview preparation course.

In our previous lesson, we explored database sharding as a way to scale beyond the write and storage limits of a single database. By partitioning data across multiple machines, we created a distributed system. This raises a fundamental question: what happens when those machines can't communicate with each other?

This lesson dives into the CAP Theorem, a cornerstone principle of distributed systems that governs this exact scenario. Your ability to apply this theorem is a critical signal of seniority in a system design interview, as it demonstrates a deep understanding of the fundamental trade-offs involved in building resilient, large-scale systems.

The learning outcome for this lesson is to: Apply the CAP theorem to make informed decisions about consistency vs. availability. We'll go beyond the definition and focus on how to use it as a practical tool during an interview to justify your design choices.

1. The Three Guarantees of Distributed Systems

The CAP theorem, first proposed by computer scientist Eric Brewer, states that a distributed system can simultaneously provide at most two out of three specific guarantees: Consistency, Availability, and Partition Tolerance.

To get a clear definition of these three terms, let's start with a video.

CAP Theorem in System Design Interviews

This video from 'Hello Interview' provides an excellent breakdown of the CAP theorem specifically for interviews. We'll start with the core definitions.

Watch from 00:36 to 02:19. Focus on the definitions of Consistency, Availability, and Partition Tolerance, and the key insight about why Partition Tolerance is non-negotiable in most systems.

Let's summarize those definitions:

  • Consistency (C): Every read from the database receives the most recent write or an error. In simpler terms, all clients see the same data at the same time, no matter which node they connect to.
  • 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 able to respond.
  • Partition Tolerance (P): The system continues to operate despite network failures (partitions) that prevent communication between nodes.

The most crucial takeaway for any modern system design interview is that Partition Tolerance is a must. You are designing systems that run on commodity hardware across different racks, data centers, or even continents. Network failures will happen. Therefore, you must design your system to be partition tolerant.

This simplifies the CAP theorem into a direct trade-off: in the event of a network partition, you must choose between Consistency and Availability.

CAP vs PACELC Theorem and System Approaches
This diagram visually defines Consistency, Availability, and Partition Tolerance and illustrates the three combinations of trade-offs: CA, AP, and CP. In practice, because network partitions are a given, the real choice is between AP and CP.

2. The Core Dilemma: A Practical Example

So, why are consistency and availability at odds during a partition? Let's walk through a scenario.

Imagine our sharded database from the last lesson. Let's say we have two replicas of a user's data, one on a server in the US and one in Europe, to provide low-latency reads for global users.

  1. A user in the US updates their profile name. The write goes to the US server.
  2. The US server attempts to replicate this change to the European server.
  3. Suddenly, the network connection between the US and Europe is severed—a network partition.

Now, a user in Europe tries to read that same profile. What should the European server do? It knows it can't reach the US server and might have stale data. This is the moment of choice:

  • Prioritize Consistency (CP - Consistent and Partition-Tolerant): The European server returns an error or blocks the request until the partition heals. It refuses to serve data that might be incorrect. The system is not available for that user.
  • Prioritize Availability (AP - Available and Partition-Tolerant): The European server returns the old profile name it has in its local storage. The data is stale, but the system remains available to the user. The data is not consistent.

CAP Theorem Simplified

The 'Hello Interview' video illustrates this exact scenario. The ByteByteGo video also provides a great example using bank ATMs, which helps solidify the concept from a different angle.

Watch from 00:57 to 02:48. Notice how the choice between consistency and availability has direct financial consequences in the ATM example.


3. Making the Decision: Use Cases and Trade-offs

As a senior engineer, your value in an interview is not just reciting the theorem but applying it to the problem at hand. The decision between C and A depends entirely on the business requirements. You should ask yourself: "What is the user impact or business risk if the data is temporarily inconsistent?"

When to Prioritize Consistency (CP)

You choose consistency when stale or incorrect data would be catastrophic.

  • Financial Systems: Banking transactions, stock trading. You cannot show an incorrect account balance or stock price.
  • E-commerce Inventory: You can't sell the same last item to two different customers. The inventory count must be strongly consistent.
  • Reservation Systems: Booking a flight seat, hotel room, or concert ticket. Double-booking is unacceptable.

In these cases, it's better to tell a user "The service is temporarily unavailable, please try again" than to perform an incorrect transaction.

When to Prioritize Availability (AP)

You choose availability when the system being responsive is more important than having perfectly up-to-date data. For these systems, eventual consistency is acceptable.

  • Social Media: A user seeing a slightly old profile picture, or their new post not appearing for a few seconds to their friends, is not a critical failure. High availability for browsing is key.
  • Content & Media Platforms (Netflix, YouTube): If a video's "like" count is a few seconds out of date, or a new movie description takes a minute to propagate, the user experience is largely unaffected.
  • Review Sites (Yelp): It's better to show slightly outdated restaurant hours than to show an error page, which might cause the user to go to a competitor.

CAP Theorem for System Design Interviews

To reinforce these use cases, review this article from Hello Interview. It provides a clear, written summary of when to choose each path.

Read the sections 'When to Choose Consistency' and 'When to Choose Availability'. This will give you a quick reference for common examples.


4. How Your Choice Impacts the Architecture

Stating your choice is step one. Step two is explaining how that choice translates into technology and design patterns.

CAP Theorem in System Design Interviews

Let's return to the 'Hello Interview' video to see how these choices influence your actual system design.

Watch from 06:43 to 09:24. Pay close attention to the lists of technologies and design patterns associated with both consistency and availability.

Here’s a summary of the architectural implications:

If you prioritize... Your design might include... Technology choices might be...
Consistency (CP) - Distributed Transactions (e.g., Two-Phase Commit): Guarantees that a transaction across multiple services or databases either completes everywhere or nowhere. This is complex and adds latency.
- Synchronous Replication: Writes are not confirmed until they are replicated to a quorum of nodes.
- Single Leader Architectures: All writes go through a single node to enforce order.
- RDBMS: PostgreSQL, MySQL
- NewSQL: Google Spanner, CockroachDB
- NoSQL (in strong consistency mode): DynamoDB, MongoDB
Availability (AP) - Asynchronous Replication (e.g., Change Data Capture): The primary node acknowledges the write immediately and replicates it in the background.
- Multiple Leaders / Leaderless Architectures: Any node can accept writes, with conflicts resolved later.
- Client-side conflict resolution.
- NoSQL (default modes): Cassandra, DynamoDB
- Search Indices: Elasticsearch
- Caching: Redis clusters

5. Advanced Nuances for Senior Roles

For a senior-level interview, you should demonstrate a more nuanced understanding than a simple binary choice. Here are two ways to do that.

1. Different Features, Different Choices

A complex system isn't monolithic. Different features can have different CAP priorities.

  • Ticketmaster:
    • Booking a ticket: Requires strong consistency (CP) to prevent double-booking.
    • Browsing events: Can prioritize availability (AP). Showing a slightly outdated event description is fine.
  • Tinder:
    • Matching: Requires consistency (CP). If two people swipe right, the match should be created and visible to both immediately.
    • Viewing profiles: Can prioritize availability (AP). Seeing an old profile picture for a few seconds is acceptable.

Articulating this shows you can apply the theorem with precision, not just as a blanket rule.

2. The Spectrum of Consistency

"Consistency" in the CAP theorem usually implies strong consistency. However, there's a whole spectrum of weaker consistency models that are often sufficient when you choose availability.

CAP Theorem in System Design Interviews

This final segment from the 'Hello Interview' video is explicitly aimed at senior candidates and covers these advanced topics.

Watch from 09:24 to 13:00. Focus on the examples of mixed requirements (Ticketmaster, Tinder) and the definitions of different consistency levels.

Key consistency models to be aware of:

  • Strong Consistency: The strictest model. All reads see the most recent committed write.
  • Read-Your-Own-Writes Consistency: A user will always see their own updates immediately. This is a very common and intuitive guarantee.
  • Eventual Consistency: The default for AP systems. The system will eventually become consistent, but there's no guarantee on how long it will take.

Mentioning these demonstrates a deeper knowledge that is expected at your level of experience.


Conclusion

Mastering the CAP theorem is about moving from theory to practice. It’s a framework for asking the right questions and making deliberate design choices early in the interview process.

Key Takeaways:

  • The CAP theorem forces a choice between Consistency (C) and Availability (A) in the presence of Partition Tolerance (P), which is a must in distributed systems.
  • The choice depends on the business impact of stale data. If it's catastrophic (e.g., banking), choose Consistency (CP). If temporary inconsistency is acceptable (e.g., social media), choose Availability (AP).
  • This choice directly influences your architecture and technology stack (e.g., RDBMS vs. NoSQL, synchronous vs. asynchronous replication).
  • For senior roles, show nuance by discussing how different features can have different priorities and by being aware of the spectrum of consistency models beyond just "strong" and "eventual."

In our next lesson, we will explore leader election. This is a critical mechanism that many distributed systems use to coordinate work and is often a key component in building the consistent (CP) systems we discussed today.