What is CAP Theorem?
CAP Theorem is one of the most important concepts in Distributed Systems and Microservices Architecture.
CAP Theorem states that a distributed system can guarantee only two out of the following three properties at the same time:
- Consistency (C)
- Availability (A)
- Partition Tolerance (P)
This theorem helps architects design scalable and fault-tolerant distributed systems.
Simple Understanding of CAP Theorem
Imagine an online banking system running on multiple servers.
Suppose:
- One server is in India
- Another server is in the US
Now assume network communication between these servers breaks.
At that moment, the system must make a difficult decision:
- Should it prioritize correct data?
- Or should it prioritize continuous availability?
CAP Theorem explains this tradeoff.
What Does CAP Stand For?
| Letter | Meaning | Description |
|---|---|---|
| C | Consistency | All nodes return same latest data |
| A | Availability | Every request receives response |
| P | Partition Tolerance | System works despite network failures |
1. What is Consistency?
Consistency means:
All nodes in the distributed system always return the same latest data.
Example of Consistency
Suppose user updates account balance:
Balance = ₹10,000
After update:
Balance = ₹15,000
All servers should immediately show:
₹15,000
No server should return old data.
Consistency Example Diagram
User Updates Balance
|
v
-------------------------
| Database |
-------------------------
| |
v v
Server 1 Server 2
Both Return:
₹15,000
Advantages of Consistency
- Accurate data
- Reliable transactions
- Important for banking systems
Disadvantages of Consistency
- Higher latency
- Reduced availability during failures
2. What is Availability?
Availability means:
Every request receives a response, even during failures.
Example of Availability
Suppose one server fails.
Other servers still respond to requests.
Server 1 = DOWN Server 2 = ACTIVE
Users continue using the system.
Availability Example Diagram
Client Request
|
v
-------------------------
| Cluster |
-------------------------
| |
X v
Server 1 Server 2
Response Returned
Advantages of Availability
- System remains operational
- Better user experience
- Suitable for large-scale web systems
Disadvantages of Availability
- May return stale or old data
3. What is Partition Tolerance?
Partition Tolerance means:
The system continues working even when network communication between nodes fails.
What is Network Partition?
A network partition occurs when servers cannot communicate because of:
- Network failure
- Server crash
- Datacenter outage
- Cloud connectivity issue
Partition Tolerance Example
Server A X Server B Network Broken
Even though communication fails:
- System should continue operating
Why Partition Tolerance is Mandatory
In distributed systems:
- Network failures are unavoidable
Therefore:
Modern distributed systems must support Partition Tolerance.
Core Idea of CAP Theorem
During network partition:
A distributed system must choose between:
- Consistency
- Availability
Both cannot be guaranteed simultaneously.
CAP Theorem Visualization
Consistency
/\
/ \
/ \
/ \
/ \
/ \
Availability -------- Partition Tolerance
Possible CAP Combinations
- CP System
- AP System
- CA System (Not practical in distributed systems)
1. CP System
CP systems prioritize:
- Consistency
- Partition Tolerance
Availability may reduce during failures.
CP Example
Banking systems often prefer CP.
If network partition occurs:
- System may reject requests temporarily
- But incorrect balance is never shown
CP Flow
Partition Happens
|
v
Reject Some Requests
|
v
Maintain Correct Data
Examples of CP Databases
- MongoDB (configured mode)
- HBase
- Zookeeper
2. AP System
AP systems prioritize:
- Availability
- Partition Tolerance
Temporary inconsistency is accepted.
AP Example
Social media systems often prefer AP.
Suppose:
- One server shows 100 likes
- Another server temporarily shows 98 likes
Eventually all nodes synchronize.
AP Flow
Partition Happens
|
v
Continue Serving Requests
|
v
Synchronize Data Later
Examples of AP Databases
- Cassandra
- DynamoDB
- Riak
3. CA System
CA systems prioritize:
- Consistency
- Availability
But:
- No partition tolerance exists
Why CA is Not Practical
Distributed systems always face possible network failures.
Therefore:
- Partition tolerance cannot be ignored
CAP Theorem in Microservices
Microservices are distributed systems.
Therefore:
- CAP tradeoffs become important
Microservices Example
Order Service
|
v
Payment Service
|
v
Inventory Service
Suppose Payment Service becomes unreachable.
System must decide:
- Stop requests for consistency?
- Or continue serving requests?
CAP Theorem in Banking System
Banking systems usually prefer:
CP (Consistency + Partition Tolerance)
Reason:
- Incorrect account balance is unacceptable
CAP Theorem in Social Media
Social media systems usually prefer:
AP (Availability + Partition Tolerance)
Reason:
- Temporary stale data is acceptable
CAP Theorem in E-Commerce
Different modules may choose different models.
| Module | Preferred Model |
|---|---|
| Payment | CP |
| Product Catalog | AP |
| Recommendations | AP |
Eventual Consistency
Many AP systems use:
Eventual Consistency
This means:
- All nodes eventually become consistent after synchronization
Example of Eventual Consistency
Server 1 = 100 Likes Server 2 = 98 Likes
After synchronization:
Both = 100 Likes
CAP Theorem vs ACID
| Feature | CAP Theorem | ACID |
|---|---|---|
| Focus | Distributed systems | Database transactions |
| Main Concern | Consistency vs Availability | Transaction reliability |
| Usage | Microservices and distributed DBs | Relational databases |
Advantages of CAP Theorem
- Helps design scalable systems
- Improves architectural decisions
- Explains distributed system tradeoffs
- Supports fault-tolerant design
Challenges of CAP Theorem
- No perfect distributed system exists
- Tradeoffs become unavoidable
- Complex architectural decisions required
Real-Time Company Examples
| Company | Preferred CAP Model |
|---|---|
| Banking Systems | CP |
| Amazon Shopping Cart | AP |
| Facebook Likes | AP |
| Payment Gateways | CP |
Best Practices for CAP Design
- Choose tradeoff based on business requirements
- Use eventual consistency where acceptable
- Prioritize consistency for financial systems
- Prioritize availability for social systems
- Design fault-tolerant communication
Interview Ready Answer
CAP Theorem states that a distributed system can guarantee only two out of three properties simultaneously: Consistency, Availability, and Partition Tolerance. Consistency means all nodes return the latest data, Availability means every request receives a response, and Partition Tolerance means the system continues working despite network failures. During network partition, a distributed system must choose between Consistency and Availability. Banking systems usually prefer CP systems, while social media systems commonly prefer AP systems. CAP Theorem is very important in Microservices and Distributed System Architecture because network failures are unavoidable in real-world environments.
Frequently Asked Questions
What does CAP stand for?
Consistency, Availability, and Partition Tolerance.
Can a distributed system support all three properties?
No, during network partition only two properties can be guaranteed.
Why is Partition Tolerance important?
Because network failures are unavoidable in distributed systems.
Which systems prefer CP?
Banking and payment systems prefer CP.
Which systems prefer AP?
Social media and large-scale web systems often prefer AP.