MongoDB and the CAP theorem
MongoDB is a NoSQL database management system that stores data in BSON (Binary JSON) format. It is frequently used for big data applications and real-time systems that run across multiple locations. According to the CAP theorem, MongoDB is considered a CP data store—it handles network partitions by maintaining consistency, while temporarily compromising availability.
MongoDB follows a single-master architecture, which means that in each replica set there is one primary node that receives all the write operations. All the other nodes in the replica set are secondary nodes that replicate the primary node's data and can also serve read operations.
While reading an article about the CAP theorem, I came across a section titled “MongoDB and the CAP Theorem.” As I continued reading, I learned that MongoDB follows Consistency (C) and Partition Tolerance (P) from the CAP theorem.
But how ?
MongoDB is Single Master system, it simply means, in each replica set there are multiple nodes (Servers), Out of that there is only one Primary Node and others are secondary Node. MongoDB uses only Primary Node to receives all the write operations and secondary node for the read operations, Now, whenever any write happens in the primary node, these three things happens :-
Suppose a user performs an update operation. The update is first written to the primary node, and the operation is recorded in the oplog. The secondary nodes then replicate this change by pulling the operation from the oplog and applying it to their own data sets. In this way, the data eventually becomes consistent across all nodes in the replica set.
Recommended by LinkedIn
Now, assume that after the update has been replicated to all nodes, the primary node becomes unavailable due to a network failure. During this time, the replica set starts an election process to choose a new primary from the available secondary nodes. While this election is in progress, the cluster cannot accept write operations, because only the primary node is allowed to handle writes.
As a result, the system temporarily becomes unavailable for write requests, even though the data remains consistent across the nodes. Therefore, in terms of the CAP theorem, MongoDB prioritizes Consistency (C) and Partition Tolerance (P), while Availability (A) is temporarily sacrificed during the primary election process.
Many people might wonder what happens if a user tries to update some data and, while the database operation is in progress, the primary node becomes unavailable. In such a situation, one might think that this could cause a consistency issue.
MongoDB avoids consistency issues by ensuring that a write operation either completes successfully and is replicated to other nodes, or if the primary fails before replication is complete, the operation may be rolled back during the election of a new primary. This mechanism prevents inconsistent states across the replica set.