MongoDB Replication - MongoDB

What is replication in MongoDB?

Replication in MongoDB is the process of matching data across multiple servers. Replication will offer and it will increase the data availability with multiple copies of data on different database servers. Replication will protect a database from the thrashing of a single server. Replication will also help in recovering from hardware failure and service interruptions. With additional copies of data, each data can be dedicated to disaster recovery, reporting, or backup.

Why Replication?

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

How Replication Works in MongoDB

MongoDB can achieve replication by using replica set. Replica set is a group of mongodb instances which hosts same data set. Replica has a primary node which receives all write operations. All other instances, such as secondaries, will apply operations from the primary so that they will have the same data set. Replica set can have only one primary node.

  • Replica set is a group of two or more nodes (basically minimum 3 nodes are needed).
  • In a replica set, one node will be a primary node and remaining nodes will be secondary.
  • All data will replicate from primary to secondary node.
  • During automatic failover or maintenance, election will be established for a primary and a new primary node is elected.
  • After the recovery of failed node, it again joins the replica set and works as a secondary node.

Typical diagram of MongoDB replication is shown below in which the client application always interact with the primary node and the primary node will then replicates the data to the secondary nodes.

replication

What are the features of Replica Set

  • 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

In this tutorial, standalone MongoDB instance can be converted to a replica set. To convert to replica set, follow below steps

  • Shutdown already running MongoDB server.
  • Start the MongoDB server by specifying -- replSet option. Following is the basic syntax of --replSet

Example

  • It starts a mongod instance with name rs0, on port 27017.
  • Now start the command prompt and connect to this mongod instance.
  • In Mongo client, issue the command rs.initiate() for initiating a new replica set.
  • For checking the replica set configuration, issue the command rs.conf(). To check the status of replica set, issue the command rs.status().

Add Members to Replica Set

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

Syntax

Basic syntax of rs.add() command is as follows

Example

Assume that your mongod instance name is mongod1.net and it is running on port 27017. To add this instance to replica set, issue the command rs.add() in Mongo client.

Mongod instance can be added to a replica set only when you are connected to primary node. To check if you are connected to primary or not, issue the command db.isMaster() in mongo client.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

MongoDB Topics