CAP Explained
The CAP theorem states that a distributed system can only guarantee two of three properties simultaneously.
The Three Properties
Consistency
/\
/ \
/ \
/ CP \
/ AP \
/____CA____\
Availability Partition
Tolerance
CA: Consistency + Availability (no partition tolerance)
CP: Consistency + Partition Tolerance
AP: Availability + Partition Tolerance
Definitions
Consistency:
Every read receives the most recent write or an error. All nodes see the same data at the same time.
Availability:
Every request receives a non-error response, without guaranteeing it contains the most recent write.
Partition Tolerance:
The system continues to operate despite network partitions (communication breaks between nodes).
Why You Can't Have All Three
Scenario: Network Partition
Node A ────X──── Node B
(partition)
Client writes X = 1 to Node A
Client reads X from Node B
Option 1 (Consistency):
- Return error (can't guarantee latest value)
- Sacrifices Availability
Option 2 (Availability):
- Return old value (X = 0)
- Sacrifices Consistency
You must choose: Consistency OR Availability
The Reality: Partitions Happen
Network partitions are inevitable:
- Hardware failures
- Network congestion
- Rack failures
- Data center outages
Since partitions WILL happen, the real choice is:
- CP: Sacrifice availability during partitions
- AP: Sacrifice consistency during partitions
CA systems only work when there are no partitions.
Real-World Implications
Understanding CAP helps you choose the right technology for your use case.
CP Systems (Consistency + Partition Tolerance)
During partition:
- Reject writes to minority side
- Accept writes to majority side
- Ensure consistent data
Examples:
- ZooKeeper (leader election)
- etcd (distributed key-value)
- HBase (column-family store)
- MongoDB (with majority write concern)
- PostgreSQL (with synchronous replication)
AP Systems (Availability + Partition Tolerance)
During partition:
- Accept writes on both sides
- Allow inconsistent data temporarily
- Resolve conflicts later
Examples:
- Cassandra (eventual consistency)
- DynamoDB (eventual consistency)
- CouchDB (multi-master)
- Riak (eventual consistency)
- DNS (eventual propagation)
CA Systems (Consistency + Availability)
No partition tolerance:
- Only works in single-node or no-partition scenarios
- Not truly distributed
Examples:
- Single-node PostgreSQL
- Single-node MySQL
- Traditional RDBMS
Note: Real distributed systems must handle partitions,
so true CA systems are rare in distributed computing.
Real-World Examples
| System | CAP Choice | Reason |
|---|---|---|
| Banking | CP | Must prevent double-spending |
| DNS | AP | Always resolve, may be stale |
| ZooKeeper | CP | Coordination requires consistency |
| Cassandra | AP | High availability for writes |
| Redis Cluster | CP | Data consistency important |
| S3 | AP | Always available for uploads |
Choosing Systems
Use CAP to guide your technology choices and design decisions.
Decision Framework
Step 1: Can you tolerate stale data?
├── Yes → Consider AP
└── No → Consider CP
Step 2: Is availability critical?
├── Yes → Consider AP
└── No → Consider CP
Step 3: What happens during failure?
├── Reject operations → CP
└── Accept operations → AP
CAP in System Design Interviews
When asked to design a system:
1. Identify components
2. For each component, determine:
- Can it tolerate stale data?
- Is availability critical?
- What happens during failure?
3. Choose CP or AP for each component
Example: URL Shortener
- URL creation: AP (always accept, resolve later)
- URL redirect: AP (serve from cache, may be stale)
- Analytics: AP (eventual consistency OK)
Example: Inventory System
- Stock check: CP (must prevent overselling)
- Order placement: CP (must be consistent)
- Product browsing: AP (can show stale data)
Beyond CAP: Practical Considerations
CAP is a starting point, not the full picture:
1. Consistency Levels
- Strong, Eventual, Read-your-writes
2. Performance
- Latency and throughput requirements
3. Operational Complexity
- How hard to manage and monitor
4. Cost
- Infrastructure and operational costs
5. Data Model
- Relational vs document vs key-value
Common Mistakes
- Assuming CA is possible in distributed systems: Partitions happen
- Ignoring the tradeoffs: Every choice has consequences
- Over-engineering: Not every system needs strong consistency
- Under-engineering: Some systems absolutely need consistency
Summary
CAP Theorem:
- Distributed systems can only guarantee 2 of 3
- Partitions are inevitable, so choose CP or AP
- CP: Sacrifice availability during partitions
- AP: Sacrifice consistency during partitions
- Choose based on business requirements
Practice Problems
Design a scalable CAP Theorem system. Cover high-level architecture, data model, and API design.
Solution
// Complete system design:
// - Functional + Non-functional requirements
// - Capacity estimation
// - Data model (SQL/NoSQL choice)
// - API endpoints
// - Component architecture
// - Scaling strategy
// - Monitoring & reliabilityHow would you scale CAP Theorem to handle 10x the current load? Identify bottlenecks and solutions.
Solution
// Scaling approach:
// 1. Load balancing
// 2. Database sharding/replication
// 3. Cache layer (Redis)
// 4. CDN for static assets
// 5. Async processing (queues)
// 6. Microservices decompositionAnalyze potential failure modes for CAP Theorem and design mitigation strategies.
Solution
// Failure mitigation:
// 1. Redundancy (multi-AZ)
// 2. Circuit breakers
// 3. Retry with backoff
// 4. Dead letter queues
// 5. Health checks
// 6. Graceful degradationQuiz
1. What does the CAP theorem state?
2. Why must distributed systems always be partition tolerant?
3. Which system is an example of a CP (Consistency + Partition Tolerance) system?
4. What happens in an AP system during a network partition?
Flashcards
Question
What is the CAP theorem?
Click to reveal answer
Answer
A distributed system can only guarantee two of three properties: Consistency (all nodes see same data), Availability (every request gets response), Partition Tolerance (works despite network failures).
Question
What is a CP system?
Click to reveal answer
Answer
Consistency + Partition Tolerance. Rejects operations rather than serve inconsistent data. Examples: ZooKeeper, etcd, HBase. Used when correctness is critical.
Question
What is an AP system?
Click to reveal answer
Answer
Availability + Partition Tolerance. Accepts operations even during partitions, may serve stale data. Examples: Cassandra, DynamoDB, DNS. Used when availability is critical.
Question
Why are network partitions inevitable?
Click to reveal answer
Answer
Hardware failures, network congestion, rack failures, and data center outages make partitions unavoidable in distributed systems. This is why CAP is relevant.
Question
What is CAP Theorem?
Click to reveal answer
Answer
CAP Theorem is a key concept in system design.
Revision Notes
Key Takeaways
- 1.CAP theorem limits distributed systems to 2 of 3 guarantees
- 2.Network partitions are inevitable, so CA systems aren't truly distributed
- 3.CP systems sacrifice availability during partitions for consistency
- 4.AP systems sacrifice consistency during partitions for availability
- 5.Choose based on business requirements: banking=CP, social=AP
Interview Tips
- •Identify CP vs AP for each component in your design
- •Explain why you chose CP or AP based on requirements
- •Discuss what happens during failures for your chosen approach
- •Mention that CAP is a starting point - consider other factors too
Cheat Sheet
CAP Theorem - Cheat Sheet
Three Properties:
- Consistency: All nodes see same data
- Availability: Every request gets response
- Partition Tolerance: Works despite network failures
The Theorem:
Can only guarantee 2 of 3 simultaneously.
Since partitions are inevitable, choose:
- CP: Sacrifice availability during partitions
- AP: Sacrifice consistency during partitions
Examples:
| Type | Systems |
|---|---|
| CP | ZooKeeper, etcd, HBase, MongoDB |
| AP | Cassandra, DynamoDB, DNS, Riak |
| CA | Single-node databases (not distributed) |
Decision Framework:
- Can you tolerate stale data? → AP
- Is availability critical? → AP
- Must prevent inconsistencies? → CP