CAP Theorem Summary
The CAP theorem is a fundamental principle in distributed computing that states it’s impossible for a distributed data store to simultaneously provide more than two of the following three guarantees:
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. The system remains operational even when nodes fail.
Partition Tolerance: The system continues to operate despite network partitions (communication breakdowns between nodes).
In practical terms, since network partitions are unavoidable in distributed systems, designers must choose between:
-
CP systems (consistent and partition-tolerant): These may become unavailable during partitions but maintain consistency. Examples include traditional relational databases with distributed transactions.
-
AP systems (available and partition-tolerant): These remain available during partitions but may return stale data. Examples include NoSQL databases like Cassandra or DynamoDB.
Many modern distributed systems employ nuanced approaches that make specific trade-offs based on business requirements, implementing different consistency models (strong, eventual, causal) depending on the use case.
What is CAP Theorem?
- You can only have 2/3
- Consistency - all nodes/users see the same data at the same time
- Availability - every request gets a response (successful or not)
- Partition tolerance - system works despite network failures between nodes
Why does this matter?
- Important to define the system characteristics early during non-functional requirements
- They influence your design decisions during deep dives
Network fails between them
- Stop serving data (consistency)
- Or risk wrong data (availability)
Examples where we need strong consistency:
- Ticket booking platform (airline, event, etc)
- If we sell a ticket, everyone needs to see it as unavailable, without delay
- Inventory System (Amazon)
- Can’t sell the same last item to multiple customers
- Financial Systems
- Stock trades must be executed in strict order
If you don’t need strong consistency, then choose availability!
- Social media app
- Yelp like business review service
- Streaming service like Netflix
How does it influence my decision?
- Strong consistency
- Implement distributed transactions
- limit to single node
- discuss consensus protocols
- accept higher latency
- Example tools:
- PostgresSQL
- Trad RDMS
- Spanner
- NoSQL with strong consistency mode (DynamoDB)
For availability
- use multiple replicas
- CDC and eventual consistency is ok
- Example tools:
- DynamoDB (in mult-AZ mode)
- Cassandra
Advanced! Different parts of system can have different requirements:
Ticketmaster
- availability for CRUD events
- consistency for booking tickets
Tinder
- availability for viewing profile data
- consistency for matching
Different types of consistency:
- Strong consistency: all reads reflect most recent writes
- Casual consistency: related events appear in order
- Read-your-writes consistency: user sees their own updates
- Eventual consistency: updates will propagate eventually