A replica set in MongoDB is group of mongod instances that maintain the same data set over different instances.Thus, replica set provides high availability and redundancy and best option for production deployment.

What is replica set ?

Replica set is a group of mongod processes with primary and secondary nodes. With multiple copies of data over all primary and secondary nodes it provides a level of fault tolerance against the loss of a single database server.

With multiple copies of data, it provides increase read capacity , increase data locality and availability for distributed applications. You can maintain additional copies for specific purposes disaster recovery, reporting, or backup.

How replica set works ?

Replica set is group of nodes that maintain same data set. There are two types of node : bearing nodes and one optionally arbiter node. Out of Bearing nodes , one is primary node and other are secondary nodes.

All writes operations are sent to primary node only.Primary node records all write operation in oplog file which is used by secondary nodes to replicate data asynchronously.This way same data set is replicated over all bearing nodes which contains data.


If primary node fails then one of eligible secondary node becomes primary node by holding automatic election. When a primary does not communicate with the other members of the set for more than 10 seconds, an eligible secondary will hold an election to elect itself the new primary. The first secondary to hold an election and receive a majority of the members’ votes becomes primary.This way replica set recover automatically from fail over.

An arbiter node also plays important role in election process.It does not maintain data but purpose of this node is to maintain a quorum in a replica set by responding to heartbeat and election requests by other replica set members. Also, If your replica set has an even number of members, add an arbiter to obtain a majority of votes in an election for primary. Arbiters do not require any dedicated hardware.


How to configure replica set ?

Let’s take an example to configure replica set on windows with 3 members replica set and all nodes deployed locally.

You can have members of replica set on different servers.In this, it is very important to setup proper connectivity between all servers so that replica set work properly. But we don’t have to worry about connectivity as our example has all three nodes deployed locally.

lets go ahead and configure the replica in following 3 steps

1. Setup each instance on replica set

Create 3 mongod instances which are members of replica set.This instances will work independently as separate process.

mongod –replSet testReplica  –port 27017 –dbpath {data-dir-path1}\rs1
mongod –replSet testReplica –port 27018 –dbpath {data-dir-path2}\rs2
mongod –replSet testReplica  –port 27019 –dbpath {data-dir-path3}\rs3

–replSet : name of replica set. We have define “testReplica” as name.

–port : specify ports on which each mongod process is working

–dbpath: Specify data directory of replica set. Replace data-dir-path1,data-dir-path2,data-dir-path3 with specific path where you want to store replica set.

Alternatively, you can also specify these configuration on mongod.conf so you don’t neeed to specify it all the time when starting instance.

2. Connect to mongo shell to start replication

Once all servers are up and running, connect one of the server using mongo shell and instantiate the replica set.


This will connect to default mongod instance with port 27017.


This will start the replica set.  You can check the status of replica set on server using rs.status() and rs.conf() display replica set configuration.

3. Add members to replica set

To add members to replica set,  you must be connected to primary node.In our example mongo(27017) server.


This will add 27018 mongod instance to replica set.


This will add 27019 mongod instance as arbiter node to replica set.

4. Run rs.slaveOk() on all secondary nodes to allow client connection to read from secondary nodes.

Done. Replica setup with three nodes is done with 27017 as primary,287018 as secondary and 27019 as arbiter. Go and sleep. Your job done.

But wait..wait.. we need to verify if it works properly and how replica set automatically recover from fail over.

Verify replica set

If you want to check if replica set configured properly then see if

  • replication works properly

You can insert documents by connecting to replica set and check if the same document is inserted in primary as well all secondary nodes. This will verify that proper connectivity between members of replica set is setup and data is replicated.

  • Automatic fail over

You can manually stop primary node(in our case, 27017 server) and connect replica set to get already inserted documents. You must get the documents even after primary node fail.

For example, if you manually stop 27017 server then automatic election process starts and secondary  node(27018) will become new primary node at the end of election process. This way ,when yon connect replica set to get inserted documents, it will internally retrieve documents from new primary(27018).

Also, when you bring back 27017 server into replica set,  then again automatically it will be added into replica set as secondary member. The automatic recovery from fail over is transparent to Client application.

Refer to https://github.com/abhi3290/mongo-replicaset-example which shows how to connect to replica set with read and insert example.

If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on social media. Thank you!