MongoDB Replication

MongoDB Replication

Replication generally refers to making a copy of the same data. In MongoDB, it ensures the availability of the same data in multiple servers. Thus, if for any reason the main database server of MongoDB goes down, you can easily access the replicated data from any other server having a copy of the same. It increases the data availability at a regular interval by ensuring the protection of the database too.

Advantages of Replication

  1. Ensures the safety of data- Even if the primary server fails, the data is not lost as the replicated copies are stored in other servers.
  2. Increase in data availability-Since multiple copies are stored in multiple servers, user can access the data anytime.
  3. Ease in recovering from any disaster-As additional copies exist in multiple servers, it is very easy to recover from a disaster like a collapse, fire, etc.
  4. No need of extra time- Maintenance doesn't need extra time.
  5. Read operation occurs from any of the replicated copy without waiting- It increases the read scaling. The load is well balanced as all the users are connected to all servers instead of connecting to a single system.
  6. Transparency of replica set with the application.

MongoDB-Replica Set

Replication is done by creating replica sets. Replica sets are the sets that contain multiple MongoDB servers, or mongod instances, having the same set of data. It has one primary node which accepts all the write operations coming from the client-side. The rest of the instances are called the secondary nodes in which only read operations are performed.

Features of Replica Set

  1. At least three nodes are required for the replica set.
  2. Only one node of the replica set is primary, the rest of them are secondary.
  3. Data is replicated from primary to secondary after the write operation
  4. If any disaster occurs then a new primary node is assigned from the secondary instances following consensus election.
  5. After recovery of the failed node, it again becomes part of the set but as secondary.

Replica Set creation

  1. Install mongod.exe instances on different servers. This ensures that the failure of any of the instance maintains the availability of others.
  2. Make sure all the instances can connect.

Syntax:

mongod --port "PORT_Number" --dbpath "Mention_DB_Data_Path" --replSet "Named_Replica_Set_Instance"

Example: From the first server Let us say X, issue the following command:

mongod –port27017 –dbpath “C:setup\mongodb\data”—replset rs1

When you set up replica set then command will work on the following way:

  • First, the MongoDB instance starts with name rs1, on the default port number 27017.
  • With the help of the command prompt connect to this instance.
  • To initiate a new replica set, execute the command rs.initiate() on the primary server.
  • Execute the command rs.conf(), to check replica set configuration.
  • Finally execute the command rs.status(), to check the status of the replica set.

3. Group all the servers of the replica set by starting the first instance of mongod.exe.

Mongo –replset “rs1”

Adding servers in a replica set:

In the mongoDB client issue the command rs.add()

Syntax: rs.add(host_name:port)

If we have three servers viz X, Y and Z, and if X is the primary server, then Y and Z can be added by

rs1.add(ServerY”)

rs1.add(“ServerZ”)

The addition of instances is possible after connecting to the primary node. Db.isMaster() command in the client node helps in checking your connection with the primary node.

Removing a server from replica set

We can remove any server from the configuration set by executing rs.remove() command. To execute rs.remove() follow these steps:

  1. Shutdown the instance that needs to be removed. To shutdown execute the following command on mongo shell:

db.shutdownserver

  • Ensure that you are connected to the primary server.

Specify the server name in the given command that you want to remove from the set.

rs.remove("server_Name")

If the replica set is on the server X, Server Y, and Server Z, and we want to remove server Y from the specified replica set, execute the command:

rs.remove(“ServerY”)

In this way, we can manage (Add or Remove) MongoDB instance to the replica set according to the requirement & we can increase the availability of the same data on multiple MongoDB servers.