Replica sets in MongoDB
In this blog.. let us try to understand …
What are Replica sets?
How to create multiple replica sets in local?
A replica set in MongoDB is a group of mongod processes that maintain the same data set. Replica sets provide redundancy and high availability and are the basis for all production deployments.
If you don’t know what is Mongo daemon- refer to this link before going further…
Problem Statement:
Consider there is one MongoD on port 27018 and it points to the /home/db1.
Now for some reason let us assume MongoD is down…and now the backend code cannot access the DB… so it sends an error to the frontend and the frontend shows an error in the UI….This creates a bad user experience.
How to avoid this?
How to increase the availability of the DB?
Solution:
Use replica sets with PRIMARY and SECONDARY nodes.
How the solution solves the problem:
Create multiple daemons on different ports numbers. Say consider 3 daemons….
daemon on port 27018 , dbpath- /home/db1
daemon on port 27019, dbpath- /home/db2
daemon on port 27020 , dbpath- /home/db3
But all these daemons should contain the same data…
Why??
Let us say one record has been added to the 1st daemon on port 27018..that record has to be present in the 2nd and 3rd daemon…
so that if 1st daemon is down….then the code can fetch updated data from 2nd or 3rd daemon…
If the daemon on port 27018 is down. Consider it fetches from the daemon on port 27019 . as shown below...
So to achieve this, all daemons have to communicate with each other.
Now how to make daemons communicate with each other???..
In order to achieve this we have to create daemons of type replica set…. and make PRIMARY and SECONDARY nodes…
In replica sets….there can be only one primary node.
Any request from the code always requests for PRIMARY node…and if we are adding a new record..it will be added to the PRIMARY node and to all the SECONDARY nodes.
Say if PRIMARY node is down…then any one of the SECONDARY node becomes PRIMARY…this way..replica sets helps in high availability with latest data.
Setting up replica sets in local:
Step 1:
Now lets create 3 daemons of type replica sets with name as rs0 as shown below:
ps: make sure the replica set name(rs0) should be same for all the daemons.
daemon on port 27018 , dbpath- /home/db1 — rs0
daemon on port 27019, dbpath- /home/db2 — rs0
daemon on port 27020 , dbpath- /home/db3 — rs0
mkdir /home/db1 /home/db2 /home/db3
chmod 777 /home/db1 /home/db2 /home/db3
(new terminal)mongod --dbpath /home/db1 --port 27018 --replSet "rs0"
(new terminal)mongod --dbpath /home/db2 --port 27019 --replSet "rs0"
(new terminal)mongod --dbpath /home/db3 --port 27020 --replSet "rs0"
Step 2:
Open a new terminal and type
mongo --port 27018
This command connects to the daemon on port 27018. Now no nodes are made PRIMARY or SECONDARY. ….Lets make
daemon on port 27018 — PRIMARY
daemon on port 27019— SECONDARY
daemon on port 27020 — SECONDARY
Step 3:
In order to make daemon on port 27018 PRIMARY…
rs.initiate()
In the same terminal, type the above command ….it will initiate replica set and makes the daemon on port 27018 as PRIMARY as shown below….
Here you see you get rs0.SECONDARY> on Enter u get rs0.PRIMARY> ..please ignore this…
Final Step:
So now, for the daemon on port 27018…is set to PRIMARY…the other daemons on port 27019 and 27020 should be made SECONDARY.
In order to achieve this..use this command
rs.add("localhost:27019")
rs.add("localhost:27020")
Now we are in this stage..as shown below…
In a new terminal type
mongo --port 27019
It shows rs0:SECONDARY>
In a new terminal type
mongo --port 27020
It shows rs0:SECONDARY>
In the primary node terminal…type
rs.config()
you will get the details of all the members.
So, now if the daemon on port 27018 is down..either daemon on port 27019 or port 27020 will become PRIMARY node ...through the process of voting.
A replica set can have up to 50 members, but only 7 voting members, non-voting members allow a replica set to have more than seven members.