{"id":23940,"date":"2015-07-27T22:16:38","date_gmt":"2015-07-27T16:46:38","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=23940"},"modified":"2015-07-29T10:31:07","modified_gmt":"2015-07-29T05:01:07","slug":"mongo-delayed-node","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/mongo-delayed-node\/","title":{"rendered":"Mongo Delayed Node"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p>This was my live <a title=\"MongoDB Case Study\" href=\"http:\/\/www.tothenew.com\/success-stories\/sony-entertainment-television\">experience with Mongo<\/a> when I felt need of something like Delayed Replica. I had a Mongo ReplicaSet setup for some client with 3 nodes &#8211; 1 Primary and 2 Secondaries. It is known that whatever is written to Primary\u2019s Oplog, is replicated to the secondaries immediately without any lag or lag of some milliseconds (as per network capacity). Any disastrous command if issued by mistake on Primary will also impact the data residing in secondaries. That is what happened !! Someone having root rights inadvertently issued a \u2018drop\u2019 command on Primary which made complete application to go down since it also wiped out all the data on Secondaries too. To avoid such \u2018harmful replication\u2019, we should choose to implement a Delayed Replica Node in our setup.<\/p>\n<p>So, in next paragraphs, I would be focussing on configuring a Delayed Replica node in existing Mongo Replica set which causes a replication delay (lag) between Primary and Delayed Node.<\/p>\n<h3>What is Delayed Node<\/h3>\n<p>A delayed node is like any secondary member in a Replica Set which have a \u2018replication lag\u2019 from Primary. That means this node will fetch group of operations from Primary\u2019s oplog &amp; replay them with some predefined delay. This becomes extremely useful in those conditions where there are high chances of accidental commits on Mongo.<\/p>\n<p>Such Delayed Node can not become Primary while election. Also they are kept hidden from applications to prevent reading lagged data.<\/p>\n<h3>How to configure a Delayed Node<\/h3>\n<p>This goes in continuation with adding members into a Replica Set on Primary. Click <a href=\"http:\/\/www.tothenew.com\/blog\/how-to-setup-replica-sets-in-mongo-db-aws-ec2\/\">here<\/a> for more detail. Let\u2019s go for an example:<\/p>\n<p><i>\u00a0\u00a0\u00a0 primary.domain.in <\/i> <i>-&gt; Primary Mongo Node<\/i><br \/>\n<i>\u00a0\u00a0\u00a0 secondary1.domain.in<\/i> <i>-&gt; First Secondary Mongo Node.<\/i><br \/>\n<i>\u00a0\u00a0\u00a0 secondary2.domain.in<\/i> <i>-&gt; Second Secondary Mongo Node.<\/i><br \/>\n<i>\u00a0\u00a0\u00a0 delayed.domain.in<\/i> <i>-&gt; Third \u2018delayed\u2019 Secondary Mongo Node.<\/i><\/p>\n<h3><b>Connect to to-be Primary Host:<\/b><\/h3>\n<p>[js]$ mongo &#8211;host primary.domain.in &#8211;port 27017 [\/js]<\/p>\n<h3><b>Initialize the replica set:<\/b><\/h3>\n<p>[js]&gt; rs.initiate() [\/js]<\/p>\n<h3><b>Adding normal secondary Nodes:<\/b><\/h3>\n<p>[js]myReplicaset:PRIMARY&gt; rs.add(&quot;secondary1.domain.in:27017&quot;)[\/js]<\/p>\n<p>[js]myReplicaset:PRIMARY&gt; rs.add(&quot;secondary2.domain.in:27017&quot;)[\/js]<\/p>\n<p><strong><strong>\u00a0<\/strong><\/strong><\/p>\n<h3><b>Adding Delayed secondary Node:<\/b><\/h3>\n<p><strong><strong><br \/>\n<\/strong><\/strong><\/p>\n<p>[js]myReplicaset:PRIMARY&gt; cnf = {_id: &#8216;myReplicaset&#8217;, members: [{_id: 0, host: &#8216;primary.domain.in:27017&#8217;}, {_id: 1, host: &#8216;secondary1.domain.in:27017&#8217;}, {_id: 2, host: &#8216;secondary2.domain.in:27017&#8217;}, {_id: 3, host: &#8216;delayed.domain.in:27017&#8217;, priority: 0, hidden: true, slaveDelay: 600}]}[\/js]<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-23989\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/07\/cnf.png\" alt=\"cnf\" width=\"614\" height=\"436\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-decoration: underline;\"><i>Note: <\/i> <\/span><\/p>\n<p style=\"padding-left: 30px;\"><i>priority: Indicates the relative eligibility of a member to become a<\/i> <i>primary.<\/i><\/p>\n<p style=\"padding-left: 30px;\"><i>hidden: Replica Set hides this instance and does not include the member in the output of<\/i><\/p>\n<p style=\"padding-left: 90px;\"><a href=\"http:\/\/docs.mongodb.org\/manual\/reference\/method\/db.isMaster\/#db.isMaster\"><i>db.isMaster()<\/i><\/a><i> or<\/i><a href=\"http:\/\/docs.mongodb.org\/manual\/reference\/command\/isMaster\/#dbcmd.isMaster\"><i> isMaster<\/i><\/a><\/p>\n<p style=\"padding-left: 30px;\"><i>slaveDelay: Number of seconds \u201cbehind\u201d the primary that this replica set member should<\/i><\/p>\n<p style=\"padding-left: 90px;\"><i> \u201clag\u201d. Here we have choosen 600 seconds (10 minutes).<\/i><\/p>\n<p style=\"padding-left: 30px;\"><i>myReplicaset: Replica Set name.<\/i><\/p>\n<p><strong><strong>\u00a0<\/strong><\/strong><\/p>\n<h3><b>Reconfiguration of Existing Replica Set<\/b><\/h3>\n<p><strong><strong>\u00a0<\/strong><\/strong><\/p>\n<p>[js]myReplicaset:PRIMARY&gt; rs.reconfig(cnf)<\/p>\n<p>myReplicaset:PRIMARY&gt; rs.conf()[\/js]<\/p>\n<p><strong><strong>\u00a0<\/strong><\/strong><\/p>\n<h3><b>Check Slave Delay parameters<\/b><\/h3>\n<p><strong><strong>\u00a0<\/strong><\/strong><\/p>\n<p>[js]myReplicaset:PRIMARY&gt; rs.conf()<br \/>\n{<br \/>\n\u00a0\u00a0 \u00a0&quot;_id&quot; : &quot;myReplicaset&quot;,<br \/>\n\u00a0\u00a0 \u00a0&quot;version&quot; : 4,<br \/>\n\u00a0\u00a0 \u00a0&quot;members&quot; : [<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0{<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0&quot;_id&quot; : 0,<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0&quot;host&quot; : &quot;primary.domain.in:27018&quot;,<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0&quot;arbiterOnly&quot; : false,<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0&quot;buildIndexes&quot; : true,<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0&quot;hidden&quot; : false,<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0&quot;priority&quot; : 1,<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0&quot;tags&quot; : {<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0},<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0&quot;slaveDelay&quot; : 0,<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0&quot;votes&quot; : 1<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0},<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0{<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0&quot;_id&quot; : 1,<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0&quot;host&quot; : &quot;secondary1.domain.in:27019&quot;,<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0&quot;arbiterOnly&quot; : false,<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0&quot;buildIndexes&quot; : true,<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0&quot;hidden&quot; : false,<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0&quot;priority&quot; : 1,<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0&quot;tags&quot; : {<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0},<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0&quot;slaveDelay&quot; : 0,<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0&quot;votes&quot; : 1<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0},<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0{<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0&quot;_id&quot; : 2,<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0&quot;host&quot; : &quot;secondary2.domain.in:27020&quot;,<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0&quot;arbiterOnly&quot; : false,<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0&quot;buildIndexes&quot; : true,<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0&quot;hidden&quot; : false,<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0&quot;priority&quot; : 1,<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0&quot;tags&quot; : {<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0},<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0&quot;slaveDelay&quot; : 0,<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0&quot;votes&quot; : 1<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0},<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0{<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0&quot;_id&quot; : 3,<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0&quot;host&quot; : &quot;delayed.domain.in:27021&quot;,<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0&quot;arbiterOnly&quot; : false,<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0&quot;buildIndexes&quot; : true,<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0&quot;hidden&quot; : true,<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0&quot;priority&quot; : 0,<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0&quot;tags&quot; : {<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0},<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0&quot;slaveDelay&quot; : 600,<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0&quot;votes&quot; : 1<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0}<br \/>\n\u00a0\u00a0 \u00a0],<br \/>\n\u00a0\u00a0 \u00a0&quot;settings&quot; : {<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0&quot;chainingAllowed&quot; : true,<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0&quot;heartbeatTimeoutSecs&quot; : 10,<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0&quot;getLastErrorModes&quot; : {<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0},<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0&quot;getLastErrorDefaults&quot; : {<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0&quot;w&quot; : 1,<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0&quot;wtimeout&quot; : 0<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0}<br \/>\n\u00a0\u00a0 \u00a0}<br \/>\n}[\/js]<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><b>Warning<\/b>: rs.reconfig() can make Primary to step down which usually causes an election to choose a new Primary. Recommendation is to make these changes during scheduled downtime.<\/p>\n<p>Post these changes, any commit on Primary node will be replicated to the Delayed Node after \u2018slaveDelay\u2019 seconds, however rest secondary nodes will immediately apply it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; This was my live experience with Mongo when I felt need of something like Delayed Replica. I had a Mongo ReplicaSet setup for some client with 3 nodes &#8211; 1 Primary and 2 Secondaries. It is known that whatever is written to Primary\u2019s Oplog, is replicated to the secondaries immediately without any lag or [&hellip;]<\/p>\n","protected":false},"author":181,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":7},"categories":[1],"tags":[1596],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/23940"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/181"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=23940"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/23940\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=23940"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=23940"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=23940"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}