MongoDB and the CAP theorem

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 :-

  1. Primary Node Write that data to the DB
  2. Records the operations in operation Log (opLog)
  3. Secondary Node always reads the operation Log for the correct data, and pull it

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.

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.

Article content


To view or add a comment, sign in

More articles by Manish kumar

  • Cache Invalidation Strategies

    Cache invalidation strategies define how and when cached data should be updated or removed when the original data…

  • Cache Invalidation and why it is Hard

    Cache invalidation is the process of removing stale or outdated data from the cache to ensure consistency and make room…

  • Cache Eviction Strategies

    Caching is a technique used to make responses faster by reducing CPU load and avoiding repeated computations. Cache…

  • Cache Types

    Here we are going to explore some common types of caching used in modern systems: 1. Client-Side Cache Client side…

  • Caching

    Caching means storing the frequently used data in the Cache (fast storage / primary storage) , so that whenever in…

    3 Comments
  • Sticky Sessions

    In my previous article, I covered various load balancing algorithms and briefly touched on Sticky Sessions. In this…

  • Load balancing Algorithm

    In my previous article, I covered two algorithms: Round Robin and Weighted Round Robin. In this article, I will cover…

    3 Comments
  • Load Balancing Algorithms

    If the Load Balancer used to distributes the traffic between different multiple servers, but now the questions is, How…

  • Load Balancer

    A load balancer is used to distribute a large number of requests across different servers based on various algorithms…

    2 Comments
  • Rate Limiting

    In my previous article, I mentioned what Rate Limiting is, but this article is fully dedicated to Rate Limiting. Rate…

Others also viewed

Explore content categories