Overview of Replication in MongoDB
The following article provides an outline for Replication in MongoDB. MongoDB is an open-source Document-oriented database that is highly scalable; it is a NoSQL database. It can be used for high-volume data storage. MongoDB stores the data in JSON-like format, which is very flexible, and the document model then maps to the objects in application codes.
What is MongoDB Replication?
By replication, we mean the clustering of servers that performs the same functionality. In the case of MongoDB, we have MongoDB servers for storing the data. The purpose of replication is to ensure that we have high data availability. If any of the servers go down, we should always have a copy of the data available in another server so that availability is not affected.
Data should be replicated periodically and at regular intervals to ensure that we do not lose any data. Even if the primary server goes down, the second one should serve the user requests.
The replication is also very effective in the case of load balancing; for example, if we have several users are trying to read or write data to servers, in that case, it would be unfair if all the requests are sent or retrieved from just one server, there should be a balancing mechanism that should equally distribute the load to the available servers in the cluster so that no server runs out of memory or bandwidth and goes down.
Working of Replication in MongoDB Process
The replication in MongoDB is achieved with the help of Replica sets. Replica sets are the grouping of MongoDB servers. There is a primary server, and it is responsible for receiving all the requests or writing operations from clients. The other instances that will be added to the replica set will be called the secondary instances responsible for all the read operations.
- As illustrated in the above diagram, all the data replicates the primary node to secondary nodes, and there will be only one primary node in any replica set.
- Whenever a failover happens or some maintenance activity is performed, a new primary node is elected.
- Once the failover is recovered, the failed node now acts as a secondary node.
- The client application always communicates with the primary node, which is then followed by the replication of data to all the secondary nodes.
Replica Sets, Creation, and Operations
To create a full-fledged replication of MongoDB servers, we must create a replica set of MongoDB instances.
Let us imagine that we have three servers, repl1, repl2 and repl3, where repl1 is the primary server, and the remaining are secondary ones.
Adding of Primary Node Replication in MongoDB
- Please make sure that mongo.exe instances are installed on all the given servers.
- All the mongod.exe must be able to ping each other, i.e. they can communicate. To check the same, please run the following commands from the primary servers (at first), i.e. repl1 in our case.
mongo –host repl2 –port 27017
mongo –host repl3 –port 27017
Similarly, we can do the same test from other servers as well
- By using the command, mongo –replset, we should start our first mongod.exe instance.
Mongo –replset “Replica_A”, Where Replica_A is the name of our replica set. - As for now, we have added the first server to our replica set, and the next step is to initiate the replica set by issuing the command rs.initiate().
- The next step is the verification step, where we will ensure that whatever we have configured so far is correct, and we can do that by running the command rs.conf().
Adding of Secondary Node Replication in MongoDB
Once the primary server is added, it is easy to add the other secondary nodes, and we can do that by issuing the command rs.add().
Hence, run the below command, given that repl1 is our primary server and repl2 and repl3 are secondary servers.
Code:
rs.add(“repl2”)
rs.add(“repl3”)
Removing of Servers from Replica Set
To remove a server from any replica set, it can be done using the command rs.remove().
Certain steps are involved in the process as given below:
- At first, shut down the instance that is required to be removed. This can be done by running the command db.shutdown server via mongo cell.
- The next step is to connect to the primary server.
- Run the following command, let’s say we have repl1, repl2 and repl3 as primary and secondary servers, respectively and we want to remove repl3, then we will run the following command.
Code:
rs.remove(“repl3”)
Some Commands for Troubleshooting
- All the mongod.exe must be able to ping each other, i.e. they can communicate. To check the same, please run the following commands from the primary servers (at first), i.e. repl1 in our case.
mongo –host repl2 –port 27017
mongo –host repl3 –port 27017
Similarly, we can do the same test from other servers as well
- The rs.status() will give the status of your replica set
- For checking oplog, which is a log for recording all the write operations that were made, issue this command – rs.printReplicationInfo.
Advantages of Replication in MongoDB
The purpose of replication is to ensure that we have high data availability. If any of the servers go down, we should always have a copy of the data available in another server so that availability is not affected. The replication is also very effective in load balancing, for example, if we have several users trying to read or write data to servers.
So long story short, the main advantages of replication serves the below purpose:
- High availability
- Load Balancing
Conclusion
As we have seen, MongoDB replication is a process where we replicate the data in more than one server to ensure high availability. This can be done by creating a replica set and adding the primary and secondary servers.
Recommended Articles
This has been a guide to Replication in MongoDB. Here we discuss the creation, operation, working, advantages and some commands for troubleshooting. You can also go through our other suggested articles to learn more –
  4 Online Courses |  2 Hands-on Projects |  22+ Hours |  Verifiable Certificate of Completion
4.5
View Course
Related Courses