The inherent trade-offs in networked shared-data system design make it very difficult to create a dependable and effective system. The CAP theorem, or CAP principle, is a central foundation for comprehending these trade-offs in distributed systems. The CAP theorem emphasizes the limitations that system designers have while addressing distributed data replication. It states that only two of the three properties—consistency, availability, and partition tolerance—can be concurrently attained by a distributed system.
Developers must carefully balance these attributes according to their particular application demands because of this underlying restriction. Designers may decide which qualities to prioritize to obtain the best performance and reliability for their systems by knowing the CAP theorem. This article will provide a thorough analysis of all the properties given in the CAP theorem, investigate the associated trade-offs, and talk about how these ideas relate to distributed systems in the real world.
What is the CAP Theorem?
The CAP theorem is a fundamental concept in distributed systems theory that was first proposed by Eric Brewer in 2000 and subsequently shown by Seth Gilbert and Nancy Lynch in 2002. It asserts that all three of the following qualities cannot be concurrently guaranteed in any distributed data system:
1. Consistency
Consistency means that all the nodes (databases) inside a network will have the same copies of a replicated data item visible for various transactions. It guarantees that every node in a distributed cluster returns the same, most recent, and successful write. It refers to every client having the same view of the data. There are various types of consistency models. Consistency in CAP refers to sequential consistency, a very strong form of consistency.
Note that the concept of Consistency in ACID and CAP are slightly different since in CAP, it refers to the consistency of the values in different copies of the same data item in a replicated distributed system. In ACID, it refers to the fact that a transaction will not violate the integrity constraints specified on the database schema.
For example, a user checks his account balance and knows that he has 500 rupees. He spends 200 rupees on some products. Hence the amount of 200 must be deducted changing his account balance to 300 rupees. This change must be committed and communicated with all other databases that hold this user's details. Otherwise, there will be inconsistency, and the other database might show his account balance as 500 rupees which is not true.
Consistency problem2. Availability
Availability means that each read or write request for a data item will either be processed successfully or will receive a message that the operation cannot be completed. Every non-failing node returns a response for all the read and write requests in a reasonable amount of time. The key word here is "every". In simple terms, every node (on either side of a network partition) must be able to respond in a reasonable amount of time.
For example, user A is a content creator having 1000 other users subscribed to his channel. Another user B who is far away from user A tries to subscribe to user A's channel. Since the distance between both users are huge, they are connected to different database node of the social media network. If the distributed system follows the principle of availability, user B must be able to subscribe to user A's channel.
Availability problem3. Partition Tolerance
Partition tolerance means that the system can continue operating even if the network connecting the nodes has a fault that results in two or more partitions, where the nodes in each partition can only communicate among each other. That means, the system continues to function and upholds its consistency guarantees in spite of network partitions. Network partitions are a fact of life. Distributed systems guaranteeing partition tolerance can gracefully recover from partitions once the partition heals.
For example, take the example of the same social media network where two users are trying to find the subscriber count of a particular channel. Due to some technical fault, there occurs a network outage, the second database connected by user B losses its connection with first database. Hence the subscriber count is shown to the user B with the help of replica of data which was previously stored in database 1 backed up prior to network outage. Hence the distributed system is partition tolerant.
Partition ToleranceThe CAP theorem states that distributed databases can have at most two of the three properties: consistency, availability, and partition tolerance. As a result, database systems prioritize only two properties at a time.
Venn diagram of CAP theoremThe Trade-Offs in the CAP Theorem
The CAP theorem implies that a distributed system can only provide two out of three properties:
1. CA (Consistency and Availability)
These types of system always accept the request to view or modify the data sent by the user and they are always responded with data which is consistent among all the database nodes of a big, distributed network.
However, such type of distributed systems is not realizable in real world because when network failure occurs, there are two options: Either send old data which was replicated moments ago before network failure or do not allow user to access the already moments old data. If we choose first option, our system will become Available and if we choose second option our system will become Consistent.
The combination of consistency and availability is not possible in distributed systems and for achieving CA, the system has to be monolithic such that when a user updates the state of the system, all other users accessing it are also notified about the new changes which means that the consistency is maintained. And since it follows monolithic architecture, all users are connected to single system which means it is also available. These types of systems are generally not preferred due to a requirement of distributed computing which can be only done when consistency or availability is sacrificed for partition tolerance.
Example databases: MySQL, PostgreSQL
CAP diagram2. AP (Availability and Partition Tolerance)
These types of system are distributed in nature, ensuring that the request sent by the user to view or modify the data present in the database nodes are not dropped and are processed in presence of a network partition.
The system prioritizes availability over consistency and can respond with possibly stale data which was replicated from other nodes before the partition was created due to some technical failure. Such design choices are generally used while building social media websites such as Facebook, Instagram, Reddit, etc. and online content websites like YouTube, blog, news, etc. where consistency is usually not required, and a bigger problem arises if the service is unavailable causing corporations to lose money since the users may shift to new platform. The system can be distributed across multiple nodes and is designed to operate reliably even in the face of network partitions.
Example databases: Amazon DynamoDB, Google Cloud Spanner.
3. CP (Consistency and Partition Tolerance)
These types of system are distributed in nature, ensuring that the request sent by the user to view or modify the data present in the database nodes are dropped instead of responding with inconsistent data in presence of a network partition.
The system prioritizes consistency over availability and does not allow users to read crucial data from the stored replica which was backed up prior to the occurrence of network partition. Consistency is chosen over availability for critical applications where latest data plays an important role such as stock market application, ticket booking application, banking, etc. where problem will arise due to old data present to users of application.
For example, in a train ticket booking application, there is one seat which can be booked. A replica of the database is created, and it is sent to other nodes of the distributed system. A network outage occurs which causes the user connected to the partitioned node to fetch details from this replica. Some user connected to the unpartitioned part of distributed network and already booked the last remaining seat. However, the user connected to partitioned node will still one seat which makes the available data inconsistent. It would have been better if the user was shown error and make the system unavailable for the user and maintain consistency. Hence consistency is chosen in such scenarios.
Example databases: Apache HBase, MongoDB, Redis.
Conclusion
The CAP theorem provides a framework for understanding the trade-offs in designing distributed systems. It highlights that only two out of three properties—Consistency, Availability, and Partition Tolerance—can be achieved simultaneously. Depending on the application requirements, developers must prioritize the properties that best meet their needs. It's also important to note that many modern databases offer configurations to balance these properties dynamically based on specific use cases.
Similar Reads
Data Storage and Querying in DBMS Database Management System is the collection of interrelated data/information or a set of programs that manages controls, and accesses the use of data. Through DBMS, users can manage the data efficiently in a database to increase accessibility and productivity. For example - Employee records and tel
4 min read
Base Properties in DBMS Pre-requisites: ACID Properties in DBMS The BASE properties of a database management system are a set of principles that guide the design and operation of modern databases. The acronym BASE stands for Basically Available, Soft State, and Eventual Consistency. Basically Available This property refers
2 min read
Integrity Rules in DBMS In DBMS systems, integrity rules, which take a prime place, are designed to ensure that the quality of data is always high, with no inconsistencies or errors. The set of principles, also known as the integrity rules or constraints, helps to manage the data stored in the system in the right way and d
5 min read
Data Models in DBMS A Data Model in Database Management System (DBMS) is the concept of tools that are developed to summarize the description of the database. Data Models provide us with a transparent picture of data which helps us in creating an actual database. It shows us from the design of the data to its proper im
8 min read
What is a Query in DBMS? In the field of Database Management Systems (DBMS), a query serves as a fundamental tool for retrieving, manipulating, and managing data stored within a database. Queries act as the bridge between users and databases, enabling them to communicate with the system to extract specific information or pe
5 min read
Codd's Rules in DBMS Codd's rules are proposed by a computer scientist named Dr. Edgar F. Codd and he also invent the relational model for database management. These rules are made to ensure data integrity, consistency, and usability. This set of rules basically signifies the characteristics and requirements of a relati
3 min read
Various properties of CAP Theorem Prerequisite â The CAP Theorem In the distributed system you must have heard of the term CAP Theorem. CAP theorem states that it is impossible to achieve all of the three properties in your Data-Stores. Here ALL three properties refer to C = Consistency, A = Availability, and P = Partition Tolerance
3 min read
Use of DBMS in System Software Here we are going to discuss about how a user interacts with a DBMS, and how the DBMS is related to system software. Using a general-purpose programming language, user can write a source program in the normal way. However, instead of writing I/O statements of the form provided by the programming lan
5 min read
Purpose of Database System in DBMS Nowadays organizations are data-dependent. efficient management and retrieval of information play a crucial role in their success. A database is a collection of data that is organized, which is also called structured data. It can be accessed or stored in a computer system. It can be managed through
3 min read
Measures of Query Cost in DBMS Query Cost is a cost in which the enhancer considers what amount of time your query will require (comparative with absolute clump time). Then the analyzer attempts to pick the most ideal query plan by taking a glance at your inquiry and insights of your information, attempting a few execution design
4 min read