CAP Theorem and System Design
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
CAP Theorem in System Design
The CAP Theorem is a fundamental concept in distributed systems, introduced by Eric Brewer. It states that in any distributed data system, you can only guarantee two out of three properties at a time:
- Consistency (C)
- Availability (A)
- Partition Tolerance (P)
Three Properties of CAP Theorem
- Consistency (C)
Every read receives the most recent write, ensuring all nodes in the system return the same data.- Example: When you update data in one node, every other node reflects the same updated value immediately.
- Availability (A)
Every request receives a response (success/failure), but it may not be the most up-to-date value.- Example: The system remains operational, and requests do not fail, but some data might be stale.
- Partition Tolerance (P)
The system continues to operate even if there is a network partition (loss of communication between nodes).- Example: Nodes can communicate independently when network failures occur.
Why You Can Only Pick Two?
In a distributed system, if a network partition occurs (P), you must choose between:
- Consistency (C): Stop serving requests until the partition resolves to ensure all nodes are synchronized.
- Availability (A): Continue serving requests with potentially outdated (stale) data.
Thus, you cannot guarantee all three properties simultaneously.
CAP Combinations
- CP (Consistency + Partition Tolerance)
- Guarantees consistency even during network partitions, but availability may be sacrificed.
- Example Systems:
- HBase
- MongoDB (configured with strong consistency)
- Google Bigtable
- Use Case: Banking systems, where consistent data is critical.
- AP (Availability + Partition Tolerance)
- Guarantees availability, but consistency may be relaxed (eventual consistency).
- Example Systems:
- Cassandra
- DynamoDB
- Riak
- Use Case: Social media feeds or real-time services where downtime is unacceptable.
- CA (Consistency + Availability)
- Works only when there are no partitions (single-node systems or centralized databases).
- Example: Traditional RDBMS like PostgreSQL and MySQL.
- Use Case: Systems operating on a single machine with no distributed components.
CAP Theorem in Real Systems
Real-world systems often aim for Partition Tolerance (since network failures are inevitable) and make trade-offs between Consistency and Availability.
- Eventual Consistency: Systems like DynamoDB, S3, and Cassandra allow for slightly stale data but eventually converge to a consistent state.
- Strong Consistency: Systems like HBase or Zookeeper ensure reads always return the latest data.
How CAP Impacts System Design?
- Understand System Requirements:
- Does your system require strict consistency (e.g., bank transactions)?
- Is high availability more critical (e.g., e-commerce checkout during failures)?
- Choose the Right Trade-offs:
- CP for critical data systems.
- AP for real-time, highly available systems with eventual consistency.
- Design for Failures:
- Use techniques like replication, sharding, and leader election to maintain availability.
Common Interview Questions Related to CAP Theorem
- Explain CAP Theorem and its implications in system design.
- What is the difference between CP and AP systems? Provide examples.
- How would you design a system for consistency vs. availability?
- Why is Partition Tolerance a must in distributed systems?
Examples in System Design Scenarios
- Design a Distributed Cache:
- Example: Redis in a distributed setup can prioritize AP with eventual consistency.
- Design a Global File Storage System:
- Example: Amazon S3 is AP with eventual consistency.
- Design a Leader Election System:
- Example: Zookeeper prioritizes CP for ensuring leader consistency.
In interviews, when discussing CAP Theorem, be clear about:
- The system trade-offs you are making.
- Real-world examples of databases/systems.
- How your system ensures reliability and fault tolerance.
Understanding the CAP Theorem will give you a solid foundation for solving distributed system design problems! 🚀

