EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

Replication in MongoDB

By Priya PedamkarPriya Pedamkar

Home » Data Science » Data Science Tutorials » MongoDB Tutorial » Replication in MongoDB

Replication in MongoDB

Overview of 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.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Data should be replicated periodically and at regular intervals to ensure that we do not lose on any data. Even if the primary server goes down, the second one should serve the user requests.

The replication is also very effective in 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 request 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 write 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.

MangoDB1Done

  • 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.

Popular Course in this category
MongoDB Training Program (4 Courses, 2 Projects)4 Online Courses | 2 Hands-on Projects | 22+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,421 ratings)
Course Price

View Course

Related Courses
Oracle Training (14 Courses, 8+ Projects)All in One Data Science Bundle (360+ Courses, 50+ projects)SQL Training Program (7 Courses, 8+ Projects)Oracle DBA Database Management System Training (2 Courses)MS SQL Training (13 Courses, 11+ Projects)

MangoDB2Done

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.

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:

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:

  1. High availability
  2. 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 –

  1. MongoDB GUI Tools
  2. MongoDB Commands
  3. How To Install MongoDB
  4. MongoDB create Index | Types of Indexes | Options
  5. Learn the List of MongoDB List Collections

MongoDB Training Program (4 Courses, 2 Projects)

4 Online Courses

2 Hands-on Projects

22+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
MongoDB Tutorial
  • Advanced
    • MongoDB Array
    • PostgreSQL ARRAY_AGG()
    • Indexes in MongoDB
    • MongoDB create Index
    • MongoDB Collection
    • MongoDB List Collections
    • MongoDB Capped Collections
    • MongoDB Auto Increment
    • MongoDB Projection
    • Replication in MongoDB
    • Mongo DB Create Database
    • MongoDB Compass
    • MongoDB Users
    • MongoDB Authentication
    • MongoDB GridFS
    • MongoDB Relationships
    • MongoDB Monitoring
    • Backup in MongoDB
    • MongoDB Sharding
    • MongoDB Java Drivers
    • MongoDB Import
    • Mongo Database Interview Questions
  • Basics
    • What is MongoDB
    • How To Install MongoDB
    • MongoDB Tools
    • MongoDB GUI Tools
    • MongoDB Versions
    • MongoDB Commands
    • Advantages of MongoDB
    • MongoDB Features
    • Is MongoDB NoSQL
    • Is MongoDB Open Source
    • Build Web Applications using MongoDB
    • MongoDB vs Elasticsearch
    • MariaDB vs MongoDB
    • Firebase vs MongoDB
  • Commands
    • MongoDB Limit()
    • MongoDB count
    • MongoDB Aggregate
    • MongoDB Distinct
    • MongoDB Unique
    • MongoDB find
    • MongoDB insert
    • MongoDB Delete
    • MongoDB Update
    • Lookup in MongoDB
    • order by in MongoDB
    • MongoDB $regex
    • MongoDB $elemMatch
    • MongoDB ObjectId()
    • MongoDB Skip()
    • MongoDB Date Query
    • MongoDB Timestamp
    • MongoDB sort()
    • MongoDB group by
    • MongoDB Join

Related Courses

MongoDB Certification Course

Oracle Certification Course

All in One Data Science Course

SQL Training Course

Oracle DBA Course

MS SQL Certification Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - MongoDB Training Program (4 Courses, 2 Projects) Learn More