moMongoDB

Replication is a really good admin feature. This is required for managing availability. In simple words, in production environments large data manipulation is going on simultaneously so at that time the application database should be capable enough to handle large queries. Replication is automatically integrated when we are using MongoDB Atlas. But in this article, we will practically implement it out local environment, so let’s start.

To implement replication, we require multiple nodes or clusters.

The above diagram gives you some basic idea about replication as we are creating multiple databases and connecting together. Amongst these databases one is a primary database and the remaining ones are secondary databases. Now we will try to explore more about these nodes and how we are going to setup this locally.

Note

I hope you are aware of all the basics of MongoDB before starting this article, as this is not a very basic concept, but rather an intermediate one. If you are very new to MongoDB then please take some time to get a better understanding of setup and installation. After getting better at being hands on in MongoDB then you can continue. Those with experience can ignore these 2-3 lines.

Setup - Replica Set

Create one folder mongoDB-replication on your local machine.



Create 4 sub folders inside this folder









First close or shutdown any MongoDB server that's already running.

Open one more folder C:\Program Files\MongoDB\Server\4.2\bin



In this folder we are able to see many tools or utilities related to mongodb.



For replication we require mongod

Open the same folder in command prompt.

mongod --port <port_number> --dbpath <database data path> --replSet <name_repltca set>

mongod --port 27017 --dbpath C:\mongoDB-replication\db1 --replSet sampleReplicaSet

And hit enter.

This means our command runs properly and MongoDB server is running on port 27017.

Now we need to repeat this same step 3 more times.

mongod --port 27018 --dbpath C:\mongoDB-replication\db2 --replSet sampleReplicaSet

mongod --port 27019 --dbpath C:\mongoDB-replication\db3 --replSet sampleReplicaSet

mongod --port 27020 --dbpath C:\mongoDB-replication\db4 --replSet sampleReplicaSet

I hope you have understood the above commands. So a total of 4 command prompts are running on your machine with 4 different ports with 4 different dbpaths.

Add new members or nodes to Replica Set

To add members to replica set, start mongo instances on multiple machines. Now start a mongo client and issue a command rs.add() method

Open one more console and run mongo --port 27017

> rs.status()

> rs.initiate()

rs.initiate() is required to get and initiate the replica set.

“ok”:1

Gives us a signal that initialization is done properly. Now we can add more nodes.

rs.add(host_name:port_number)

> rs.add("localhost:27018")

rs.add({ host: "localhost:27018"})

rs.add({ host: "localhost:27019"})

rs.add({ host: "localhost:27020"})

Now we have added all the nodes.

sampleReplicaSet:PRIMARY> rs.status() { "set" : "sampleReplicaSet" , "date" : ISODate( "2020-01-18T18:34:52.420Z" ), "myState" : 1, "term" : NumberLong(1), "members" : [ { "_id" : 0, "name" : "localhost:27017" , "ip" : "127.0.0.1" , "health" : 1, "state" : 1, "stateStr" : "PRIMARY" , }, { "_id" : 1, "name" : "localhost:27018" , "ip" : "127.0.0.1" , "health" : 1, "state" : 2, "stateStr" : "SECONDARY" , }, { "_id" : 2, "name" : "localhost:27019" , "ip" : "127.0.0.1" , "health" : 1, "state" : 2, "stateStr" : "SECONDARY" , }, { "_id" : 3, "name" : "localhost:27020" , "ip" : "127.0.0.1" , "health" : 1, "state" : 2, "stateStr" : "SECONDARY" , } ], "ok" : 1, "$clusterTime" : { "clusterTime" : Timestamp(1579372490, 1), "signature" : { "hash" : BinData(0, "AAAAAAAAAAAAAAAAAAAAAAAAAAA=" ), "keyId" : NumberLong(0) } }, "operationTime" : Timestamp(1579372490, 1)

If you are going through status you should understand that in member node it is giving us how many nodes are active which is primary. Port details are also available.

Here we have done the replica set setup.

Console information states relicaSet name and the role.

Now we need multiple consoles to understand more replica sets.

C:\Program Files\MongoDB\Server\4.2\bin

We are having a complete system.

primary cluster port – 27017

secondary cluster port – 27018, 27019, 27020



We do some CRUD operations to get more ideas regarding replica set.



The above image gives us the understanding that when a new document gets inserted or updated then it updates through primary node only. The data retrieval is from secondary nodes only. Primary node gets changed and this gets handled by mongodb only. Primary node gets elected by all nodes and for that purpose we do the required minimum 3 nodes and maximum 7 nodes. As with primary and secondary, we do have arbitrary nodes that are used only for election purpose only.

db.replicaSetSampleCollection.insert({_id:1,'name':'test','email':'test@123.com'})

Open any secondary mongo console and execute the below.

commandsampleReplicaSet:SECONDARY> db.replicaSetSampleCollection.find({})

You will definitely get the above error. To solve this, we need to execute the below.

commandsampleReplicaSet:SECONDARY> rs.slaveOk(true)

Now execute the below command again. sampleReplicaSet:SECONDARY> db.replicaSetSampleCollection.find({})

Advantage of Replica Set

Reliability and availability means you get increased· query performance faster.

The disadvantage of Replica Set

Storage space is more required as data gets duplicated between multiple nodes.· Multiple writes are required in multiple nodes to manage reliability and availability.

Summary