Introduction to Replication in MongoDB

Mongodb has another feature called replication. In this article we will be going through the introduction and basics of replication.

What is replication in mongodb?

In simple terms replication is a process to synchronize data in multiple servers. This process provides us with redundancy and increases data availability with multiple copies of data on different database servers. Replication also allow to recover from hardware failure and service interruptions

Why should we use replication in mongodb?

  • It protects a database from the loss of a single server.
  • Replication also allows recovering from hardware failure and service interruptions.
  • Disaster Recovery.
  • No downtime for maintenance (like backups, index rebuilds, compaction).
  • Read scaling (extra copies to read from).
  • Replica set is transparent to the application.
  • replication increases read capacity.

How to use replication in mongodb?

In mongodb replication is used by the help of replica sets. So, to understand how replication works, we have to know about replication set.

What is replica set?

As per a replica set in mongodb is a group of mongod processes that maintain the same data set. Replica sets provide redundancy and high availability, and are the basis for all production deployments.

Or we could say

  • A replica set is a group of mongod instances that host the same data set. In a replica one node is primary node that receives all write operations.
  • Replica set is a group of two or more nodes (generally minimum 3 nodes are required).
  • In a replica set one node is primary node and remaining nodes are secondary.
  • All data replicates from primary to secondary node.
  • At the time of automatic failover or maintenance, election establishes for primary and a new primary node is elected.
  • After the recovery of failed node, it again join the replica set and works as a secondary node.

How does replica set works in mongodb?

Replication sets in their basic form are somewhat similar to nodes in a master-slave configuration. A single primary member is used as the base for applying changes to secondary members.
Primary member:
The primary member is the default access point for transactions with the replication set. It is the only member that can accept write operations. Each replication set can have only one primary member at a time. This is because replication happens by copying the primary’s “oplog” (operations log) and repeating the changes on the secondary’s dataset. Multiple primaries accepting write operations would lead to data conflicts.
Secondary members:
A replication set can contain multiple secondary members. Secondary members reproduce changes from the oplog on their own data. Although by default applications will query the primary member for both read and write operations. A secondary member can become the primary if the primary goes offline or steps down.
An arbiter is an optional member of a replication set that does not take part in the actual replication process. It is added to the replication set to participate in only a single, limited function, to act as a tie-breaker in elections. If a replication set has only one secondary member, an arbiter is required.

Asynchronous Replication:
By applying operations after the primary, sets can continue to function without some members.

Automatic Failover:
When a primary does not communicate with the other members of the set for more than 10 seconds, the replica set will attempt to select another member to become the new primary. The first secondary that receives a majority of the votes becomes primary.

How to set up a replica set?

Now start the mongodb server by specifying –replSet option. Basic syntax of –replSet is given below:

mongod --port "PORT" --dbpath "YOUR_DB_DATA_PATH" --replSet "REPLICA_SET_INSTANCE_NAME"

As for an example we follow the below code snippet.

mongod --port 27017 --dbpath "D:\set up\mongodb\data" --replSet rs0

It will start a mongod instance with the name rs0, on port 27017. Now start the command prompt and connect to this mongod instance. In mongo client issue the command rs.initiate() to initiate a new replica set. To check the replica set configuration issue the command rs.conf(). To check the status of replica sete issue the command rs.status().

Add members to replica set:
To add members to replica set, start mongod instances on multiple machines. Now start a mongo client and issue a command rs.add().
Basic syntax of rs.add() command is as follows:


In the above article we have only discussed about replication and how to initiate it. In the later articles we will try to look further into it.

Related Links:

1> How to Get Started with MongoDB Database?
2> How to Get Started with MongoDB?
3> How to Import and Export Through Mongodb?
4> How to Use Projection in MongoDB?
5> Using sort method in mongodb
6> Map-Reduce in MongoDB
7> Deploying a Replica Set in MongoDB
8> Discussing Replication Lag in MongoDB
9> Replica Set Members in Mongodb
10> Working with Sharding in MongoDB
11> Working with Index in MongoDB
12> Working with Aggregation in MongoDB
13> How to Work with Aggregation Framework in MongoDB?
14> Working with Pipeline Concept in MongoDB
15> Discussing about Pipeline Expression in MongoDB

If you find this article helpful, you can connect us in Google+ and Twitter.

1 thought on “Introduction to Replication in MongoDB

Leave a Reply

Your email address will not be published. Required fields are marked *