Directory Image
This website uses cookies to improve user experience. By using our website you consent to all cookies in accordance with our Privacy Policy.

What is Replication in MongoDB?

Author: Itguru Online
by Itguru Online
Posted: Dec 29, 2019

MongoDB is an open-source database, which is adaptable. It saves data in JSON files, which can differ in Design.

In MongoDB system, Mongod is the main daemon process which means a time taking background process that provides, solutions to requests for services. A set of mongod processes, that manage similar data sets are called Replica sets.

These are the base points for complete production implementations. In this article, I will explain about MongoDB replica set.

Why we need Replication?

Replication provides repetition and improves data availability. In Replication, many copies of data are made on various database servers. Due to this, it can allow errors that occur due to the loss of one database server.

It also enables us to restore data, even from hardware failure and service disturbances. As we have extra copies of the data, we can maintain one for disaster recovery, backup or reporting.

How does Replication works?

For Replication, it makes use of a replica set. A Replica Set is a set of mongod process that holds the same data set. In a MongoDB replica set, there are many nodes and one arbiter node.

Among all the nodes, only one node is considered as the primary node and other nodes are considered as secondary nodes. All write operations are received, by the primary node.

All other secondary nodes, apply functions from the primary, by which they have the same data set. The primary node stores all modifications, to its data sets in its operation log i.e oplog.

If the primary node is not available, then an available secondary node will vote itself as the new primary node. We can also add another instance, as arbiter to a MongoDB replica set.

Arbiters don’t manage the data set. Arbiters manage majority in a replica set by responding to election and heartbeat requests, by other members of the replica set.

Arbiters are the better way to supply replica set majority functionalities, with low cost than a completely operational replica set member with a data set. If a number of members in a MongoDB replica set is even, then add an arbiter to get majority votes in an election for primary.

Arbiters don’t require committed hardware. Always an arbiter node will be an arbiter whereas, a secondary node may become primary or a primary may become a secondary node while election.

Asynchronous Replication:

Secondary nodes copy the primary nodes oplog and implement the functions, to their data sets serially. As the secondary nodes have the data-sets the same as the primary node’s data set, the MongoDB replica set can work continuously even if one or more members fail.

Get training from OnlineITGuru, to get certification and job support just enroll now at MongoDB training

Automatic Fail over:

If a primary node doesn’t interact with the other members of the set, for more than ‘electionTimeoutMillis’ period which by default 10 seconds. Then an available secondary node suggests as the new primary, on its own. The set tries to finish the election of a new primary node and starts normal functions.

Till the election is completed successfully, the MongoDB replica set will not perform write operations. During the election process, the MongoDB replica set continues to perform read operations, that are on secondaries when the primary is not responding.

Read Preference:

Users can define a ‘read preference’ to transmit read functions to secondary nodes, But by default, clients read from the primary node.

How to start a Replica Set?

Follow the below steps to convert the independent instance to a replica set.

  1. 1. Stop previously running the MongoDB server.

  2. 2. Begin the MongoDB server, by defining –replSet option.

  3. 3. Then it will begin a mongod instance.

  4. 4. Then initiate the command window, and link to this mongod instance.

  5. 5. In the Mongo user, give the command rs.initiate( ) to start a new replica set.

  6. 6. Give the command rs.conf( ), to see the configuration of the MongoDB replica set.

  7. 7. Give the command rs.status( ), to see the status of the replica set.

  8. 8. For adding members to the MongoDB replica set, then initiate mongod instances on different machines. Then start mongo client and give command rs.add( ).

Syntax:

>rs.add(HOST_NAME:PORT)

  1. 9. When we are linked to the primary node, then only we can add mongod instances to the replica set. To verify if we are connected to the primary, type the command db.isMaster( ) in mongo user.

In this article, I have explained the Replication process is done in MongoDB. Follow my articles, to get more updates on MongoDB online training

About the Author

I wrote many contents on many trending technologies

Rate this Article
Leave a Comment
Author Thumbnail
I Agree:
Comment 
Pictures
Author: Itguru Online

Itguru Online

Member since: Dec 25, 2019
Published articles: 1

Related Articles