The CAP theorem, also known as Brewer's theorem, is one of the most fundamental concepts in distributed systems. It states that any distributed system can provide at most two of the following three guarantees: Consistency, Availability, and Partition tolerance.
The Three Guarantees
Consistency (C)
Consistency means that all nodes in the distributed system see the same data at the same time. When a write operation completes, all subsequent read operations must return the updated value, regardless of which node serves the request.
In practical terms, this means that if you update a user's profile on one server, that change should be immediately visible when accessing the profile from any other server in the system.
Availability (A)
Availability guarantees that the system remains operational and responsive to requests. Every request receives a response, without guarantee that it contains the most recent data. The system continues to function even when some nodes fail.
Partition Tolerance (P)
Partition tolerance means the system continues to operate despite network failures that split the system into multiple isolated groups of nodes. The system can handle communication breakdowns between nodes and still function correctly.
The Trade-offs
Since network partitions are unavoidable in distributed systems, the practical choice is usually between consistency and availability:
CP Systems: Consistency + Partition Tolerance
- MongoDB (with strong consistency)
- HBase
- Redis Cluster
AP Systems: Availability + Partition Tolerance
- Cassandra
- DynamoDB
- CouchDB
Real-World Applications
Understanding CAP theorem helps architects make informed decisions:
- Financial systems: Often choose CP for data accuracy
- Social media: Typically choose AP for user experience
- E-commerce: May use different strategies for different data types
Beyond CAP: PACELC Theorem
The PACELC theorem extends CAP by addressing system behavior during normal operation. It states that in case of network partitioning (P), one has to choose between availability (A) and consistency (C), but else (E), even when the system is running normally, one has to choose between latency (L) and consistency (C).
Conclusion
The CAP theorem provides a valuable framework for understanding the fundamental trade-offs in distributed systems. While it presents constraints, understanding these limitations helps architects make informed decisions about system design and choose appropriate technologies for their specific use cases.