MongoDB replication with Delayed Node

Posted By : Ankit Arora | 29-Mar-2017

MongoDB Replication plays an important role to provide reliablity & better performance.

Right now I have a mongo ReplicaSet setup with 3 nodes in which one is primary & two are secondary. As we know whenever data is written/inserted to primary oplog, that data is replicated to secondaries instantly with no lag or delay.

Incase of any disasterous query fired on primary by mistake will also affect the secondary sets.

To avoid this kind of situation we can use or setup delayed replica node in our setup.

 

A Delayed Node:

 

A delayed node is nothing but just a secondary node in replicaset with replication lag from Primary node. Hence this delayed node with fetch the operations from Primary's oplog and replay them on delayed replica node with some delay. It becomes extremely useful to prevent conditions of accidental deletion of data from DB.

 

Note: These delayed nodes are kept hidden to prevent lagged DB reading. Also they can't become primary while election takes place.

 

Configuring delayed node:

 

Adding members in Replicaset on Primary.

 

I hope you have already configured Replicaset before adding replicaset members

 

    primary.domain.in -> Primary Mongo Node
    secondary1.domain.in -> First Secondary Mongo Node.
    secondary2.domain.in -> Second Secondary Mongo Node.
    delayed.domain.in -> Third ‘delayed’ Secondary Mongo Node.

 

 

Connect to primary node:

 

$ mongo --host primary.domain.in --port 27017

 

Initialize the replica set:

rs.initiate();

 

Adding normal secondary Nodes:

myReplicaset:PRIMARY> rs.add("secondary1.domain.in:27017")

myReplicaset:PRIMARY> rs.add("secondary2.domain.in:27017")

 

Adding Delayed secondary Node:

 

myReplicaset:PRIMARY> cnf = {_id: 'myReplicaset', members: [{_id: 0, host: 'primary.domain.in:27017'}, {_id: 1, host: 'secondary1.domain.in:27017'}, {_id: 2, host: 'secondary2.domain.in:27017'}, {_id: 3, host: 'delayed.domain.in:27017', priority: 0, hidden: true, slaveDelay: 600}]}

 

Here I have set 600 seconds in delay.

 

To reconfigure existing replicaset:

 

myReplicaset:PRIMARY> rs.reconfig(cnf)

myReplicaset:PRIMARY> rs.conf()

 

Thanks

About Author

Author Image
Ankit Arora

Ankit is a Redhat Certified Engineer and Cloud Engineer.

Request for Proposal

Name is required

Comment is required

Sending message..