Suppose we are building an e-commerce system that needs to remain operational even when network issues occur between data centres. However, it is critical that customer data like cart items and order history remain consistent across regions.
Even if you don't work directly with databases, understanding CAP can provide valuable insight into system design trade-offs.
CAP stands for:
Consistency
Availability
Partition tolerance
These are three critical properties of distributed databases.
What is the CAP Theorem?
The CAP theorem states that in any distributed database system, you can only guarantee two of these three properties. Sacrificing one property is inevitable when designing a distributed system.
Consistency
Consistency means that every read from the database returns the latest write. All nodes in the distributed system return the same data for any given query, even in the event of failures.
Strong consistency is important for use cases like financial transactions, where consistent reads and writes are critical.
Availability
Availability means every request is guaranteed to receive a response, even if one or more nodes in the system go down. The system stays operational even during network partitions, hardware failures or other outages.
Availability is important for high-performance systems where uptime is critical.
Partition Tolerance
A partition refers to a communications break between nodes in a distributed system.
Partition tolerance means the system continues to function and uphold its consistency guarantees despite any number of network failures or partitions.
Most distributed systems are designed with partition tolerance, as network issues are a fact of life.
The Trade-Off
According to the CAP theorem, among these three properties, a distributed system can only guarantee two at any given time.
Often there is a trade-off between consistency and availability during a partition, known as the CA guarantee.
Some common database types and their CAP guarantees:
CP (Consistency and Partition Tolerance): MongoDB, Redis
AP (Availability and Partition Tolerance): Cassandra, Riak
CA (Consistency and Availability): Traditional RDBMS like MySQL
When designing a distributed system, engineers must make the tough choice between consistency and availability based on the application's requirements.
There are various strategies to minimize the CAP trade-off, but ultimately the theorem proves it is impossible to guarantee all three properties.
If you enjoyed this article, please consider subscribing to our newsletter 📬 and joining our WhatsApp Channel 👥, where we share similar content. ✏️
Top comments (2)
well written CAP theorem enjoyed reading it.
Thanks 😊 . Don’t forget to Join our WhatsApp Channel.