EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials MongoDB Tutorial Replication in MongoDB
Secondary Sidebar
MongoDB Tutorial
  • Advanced
    • MongoDB Array
    • PostgreSQL ARRAY_AGG()
    • Indexes in MongoDB
    • MongoDB create Index
    • MongoDB Collection
    • MongoDB List Collections
    • MongoDB Capped Collections
    • MongoDB Delete Collection
    • Mongodb show collections
    • MongoDB Auto Increment
    • MongoDB Triggers
    • MongoDB Projection
    • Replication in MongoDB
    • MongoDB Database
    • Mongo DB Create Database
    • MongoDB Compass
    • MongoDB Users
    • MongoDB Authentication
    • MongoDB GridFS
    • MongoDB Relationships
    • MongoDB MapReduce
    • MongoDB Geospatial
    • MongoDB Monitoring
    • Backup in MongoDB
    • MongoDB Sharding
    • MongoDB Java Drivers
    • MongoDB Import
    • Mongo Database Interview Questions
    • MongoDB Join Two Collections
    • MongoDB Group by Multiple Fields
    • MongoDB Pagination
    • MongoDB Replica Set
    • MongoDB Bulk Update
    • MongoDB greater than
    • MongoDB Encryption
    • MongoDB find in array
    • MongoDB like query
    • Mongodb shell
    • MongoDB port
    • MongoDB Query Operators
    • MongoDB Web Interface
    • MongoDB Query Array
    • MongoDB Transactions
    • MongoDB Not In
    • MongoDB not null
    • MongoDB npm
    • MongoDB Remove
  • 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 Data Types
    • MongoDB Administration
    • Data Modeling in MongoDB
    • MongoDB vs Elasticsearch
    • MariaDB vs MongoDB
    • Firebase vs MongoDB
  • Commands
    • Mongodb updateMany
    • MongoDB Aggregation
    • Mongodb unwind
    • Mongodb where
    • MongoDB BSON
    • MongoDB Filter
    • Mongodb Match
    • MongoDB sort by date
    • MongoDB Limit()
    • MongoDB Atlas Login
    • MongoDB Relational Database
    • MongoDB count
    • MongoDB Aggregate
    • MongoDB Distinct
    • MongoDB Unique
    • MongoDB find
    • MongoDB findOne()
    • MongoDB insert
    • MongoDB Delete
    • MongoDB Update
    • Lookup in MongoDB
    • order by in MongoDB
    • MongoDB $regex
    • MongoDB $elemMatch
    • MongoDB ObjectId()
    • MongoDB Skip()
    • MongoDB findAndModify
    • Mongodb findOneAndUpdate
    • MongoDB Date Query
    • MongoDB Timestamp
    • MongoDB sort()
    • MongoDB group by
    • MongoDB Join

Replication in MongoDB

By Priya PedamkarPriya Pedamkar

Replication in MongoDB

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.

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

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.

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.

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 –

  1. MongoDB GUI Tools
  2. MongoDB Commands
  3. How To Install MongoDB
  4. MongoDB List Collections
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
4.5
Price

View Course

Related Courses

Oracle Training (17 Courses, 8+ Projects)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
SQL Training Program (10 Courses, 8+ Projects)4.7
Oracle DBA Database Management System Training (2 Courses)4.7
MS SQL Training (16 Courses, 11+ Projects)4.7
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • 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

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

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

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

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

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

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

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

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more