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
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Ankit Arora
Ankit is a Redhat Certified Engineer and Cloud Engineer.