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.
MongoDB - Replication
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
For replication we require mongod
MongoDB - Replication
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.
MongoDB - Replication
MongoDB - Replication
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
MongoDB - Replication
MongoDB - Replication
> rs.status()
MongoDB - Replication
> 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.
MongoDB - Replication
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"})
MongoDB - Replication
MongoDB - Replication
MongoDB - Replication
Now we have added all the nodes.
  1. sampleReplicaSet:PRIMARY> rs.status()
  2. {
  3. "set" : "sampleReplicaSet",
  4. "date" : ISODate("2020-01-18T18:34:52.420Z"),
  5. "myState" : 1,
  6. "term" : NumberLong(1),
  7. "members" : [
  8. {
  9. "_id" : 0,
  10. "name" : "localhost:27017",
  11. "ip" : "127.0.0.1",
  12. "health" : 1,
  13. "state" : 1,
  14. "stateStr" : "PRIMARY",
  15. },
  16. {
  17. "_id" : 1,
  18. "name" : "localhost:27018",
  19. "ip" : "127.0.0.1",
  20. "health" : 1,
  21. "state" : 2,
  22. "stateStr" : "SECONDARY",
  23. },
  24. {
  25. "_id" : 2,
  26. "name" : "localhost:27019",
  27. "ip" : "127.0.0.1",
  28. "health" : 1,
  29. "state" : 2,
  30. "stateStr" : "SECONDARY",
  31. },
  32. {
  33. "_id" : 3,
  34. "name" : "localhost:27020",
  35. "ip" : "127.0.0.1",
  36. "health" : 1,
  37. "state" : 2,
  38. "stateStr" : "SECONDARY",
  39. }
  40. ],
  41. "ok" : 1,
  42. "$clusterTime" : {
  43. "clusterTime" : Timestamp(1579372490, 1),
  44. "signature" : {
  45. "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
  46. "keyId" : NumberLong(0)
  47. }
  48. },
  49. "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.
MongoDB - Replication
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
MongoDB - Replication
MongoDB - Replication
MongoDB - Replication
MongoDB - Replication
MongoDB - Replication
MongoDB - Replication
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.
MongoDB - Replication

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':'[email protected]'})
MongoDB - Replication
Open any secondary mongo console and execute the below.
commandsampleReplicaSet:SECONDARY> db.replicaSetSampleCollection.find({})
MongoDB - Replication
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({})
MongoDB - Replication

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

In this article we implemented replica set locally and also understand how to insert and retrieve the records. To get more understanding then please visit here as well.