Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Hey Ninjas! You must have heard the term database if you are from a coding background. And everyone knows today's world is running on the internet, so as our databases too. But have you ever thought about what will happen if you lose the connection at some point? Or any server refuses to respond? Prof. Eric A. Brewer created the CAP theorem to tackle all these issues.
This blog will discuss the advanced concepts of the CAP theorem. It is suggested to go through the CAP theorem Part-1 before proceeding to the CAP theorem Part-2.
CAP Theorem
Let's first recall the definition of the CAP theorem. The full form of CAP is Consistency, Availability, and Partition tolerance. The CAP theorem allows you to decide how to handle your distributed Database systems when a few servers refuse to connect due to a system fault.
Types of CAP Theorem
There are three attributes of the CAP theorem.
Consistency: According to CAP, a system is consistent if all nodes have the same data at the same time. The data is replicating synchronouslyacross the systems.
Availability: Availability means responding to a query with the current value on the server. Every client requesting data will receive a response, even if one or more nodes are unavailable.
Partial Tolerance: Partial Tolerance says that regardless of network failure or whether messages are dropped or delayed between nodes in a system, the system will not fail.
CP, AP, and CA
You must be confused about CP, AP, and CA after reading the heading. Don't worry. Let's understand this using a diagram.
You must have gotten an idea of CP, AP, and CA after looking at the diagram above. These are the types of databases on the basis of their features like Availability, Consistency, and Partial Tolerance. We will now check all of them in more detail.
Consistency + Partial Tolerance (CP): Consistency and partition tolerance are the main constraints in this system, although it does not ensure availability and will throw an error if the partitioned state is not resolved.
Examples: MongoDB, Big Table, and HBase.
Availability + Partial Tolerance (AP): AP system cannot ensure consistency due to the chance of updates to either a node or a network. The AP system can have different values on the different nodes.
Examples: Cassandra, CouchDB, and Dynamo.
Consistency + Availability (CA): The primary constraints in this system are consistency and availability, but they do not assure that if one system goes down, the entire system will also go down. Without it, some nodes won't be stable and wouldn't have the most recent data.
Examples: MySQL, Oracle, and SQLServer.
CAP Theorem with Examples
Let us now understand the CAP theorem with some examples:
MongoDB
Let's now understand the diagram:
The single leader-based MongoDB system supports multiple replicas. These replicas asynchronously update themselves from the Leader's OpLog.
Each node keeps track of the heartbeat of every other node to determine if other replicas or the leader are still alive or dead. In other words, every node keeps track if other nodes are connected to the server or not.
By any chance, if the primary node or the leader node goes down or dead, then the replicas can choose and elect a new leader based on priority.
Cassandra
Cassandra writes multiple copies of the same data (usually three) to multiple cluster nodes. This guarantees that data is not lost even if a node fails or becomes unavailable. The replication factor defines the number of copies of data written and entered when a database is built.
Let's discuss some features of Cassandra:
High Scalability
Durability
High Availability
Eventual consistency
Consistent lightweight transactions (movement of data using nodes).
Advantages of CAP Theorem
The advantages of the CAP theorem are
It helps in decision-making and allows the user to create database systems in an efficient manner.
A user can get high consistency at a lower availability cost.
It is easy to use.
Disadvantages of CAP Theorem
The disadvantages of the CAP theorem are
Only Consistency and Availability are the options given by the CAP theorem. It does not offer a solution that balances availability and consistency.
The CAP theorem does not cover problems like node failure, message loss or delay.
Frequently Asked Questions
What are the three properties of the CAP Theorem?
The three properties are Consistency, Availability, and Partial Tolerance.
Give an example of a web application that uses eventual consistency.
Gmail and Facebook use eventual consistency.
What does the modern CAP theorem say?
The goal of modern CAP is to maximise the varieties of consistency and availability that are fit for the individual application. A method like this includes strategies for operating during a partition as well as recovery after that.
Name any real-time web app that focuses on availability more than consistency.
Google Search prioritises availability above consistency.
Which feature is unavailable in the AP (Availability + Partial Tolerance)?
In exchange for losing consistency, an AP database offers availability and partition tolerance.
Conclusion
This article discusses the topic of CAP Theorem - Part 2. In detail, we have seen the definition of the CAP Theorem, its types, examples, and advantages and disadvantages of the CAP Theorem.
We hope this blog has helped you enhance your knowledge of CAP Theorem - Part 2. If you want to learn more, then check out our articles.
But suppose you have just started your learning process and are looking for questions from tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problems, interview experiences, and interview bundles for placement preparations.
However, you may consider our paid courses to give your career an edge over others!