In this blog post, we will learn what MongoDB chained replication is, why you might choose to disable it, and the steps you need to take to do so.
Chain Replication in MongoDB, as the name suggests, means that a secondary member is able to replicate from another secondary member instead of a primary.
By default, chained replication is enabled in MongoDB. It helps to reduce the load from the primary but it may lead to a replication lag. When enabled, the secondary node selects its target using the ping time for the closest node.
The main reason to disable chain replication is replication lag. In other words, the length of the delay between MongoDB writing an operation on the primary and replicating the same operation to the secondary.
In either case—chained replication enabled or disabled—replication works in the same way when the primary node fails: the secondary server will promote to the primary. Therefore, writing and reading of data from the application is not affected.
1) Check the current status of chained replication in replica set configuration for “settings” like this:
|
1 |
PRIMARY> rs.config().settings<br>{<br>"chainingAllowed" : true,<br>} |
2) Disable chained replication, set “chainingAllowed” to false and then reconfig to implement changes.
|
1 |
PRIMARY> cg = rs.config()<br>PRIMARY> cg.settings.chainingAllowed = false<br>false<br>PRIMARY> rs.reconfig(cg) |
3) Check again for the current status of chained replication and its done.
|
1 |
PRIMARY> rs.config().settings<br>{<br> "chainingAllowed" : false,<br>} |
Yes, even after you have disabled chained replication, you can still override sync target, though only temporarily. That means it will be overridden until:
Parameter “replSetSyncFrom” could be used, for example, the secondary node is syncing from host 192.168.103.100:27001 and we would like to sync it from 192.168.103.100:27003
1) Check for the current host it is syncing from:
|
1 |
PRIMARY> rs.status() <br>{<br> "_id" : 1,<br> "name" : "192.168.103.100:27002",<br> "syncingTo" : "192.168.103.100:27001",<br> "syncSourceHost" : "192.168.103.100:27001",<br> }, |
2) Login to that mongod, and execute:
|
1 |
SECONDARY> db.adminCommand( { replSetSyncFrom: "192.168.103.100:27003" }) |
3) Check replica set status again
|
1 |
SECONDARY> rs.status()<br>{<br> "_id" : 1,<br> "name" : "192.168.103.100:27002",<br> "syncingTo" : "192.168.103.100:27003",<br> "syncSourceHost" : "192.168.103.100:27003",<br> }, |
This is how we can override the sync source in case of testing, maintenance or while the replica is not syncing from the required host.
I hope this blog helps you to understand how to disable chained replication or override the sync source for the specific purpose or reason. The preferred setting of the chainingAllowed parameter is true as it reduces the load from the primary node and also a default setting.
Resources
RELATED POSTS