Homebreadcumb forward arrow iconBlogbreadcumb forward arrow iconBig Databreadcumb forward arrow iconMongoDB Replication: Step by Step Replica Set Comprehensive Guide

MongoDB Replication: Step by Step Replica Set Comprehensive Guide

Last updated:
27th Feb, 2020
Read Time
9 Mins
share image icon
In this article
Chevron in toc
View All
MongoDB Replication: Step by Step Replica Set Comprehensive Guide

MongoDB has many functions. And replication is a prominent function among them. There are many advantages to performing replication. With MongoDB, you work with large data sets that include array objects and embedded arrays. As you work with array objects and embedded arrays, you should keep your data processing rate fast. Read about MongoDB real world use cases to know it’s importance.

And for keeping the rate of that process fast, you’ll need to make sure that your data availability remains high. A great way to ensure high data availability is to perform replication. Replication also helps in having a backup and saves you from data loss. 

In this article, you’ll find out what replication in MongoDB is, how it works, and how you can do it. You’ll also find out the advantages of performing replication in MongoDB, and if you encounter any errors while performing this task, how you can fix them. 

Let’s get started. 

Ads of upGrad blog

What is Replication?

While using servers, you’ll need to synchronize your data on multiple MongoDB servers. The process of Replication helps you in this regard. It ensures you have the same data stored on various servers. 

Replication enhances data availability. Data loss is a significant concern for organizations, and replication helps you in mitigating the worries about the same. With high data availability and redundancy, your database remains secure if you lose a single server. You’d lose access to your database if the only server storing it goes down.

Replication ensures you don’t face those issues. MongoDB replication is an effective method of increasing the accessibility of your data as well. Having additional copies of your data is always beneficial. Replication can also reduce the downtime of server maintenance. 

You have the option of dedicating an entire server to reporting, backup, or disaster recovery. 

There are plenty of pros of replication in MongoDB. Let’s find them out. 

Explore our Popular Software Engineering Courses

Benefits of Replication

These are some of the reasons why replication is a widespread practice:

  • Replication helps you in recovering data in case a disaster takes place. You’d have a backup of your data from which you can recover your lost files quickly.
  • Your data remains available all the time. This means replication ensures you have 24×7 accessibility.
  • The downtime of server maintenance reduces if you perform replication regularly. 
  • Replication enhances your reading scalability. Having multiple copies helps in scaling the reading of data. 

Apart from the benefits we’ve discussed here, replication has a drawback too. As you’ll store copies of your data, you’ll need to have more storage space. Although this is not a significant issue, it’s worth noting. You might need to increase your server’s storage capability. 

How MongoDB Replication Works

For performing replication, you’ll need to use MongoDB replica sets. A group of mongod instances that host the same data is called a replica set. A replica set contains one primary node. The primary node receives all the write operations. 

A replica set has only one primary node. The other instances apply functions from the set’s primary node, including the secondary nodes. This way, they all have the same data set. Here is how a MongoDB replica set works:

  • A replica set has a minimum of 3 nodes
  • One node of the replica set is the primary node. All the other nodes present in the group are secondaries.
  • In a replica set, the data replicates from the primary to secondary nodes
  • If an automatic failover happens (or during maintenance), an election takes place for determining the primary. Then, the nodes select a new primary. 

Now that we’ve discussed the basic concept of replication in MongoDB, we can start with its process. 

Step 1: Add the First Member

You know now that to perform replication, you’ll need replica sets. So, the first step of replication in MongoDB is to create a replica set of its instances. Suppose you have three servers, Server X, Server Y, and Server Z. Out of these three, Server X is the primary one, and Server X and Y are the secondaries. 

You already know that replication takes place from the primary server to the secondary ones. First, you’ll have to make sure that all of the mongod instances (that you’ll add to the replica set) are installed on various servers. This way, you’d have multiple servers available even if one of them goes down, and so, you’d have other instances of MongoDB present. 

Then, you should make sure that all the instances can connect. Issue the given commands from Server X:

mongo –host ServerY –port 27017

mongo –host ServerZ –port 27017

After issuing them from Server X, issue them from the other remaining servers. Now start the first instance by using the replSet option. The replSet option gives you a collection of all the servers that would take part in this process. 

mongo –replSet “ReplicaA”

Here, ReplicaA is the name of the replica set. You can choose any name you like, but we’ll use this term for this example. You’d have to issue the command rs.initiate() for initiating the replica set after you’ve added one server to the replica set. After that, you should verify your replica set. 

To do so, issue the command rs.conf(). This step will help you in ensuring that your replica is set up optimally and without any issues. 

Read: MongoDB Interview Questions and Answers

In-Demand Software Development Skills

Step 2: Add a Secondary Server

After adding the primary server, we can now focus on adding a second one. You can use the rs.add command for this purpose. You’d have to enter the name of the secondary servers you wish to add in this command. It’ll add them automatically. 

In our example, we had Server X, Server Y, and Server Z. Out of those three servers, Server X was the primary one in the replica set. We’d need to add the remaining servers as secondaries. And to do that, we’ll issue the following commands:



And that’s it. Now you have successfully added two secondaries to your replica set. 

Step 3: Reconfiguration (or Removal)

Establishing and adding servers is just one side of the coin. You might need to remove a server from the configuration group as well. For that purpose, you’d have to use the rs.remove command. 

Before you remove a server, you’ll need to shut it down. Use the db.shutdownserver command from your mongo shell for shutting down the required server. After that, connect to the primary and use rs.remove for removing the server you need to remove. This command will delete the required server from the replica set. 

So, if you have Server X, Server Y and Server Z in your replica set, out of which, you need to get rid of Server Z, you will use the following command:


How to Fix Replica Set Errors

While performing replication in MongoDB, you might encounter some errors. To troubleshoot those errors, you should take the help of the following methods:

  • First, make sure that all the mongo instances are connected. So, if you have three servers, namely Server X, Server Y, and Server Z. And among those three servers, Server X is the primary one. You’d issue the following commands:

mongo -host ServerY -port 27017

mongo -host ServerZ -port 27017

Running the above two commands will help you ensure that they’re connected. 

Now, run the status command, which is rs.status. The rs.status command gives you the status of your replica set. The members of a replica set send some messages to one another. We call these messages ‘heartbeat’. We call them heartbeat because these messages show that the member is working (i.e., alive). 

The rs.status command checks these messages and tells you if there’s any problem arising with a member of the replica set. 

  • You can examine the oplog. In MongoDB, the oplog stores the history of writes you’ve performed on your database. MongoDB takes the help of Oplog for replicating the writes to all other members of your replica set. 
  • You can check the Oplog by using the rs.printReplicationinfo command after connecting to the required member. The rs.printReplicationinfo command will show you the size of Oplog and its limit of holding transactions until it gets full. 

And that’s it. Now you know how to troubleshoot problems with a MongoDB replica set. With this knowledge, you can start performing replication in MongoDB without facing any hassles. 

Explore Our Software Development Free Courses

Final Thoughts

Replication is just one of the many things you can do in MongoDB. Learning the use of this database program isn’t easy. However, with proper practice and resources, you can quickly become adept at using it.

Ads of upGrad blog

If you wish to learn more about MongoDB and the various functions present in it, go to our blog. You’ll find plenty of useful articles there that can help you in expanding your knowledge on this topic.

If you are interested to know more about Big Data, check out our Advanced Certificate Programme in Big Data from IIIT Bangalore.

Learn Software Development Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs or Masters Programs to fast-track your career.


Utkarsh Singh

Blog Author
Get Free Consultation

Selectcaret down icon
Select Area of interestcaret down icon
Select Work Experiencecaret down icon
By clicking 'Submit' you Agree to  
UpGrad's Terms & Conditions

Our Popular Big Data Course

Frequently Asked Questions (FAQs)

1How is sharding different from replication?

Sharding is a process where data is allocated across multiple machines or servers. MongoDB uses sharding to conduct its heavy deployment related to huge data sets. Moreover, sharding also allows combining various devices to execute data extension at once. It follows with reading and writing operations also. Replication, on the other hand, is data duplication across multiple servers. Sharding solves the issues related to horizontal breaking in datasets. There are many factors to distinguish between the two in terms of latency, network, and throughput.

2What are the ways to deal with replication delay?

Replication brings certain drawbacks, one of which is replication delay or lag. The replication process undergoes an inevitable delay while replicating large chunks of data across servers. There are plenty of factors that increase replication delay. First is network latency which comes into the picture when dealing with MongoDB databases. In MongoDB instances, where continuous replication occurs, the only way to communicate is through the network. However, if the replication needs aren’t fulfilled, there is a chance of network delay. Thus, make sure to always work with suitable bandwidth. The second way is heavy workloads. Replication operations are often delayed when long-running heavy operations take place. Therefore, using MongoDB Write Concern could be an effective solution. The next is database operations, which can sometimes be very slow to operate. However, for a few of them, the time taken could be longer than others. A database profiler helps to optimize accordingly based on the queries we feed.

3Why is there a need for replication?

Replication happens across multiple servers where data is replicated continuously. With replication, we get redundancy, and simultaneously data availability takes a spike. This also leads to generating multiple copies of data on multiple servers. With replication, databases are secure. Data recovery from hardware failure and service interruptions is also possible through replication. Also, if your data is very significant to you, it is all safe and sound with replication. Furthermore, data is available for your applications round the clock.

Explore Free Courses

Suggested Blogs

13 Best Big Data Project Ideas & Topics for Beginners [2024]
Big Data Project Ideas Big Data is an exciting subject. It helps you find patterns and results you wouldn’t have noticed otherwise. This skill
Read More

by upGrad

29 May 2024

Characteristics of Big Data: Types & 5V’s
Introduction The world around is changing rapidly, we live a data-driven age now. Data is everywhere, from your social media comments, posts, and lik
Read More

by Rohit Sharma

04 May 2024

Top 10 Hadoop Commands [With Usages]
In this era, with huge chunks of data, it becomes essential to deal with them. The data springing from organizations with growing customers is way lar
Read More

by Rohit Sharma

12 Apr 2024

50 Must Know Big Data Interview Questions and Answers 2024: For Freshers & Experienced
Introduction The demand for potential candidates is increasing rapidly in the big data technologies field. There are plenty of opportunities in this
Read More

by Mohit Soni

What is Big Data – Characteristics, Types, Benefits & Examples
Lately the term ‘Big Data’ has been under the limelight, but not many people know what is big data. Businesses, governmental institutions, HCPs (Healt
Read More

by Abhinav Rai

18 Feb 2024

Cassandra vs MongoDB: Difference Between Cassandra & MongoDB [2023]
Introduction Cassandra and MongoDB are among the most famous NoSQL databases used by large to small enterprises and can be relied upon for scalabilit
Read More

by Rohit Sharma

31 Jan 2024

Be A Big Data Analyst – Skills, Salary & Job Description
In an era dominated by Big Data, one cannot imagine that the skill set and expertise of traditional Data Analysts are enough to handle the complexitie
Read More

by upGrad

16 Dec 2023

12 Exciting Hadoop Project Ideas & Topics For Beginners [2024]
Hadoop Project Ideas & Topics Today, big data technologies power diverse sectors, from banking and finance, IT and telecommunication, to manufact
Read More

by Rohit Sharma

29 Nov 2023

Top 10 Exciting Data Engineering Projects & Ideas For Beginners [2024]
Data engineering is an exciting and rapidly growing field that focuses on building, maintaining, and improving the systems that collect, store, proces
Read More

by Rohit Sharma

21 Sep 2023

Schedule 1:1 free counsellingTalk to Career Expert
footer sticky close icon