When designing distributed systems, developers often face an unavoidable tradeoff: consistency, availability, or partition tolerance—choose two.
This fundamental concept, known as the CAP theorem, shapes how modern databases and distributed architectures function.
Let's break it down in a simple and practical way.
What is the CAP Theorem?
The CAP theorem, formulated by Eric Brewer in 2000 and later proven formally, states that in a distributed system, you can achieve only two out of the following three guarantees:
- Consistency (C) – Every read receives the most recent write or an error. This means that data is the same across the cluster, so you can read or write from/to any node and get the same data.
- Availability (A) – Every request receives a response, even if it's not the most recent data. This means the system remains accessible even if one or more nodes fail.
- Partition Tolerance (P) – The system continues to function even if there is a network partition (communication break) between nodes. The cluster should still respond even if some nodes can't communicate.
The Impossible Trinity: Why Can’t We Have It All?
A distributed system inherently needs partition tolerance because networks can fail, and nodes can lose connectivity. That leaves us with two choices:
- CP (Consistency + Partition Tolerance): The system sacrifices availability. If a partition occurs, nodes may reject reads/writes to maintain a consistent state.
- AP (Availability + Partition Tolerance): The system sacrifices strict consistency, meaning it might serve stale or divergent data during a partition.
- CA (Consistency + Availability): Only possible in a system without partitions—essentially, a single-node database.
Understanding the Tradeoff: Why Partition Tolerance is Non-Negotiable
Since network partitions will happen, we must always consider partition tolerance (P).
Given that, the real decision is between Consistency (CP) vs. Availability (AP) during a partition:
- If you choose AP, nodes remain online even if they can't communicate with each other. They will resync data once the partition is resolved, but data might be inconsistent across nodes.
- If you choose CP, data remains consistent across all nodes, but some nodes may become unavailable during a partition.
Why CA Systems Are Not Practically Possible
A CA system theoretically guarantees consistency and availability as long as all nodes are online.
However, if a partition occurs, it will lead to data inconsistency or downtime.
Since network failures are inevitable, CA systems do not exist in a practical distributed environment.
Beyond CAP: The PACELC Theorem
CAP tells us about tradeoffs during a network partition, but what happens when there’s no partition? Enter PACELC, which states:
- If there is a Partition, a system must choose between Availability and Consistency.
- Else (under normal conditions), it must choose between Latency and Consistency.
This extends CAP to capture performance considerations, explaining why databases like DynamoDB prefer lower latency over strong consistency.
Choosing the Right Database for Your Use Case
When picking a distributed database, ask yourself:
- Do I need strong consistency (CP) or can I tolerate some inconsistency (AP)?
- How critical is availability for my application?
- What happens if parts of my system temporarily go offline?
Diving Deeper: AP vs CP in Practice
In an AP system, you might experience inconsistent reads and write conflicts.
Some AP databases resolve write conflicts automatically, while others require application-level conflict resolution.
In a CP system, network partitions can lead to temporary downtime or degraded performance.
Some CP databases have mechanisms to reduce downtime, but increasing replication can sometimes make partitions more disruptive.
Final Thoughts
The CAP theorem isn’t just an academic concept, it’s a practical reality for anyone building distributed systems.
Understanding its implications helps make better architectural choices, balancing consistency, availability, and partition tolerance based on the needs of your application.
I’ve been working on a super-convenient tool called LiveAPI.
LiveAPI helps you get all your backend APIs documented in a few minutes
With LiveAPI, you can quickly generate interactive API documentation that allows users to execute APIs directly from the browser.
If you’re tired of manually creating docs for your APIs, this tool might just make your life easier.
Top comments (3)
Good article about technical constraints in distributed database management systems. I am an enthusiast myself.
Got A Practical Understanding CAP
Thanks for the share
Interesting article, thanks for sharing