MongoDB Replication

Replication is the process of copying and maintaining database objects in multiple databases that make up a distributed database system. Replication can improve the performance and protect the availability of applications because alternate data access options exist. For example, an application might normally access a local database rather than a remote server to minimize network traffic and achieve maximum performance. Furthermore, the application can continue to function if the local server experiences a failure, but other servers with replicated data remain accessible.


Replication in MongoDB

MongoDB initiates this replication process using replica set. A replica set contains several data bearing nodes and optionally one arbiter node. Of the data bearing nodes, one and only one member is deemed the primary node, while the other nodes are deemed secondary nodes. We will mention replication details with mongod command as

mongod –replSet <name of replica set with in " ">

The primary node receives all write operations. A replica set can have only one primary capable of confirming writes with { w: "majority" } write concern; although in some circumstances, another mongod instance may transiently believe itself to also be primary. The primary records all changes to its data sets in its operation log.

MongoDB Procedure to deploy Replica Set

Three member replica sets provide enough redundancy to survive most network partitions and other system failures. These sets also have sufficient capacity for many distributed read operations. Replica sets should always have an odd number of members. This ensures that elections will proceed smoothly.

The basic procedure is to start the mongod instances that will become members of the replica set, configure the replica set itself, and then add the mongod instances to it.

1. Start the replica Set of each member as

mongod –replSet “rs0”
(anyname we can use instead of “rs0” but all the nodes/systems need to use the same name of a replication group).

2. Initiate the replica set
  1. rs.initiate() : will initiate replication (initiate system considered as primary)
  2. rs.conf() : Verify the initial replica set configuration
  3. rs.add(): Add the remaining members to the replica set
  4. rs.status() :Check the server status

Example Replication:-
Assuming I have 3 systems having ips

  1. 192.168.0.7 (Primary), 192.168.0.75(Secondary) ,192.168.0.76 (secondary)
  2. Each system installed with MongoDB
  3. Initiate of Replication in systems
    • opening mongod of 192.168.0.7 as mongod –replSet “rs0”
    • opening mongod of 192.168.0.75,76 as mongod –replSet “rs0”
    • in 192.168.0.7 machine
      > rs.initiate()
      Now shell show this
      rs0:PRIMARY>
    • in 192.168.0.7 machine
      rs0:PRIMARY>rs.add(“192.168.0.75:27017”)
      rs0:PRIMARY>rs.add(“192.168.0.76:23396”)
      now 192.168.0.75,76 shell will change as
      rs0:SECONDARY>
  4. Replication done. All the data of primary is replicated in secondary machines, try with create, insert and delete operations in primary it will replicate on secondary machines.

Advantages of Replication:-

  1. Increased reliability and availability
  2. Queries requesting replicated copies of data are always faster
  3. Less Communication Overhead

Disadvantages:-

  1. More Storage Space is needed.
  2. Write operations are costly.
  3. Difficult to main data integrity.