MongoDB – Replication

 Replication defends a database from a single server’s loss. Replication also helps you to recover from the failure of hardware and interruptions of service. You may dedicate one to disaster recovery, reporting, or backup, with additional copies of the data

The process of synchronizing data through several servers is replication. With several copies of data on various database servers, replication provides consistency and improves the availability of data. Replication defends a database from a single server’s loss. Replication also helps you to recover from the failure of hardware and interruptions of service. You may dedicate one to disaster recovery, reporting, or backup, with additional copies of the data.

Why Replication?

  • To keep your data safe
  • High (24*7) availability of data
  • Disaster recovery
  • No downtime for maintenance (like backups, index rebuilds, compaction)
  • Read scaling (extra copies to read from)
  • The replica set is transparent to the application

How Replication Works in MongoDB

MongoDB achieves replication by utilizing the replica set. A replica set is a collection of MongodB instances that all have the same set of data. A replica has one primary node that receives all writing operations. All other cases, such as secondary ones, use primary operations to achieve the same data collection. The replica set can only have one primary node.

  • A replica set is a group of two or more nodes (a minimum of 3 nodes is usually required).
  • One node is the primary node in a replica set and the remaining nodes are secondary.
  • It replicates all data from the primary to the secondary node.
  • At the time of automatic failover or maintenance, the primary option is established and a new primary node is selected.
  • It decides to join the replica collection again after the recovery of the failed node and acts as a secondary node.

A typical MongoDB replication diagram is shown, in which the client application always interacts with the primary node before replicating data from the primary node to the secondary nodes.

Replica Set Features

  • A cluster of N nodes
  • Any one node can be primary
  • All write operations go to primary
  • Automatic failover
  • Automatic recovery
  • Consensus election of primary

Set Up a Replica Set

We’ll convert the standalone MongoDB instance to a replica set in this tutorial. To convert to the replica set, the steps below are

mongod --port "PORT" --dbpath "YOUR_DB_DATA_PATH" --replSet "REPLICA_SET_INSTANCE_NAME"
  • Shutdown The MongoDB server is already running.
  • Start the server on MongoDB by specifying the– replSet option. The following is the simple replSet — syntax

Example

mongod --port 27017 --dbpath "D:\set up\mongodb\data" --replSet rs0
  • This will initiate a Mongod instance on port 27017 with the name rs0.
  • Start the command prompt now and connect to this instance of the Mongod.
  • Use the rs.initiate() command in the Mongo client to initiate a new replica set.
  • To check the configuration of the replica set, issue the rs.conf() command. The rs.status() command is used to check the status of the replica set.

Add Members to Replica Set

Start the Mongod instances on multiple machines to add members to a replica set. Open a Mongo client now and issue an rs.add() command.

Syntax

The basic syntax of rs.add() command is as follows −

>rs.add(HOST_NAME:PORT)

Example

Suppose your name for the Mongod instance is mongod1.net and runs on port 27017. Use the rs.add() command in the Mongo client to add this instance to the replica set.

>rs.add("mongod1.net:27017")
>

When you are connected to the primary node, you can only add the Mongod instance to the replica collection. In the mongo client, use the db.isMaster() command to determine whether you are connected to the primary or not. 

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *