Replica Sets In MongoDB

Replica Sets In MongoDB

Replica sets is the process of ensuring that the same data exists and shared between multiple mongodb instances. The main goal of replicat sets is to provide redundancy and high availability. Replica sets ensure that if for any reason the mongodb primary instance fail for some reason, the data will still be accessible from an other instance which will be selected using what we call election.

So, in general with replica sets you will have a fault-tolerance system (which is the basis for all mongodb production environments due to the multiple copies of the same data on different servers), Improving the read operations and also, can help you keep and maintain additional copies for dedicated purposes, such as disaster recovery, reporting, or backup.

Replica Set in MongoDB

How replication works

In normal condiguration, the client can only talk to the primary key. So what happen is whenever a client send an write operation to the primary node (MongoDB can only have one primary server that is capable of confirming writes with {w: "majority"} write concern), the server will record those changes in its operation logs (ie: oplog), and the secondary nodes will check this oplog to look for changes and save them as well (this is an asynchronous operation).

Start MongoDB Replica set

The first step will be to start the MongoDB instance with the option "--replset" which will group all the replica sets. The group name in our example will be "ReplicaSet1" and after that we should run other instances that will join our group later.

mongod –-replSet ReplicaSet1 --dbpath "C:\Program Files\MongoDB\Server\4.2\data"
mongod –-replSet ReplicaSet1 --dbpath "C:\Program Files\MongoDB\Server\4.2\data2" --port 27018
mongod –-replSet ReplicaSet1 --dbpath "C:\Program Files\MongoDB\Server\4.2\data3" --port 27019

The previous comman will start a replica set named "ReplicaSet1", so to start using this replica set we should first enter to our mongo server and initiate a new replica set. To do so we will use rs.initiate() but first let's check the status of our replica set before doding anything.

Status of replica set

rs.status()
{
        "operationTime" : Timestamp(0, 0),
        "ok" : 0,
        "errmsg" : "no replset config has been received",
        "code" : 94,
        "codeName" : "NotYetInitialized",
        "$clusterTime" : {
                "clusterTime" : Timestamp(0, 0),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}

Initate the replica set

mongo
> rs.initiate()

Run rs.initiate() on just one and only one mongod instance for the replica set.

Show the configuration

mongo
> rs.conf()

Add replica set

rs.add(HOST:PORT)

Example and Result

rs.add(HOST:PORT)
{
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1582226751, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1582226751, 1)
}

You can always check your configuration and members using rs.status() from any replica set and you can always check if the current node you are connected to is a primary node or not using db.isMaster().