Deploying a Replica Set in MongoDB

In this article we are going to discuss about how to deploy a replica set in mongo db.

Deploying a replica set:

Replica set can be deployed from single instances as well as from multiple instances. To get there we have to figure out how replica set function. As stated in replica set is a cluster of mongod instances that replicate amongst one another and ensure automated failover.
Although these replica sets may be consist of more than one instance. One of them is has to be the primary instance, which will be in direct contact with the client. Mongo dB’s replica sets provide automated failover. If a primary fails, the remaining members will automatically try to elect a new primary. A replica set can have up to 12 members, but only 7 members can have votes.

Replica set member properties.

Secondary-Only: These members have data but cannot become primary under any circumstance. See Secondary-Only Members.
Hidden: These members are invisible to client applications. See Hidden Members.
Delayed: These members apply operations from the primary’s oplog after a specified delay. You can think of a delayed member as a form of “rolling backup.” See Delayed Members.
Arbiters: These members have no data and exist solely to participate in elections. See Arbiters.
Non-Voting: These members do not vote in elections. Non-voting members are only used for larger sets with more than 7 members. See Non-Voting Members.

Member selection in failover:

When any failover occurs, an election takes place to decide which member should become primary. Elections provide a mechanism for the members of a replica set to automatically select a new primary without administrator’s permission. The election allows replica sets to recover from failover situations very quickly and robustly.
Whenever the primary becomes unreachable, the secondary members trigger an election. The first member to receive votes from a majority of the set will become primary. The most important feature of replica set elections is that a majority of the original number of members in the replica set must be present for election to succeed.
There is an absolute rule in election for maximum connectivity. In simple terms whenever a primary member becomes unreachable 2/3 of the secondary member has to be connected to each other for voting. If this condition does not match, then there should be no election held and therefore no primary members should be selected.

Replication set member priority:

In a replica set, every member has a “priority,” that helps determine eligibility for election to primary. By default, all members have a priority of 1, if the priority value has not been modified. All members have a single vote in elections.
Now let us talk about how to deploy replica set from a single instance. Which in mongo dB terms are called standalone mongod instance.
Deploying a replica set (standalone mongod instance):

  • At first we need to shut down the instance for further progress.
  • Restart the instance using

mongod --port port_number --dbpath --replSet replSet_name
  • Connect to the mongod instance.
  • Use rs.initiate() to initiate the new replica set


After this we have to add some instances. To do that let us look at the following steps.

  • On two distinct systems, start two new standalone mongod instances.
  • On the preferable connection to the original mongod instance, issue a command for each new instance to add to the replica set.


Now as for deploying replica set of multiple instances, at the first we have to check the connectivity with every instance for no further disturbance. To make it easier we can use a private virtual network. Let us see how it is done.

Ensuring connectivity:

    • Establish a virtual private network. Ensure that the network traffic between members within a single site over the local area network.
    • Configure access control to prevent connections from unknown clients to the replica set.
    • Configure networking and firewall rules so that incoming and outgoing packets are permitted only on the default Mongo dB port.
    • Finally ensure that each member of a replica set is accessible by way of resolvable DNS or hostnames.

After making sure of the network activity let us begin the procedure.

At first we need to select a name for the replica set. We can achieve this by using the codes in the below.

mongod --replSet " replSet _name"

after this we have to initialize it I mongo shell using initialize().


Verify the initial replica set configuration.


Add the remaining members to the replica set. When complete, the replica set will be fully functional. The new replica set will elect a primary.


Check the status of the replica set.


In the above article we have discussed about the deployment in replica set. Although we have scratched the surface we still need to learn many pros and cons about replica set. As we further progress in this path. We will try to understand things in our way.

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> Introduction to Replication 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.

Leave a Reply

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