DEV Community

Cover image for Understanding the CAP Theorem
gulnur
gulnur

Posted on Edited on

Understanding the CAP Theorem

As developers, we often assume that distributed systems can provide everything at once: perfectly consistent data, high availability, and resilience to network failures. Unfortunately, distributed systems don't work like that.

This is where the CAP Theorem comes in, one of the most fundamental concepts in distributed computing.

What Is the CAP Theorem?

Proposed by computer scientist Eric Brewer, the CAP Theorem states that a distributed system can guarantee only two of the following three properties simultaneously:

  • Consistency (C)
  • Availability (A)
  • Partition Tolerance (P)

When a network partition occurs, you must choose between consistency and availability.

Consistency

In the CAP Theorem, consistency refers to linearizability[2] This means that every read receives the most recent successful write, or an error. The system behaves as if all operations happened in a single, globally consistent order.

For example, if you update a user's account balance, any subsequent read should return the updated value, regardless of which node handles the request.

From the user's perspective:

  • Read-after-write returns the latest value
  • No stale data is returned
  • All operations follow a consistent order

Availability

Availability means every request receives a response, even if some servers are experiencing issues.

The response might not contain the most recent data, but the system remains operational.

Examples:

  • Social media feeds: If User X updates their profile info, it's just fine if User Y sees the old info for a few minutes.
  • Product recommendations
  • Content Platforms
  • Analytics dashboards
  • Review Sites (Yelp etc.)

These systems often prioritize availability over strict consistency.
We can basically ask ourselves "Would it be catastrophic if users briefly saw inconsistent data?" If the answer is yes, choose consistency. If not, choose availability[1]

Partition Tolerance - Not Optional

A partition happens when nodes cannot communicate because of network failures."Partition Tolerance” is the the ability of a distributed system to continue operating even when a partition occurs.

In modern cloud environments, partitions are not hypothetical events.

They happen because of:

  • Network outages
  • Region failures
  • Router issues
  • Infrastructure maintenance

Since partitions are unavoidable, most distributed systems must support partition tolerance. Because you cannot prevent hardware failures or router crashes.The systems should not be affected by network partitions.
Therefore, any distributed system must be partition-tolerant.

A Real-World Example: The ATM

Imagine you have $1,000 in your bank account and two ATMs connected to different bank servers.

You withdraw $200 from the first ATM, but a network failure prevents the second server from receiving the update 🫣 The second ATM still shows your balance as $1,000.

Now, you try to withdraw another $900 from the second ATM.

  • Consistency (CP): The ATM rejects the transaction because it cannot verify your current balance. The system sacrifices availability to protect data consistency.
  • Availability (AP): The ATM continues processing transactions using its outdated balance, potentially allowing you to withdraw more money than you actually have.

Real banking systems use additional safeguards to prevent such situations.

This example illustrates the fundamental CAP trade-off: when a network partition occurs, a system may need to reject or delay requests to maintain consistency, or continue responding despite the risk of serving outdated data.

The Real Trade-off

The CAP Theorem becomes important when a partition occurs.

At that moment you must decide:

CP Systems

Consistency + Partition Tolerance

The system rejects or delays requests to ensure data remains correct.

Examples:

  • Banking transactions
  • Inventory management
  • Financial systems

Correctness matters more than availability.

AP Systems

Availability + Partition Tolerance

The system continues serving requests even if some nodes have outdated information.

Examples:

  • Social networks
  • Messaging systems
  • Content platforms

Availability matters more than immediate consistency.

What About CA Systems?

Technically, CA systems exist only when partitions never happen.

In real distributed environments, partitions are inevitable.

This is why most cloud-native systems end up choosing either CP or AP behavior during failures.

CAP in Modern Databases

Different databases make different trade-offs:

Database Typical Preference
MongoDB CP
Apache Cassandra AP
DynamoDB AP-oriented
PostgreSQL (single node) CA-like
CockroachDB CP

Understanding these trade-offs helps architects choose the right database for their workload.

Why Developers Still Need CAP

Many developers learn CAP Theorem during interviews or reviewing the interview questions and then forget about it😀

But CAP influences real-world decisions:

  • Designing microservices
  • Choosing databases
  • Building global applications
  • Handling outages
  • Architecting cloud-native systems

Whenever someone says, "Why can't we just keep everything available and perfectly consistent?", they are usually running into the limitations described by CAP.

I think CAP Theorem is not like a database rule. It is a reminder that distributed systems require trade-offs.

As systems scale across regions, clouds, and continents, understanding those trade-offs becomes more important than memorizing any specific technology.

Beyond CP and AP: Real-World Trade-offs

The real challenge is not simply choosing between consistency and availability. It's understanding which operations require strong consistency and which can tolerate temporarily stale data.

Resources & Further Reading
[1]- Cap Theorem

[2]- Linearizability (Consistency in CAP)

Understanding CAP Theorem: Basics and Real-World Examples

Image credit: My original design - Cover Image

Top comments (7)

Collapse
 
merbayerp profile image
Mustafa ERBAY •

Nice explanation, Gulnur! 👏 CAP is one of those concepts that looks simple on paper but becomes much more interesting when you’re designing real production systems.

I’d add two important distinctions.

First, CAP consistency specifically refers to linearizability, not simply every node having identical data at every moment. That’s an important difference when discussing distributed databases.

Second, labeling entire databases as CP or AP can be misleading. MongoDB, Cassandra, and even distributed PostgreSQL deployments can exhibit different behaviors depending on their replication configuration, consistency settings, and failure-handling policies.

There’s also an interesting operational question: what happens when the network partition heals?

Choosing availability during a partition is only half the story. Conflict resolution, reconciliation, and preventing lost updates can become much harder than keeping the system online.

In my experience, the most useful architectural question isn’t simply “Should this system be CP or AP?”

It’s “Which operations must remain correct during a partition, and which can safely tolerate stale data?”

A financial transaction and an analytics dashboard might belong to the same application but require completely different consistency guarantees.

Great topic! Distributed systems become much easier to reason about once we stop treating CAP as a database classification chart and start treating it as a failure-mode design problem.

Collapse
 
glnurltn profile image
gulnur •

Thanks for sharing this 😊 You brought up some really good points, especially about how CAP works in real-world systems rather than just looking at databases as CP or AP.

I hadn't thought much about what happens after a network partition heals, and your point about conflict resolution is really interesting.
Definitely gave me a few more things to think about. Appreciate your input 🙌

Collapse
 
merbayerp profile image
Mustafa ERBAY •

You’re very welcome, Gulnur! 😊

And I noticed you’ve already incorporated some of those distinctions into the article. That’s great to see!

One thing I’ve learned from working with distributed systems is that the most interesting problems often begin after the network connection comes back.

Restoring connectivity is relatively straightforward. Figuring out which writes to preserve, how to reconcile conflicting states, and whether the system violated any business invariants along the way is where architecture gets interesting.

That’s also why I enjoy discussions like this. A good technical article doesn’t have to answer every question. Sometimes its biggest value is starting conversations that make all of us reconsider our assumptions.

Keep writing and exploring these topics. Looking forward to your next one! 🙌

Thread Thread
 
glnurltn profile image
gulnur • • Edited

I really enjoy these kinds of discussions too. It's amazing how much you can learn from different perspectives and experiences.
Thanks again for taking the time to share your knowledge and for the encouragement. I really appreciate it 😊

Collapse
 
arslan_ah profile image
Arslan Ahmad •

Good explanation, especially calling out that the C/A tradeoff only kicks in during a partition. That's the part most people miss. In normal operation you can have both. One addition I always suggest: look up PACELC next. It extends CAP to the no-partition case, where the real tradeoff is latency vs consistency. That's the one you live with day to day. I've taught CAP for years in my system design course at DesignGurus, and PACELC is the follow-up that separates surface knowledge from real understanding in interviews.

Collapse
 
glnurltn profile image
gulnur •

Thank you for this thoughtful insight. I hadn't explored PACELC before, and your explanation makes me realize there's more to the story beyond CAP. The latency-consistency tradeoff during normal operation is especially interesting. I'll definitely dig into PACELC. Appreciate you sharing your experience ☺️

Collapse
 
robertpaschal profile image
Odinaka Robert Nnamani •

partitions make the choice for you in practice. the question is which pain you accept.