What is the MongoDB Replication?

Replication is the process to maintain multiple copies of the same data on different servers. Replication provides high redundancy and high availability of data by replication one set of data on multiple nodes. In case a server is getting down due to hardware failure or some other reason, the data is still available on another node and the application can be online.

MongoDB achieves the replication using the replica sets. The replica sets are the group of mongod processes that maintain the same data sets. In a replica, there is one primary node that receives all write operations and all other nodes are secondary nodes. The secondary nodes read the primary oplog and apply those changes operation on their datasets so that data in between primary node and secondary are sync and reflect the same.

MongoDB Replication Architecture and Flow

Let us understand the MongoDB replication flow using the below steps.

  • All the application write request goes to the Primary Node.
  • Primary Node records all changes in the operation logs also called oplog.
  • Now the Asynchronous Replication takes place.
  • Secondary Node read the Primary Node oplog and apply all those changes in their datasets.
  • In case of a Primary Node failure, the eligible Secondary Node gets elected for the Primary Node.
  • Once the recovery process is completed it joins the replica set and works as a Secondary Node.

The below diagram represents the MongoDB replication architecture in which an application client will interact with the Primary Node and then Primary Node replicate the data to the Secondary Node.

Mongodb replication

MongoDB Replica Set Members

The Replica set is the group of mongod processes. There are three types of Replica set members in MongoDB, the Primary Replica Set, the Secondary Replica Set, and the Arbiter Replica Set.

  • Primary Replica Set
  • Secondary Replica Set
  • Arbiter Replica Set

Let us see each MongoDB replica set in detail in the below section.

1. Primary Replica Set

The Primary Replica set receives the write operation request from the client application, after that MongoDB applies those changes on Primary after that it records those changes in Primary oplog.

The following figure represents the three-member replica set, in which the Primary will accept the write and read request from the client and secondaries will replicate the oplog to their data sets.

Mongodb Primary Replica Set

1.1 Primary Replica Set Failure

In case of Primary Replica Set failure, an election take place to select the new Primary Replicate Set.

The election of the new Primary Replica Set triggers based on the following events.

  • If we are adding a new node to the MongoDB Replica Set.
  • If we are initializing a Replica Set through rs.initiate() command.
  • If Replica Set maintenance is going on using rs.stepDown() or rs.reconfig() commands.
  • If there is a connectivity issue between the Secondery Replica Set and the Primary Replica Set for more than 10 seconds(Default Value) of timeout.

In the below three-member Replica set figure, If the Primary Replica Set goes down then the election will choose one of the remaining Secondary Replica Set as a Primary Replica Set.

Mongodb Primary Replica Set failure

2. Secondary Replica Set

The Secondary Replica Set uses the Primary Replica Set oplog files and applies those changes in their data sets asynchronously. In a Replica Set, there could be more than one Secondary Replica Set.

In the below three-member replica set figure, there is two Secondary Replica Set that is reading the Primary Replica Set oplogs and apply changes at their end.

Mongodb secondary Replica Set

3. Arbiter Replica Set

The Arbiter Replica Set is used in those conditions where we have a Primary Replica Set and the Secondary Replica Set but due to costing we are not adding Secondary Replica Set, here we can use the Arbiter Replica Set which does not store a copy of the dataset and it can't become the Primary Replica Set, but it can participate in the elections to choose the Primary Replica Set, in case of failure. It has 1 election vote.

In the following 2 member replica set, the Arbiter Replica Set provides its odd number of votes.

Mongodb secondary Replica Set

MongoDB Replica Set Lifecycle

The MongoDB Replica Set Lifecycle represents the different stages of the process starting from Replica Set creation, Replica Set initialization, Replica Set failure, Replica Set failover, Replica Set recovery, and Replica Set recover.

The below figure represent the Replica Set Lifecycle flow.

Mongodb replication