5 (Migration MongoDB) MongoDB Cluster Migration With Zero Downtime
5 (Migration MongoDB) MongoDB Cluster Migration With Zero Downtime
Prerequisite
Mongo should be running with replica-set
Solution
Add new instances of destination region into existing cluster replica-set as
new replicas
Update DNS entries
ReConfigure replica-set to ensure new primary get's elected from destination
region
Remove old replica from replica-set
Initiate leader election to automatically migrate primary node from source
region to destination region
Step by step migration
Add new replicas (destination region)
Create AMI for any mongo instance (replica) and copy it to the destination
region
Launch n (same number of instances as source region) instances using AMI
Update bindIp of new launch instances to use private Ip of instance and
restart new mongo instances
--bindIp: It is the IP address that MongoDB binds to listen for connections from
different applications
Note: This Step is optional if you are using bindIpAll : true config or you are setting
mongo instance manually instead of AWS AMI.
Login to the primary instance and add all new region instances as a replica
rs.add( { host: "mongodbd4.example.net:27017", priority: 0, votes:0} )
Note: As per mongo documentation, replica-set can trigger a leader election in
response to add replica events hence, we should configure the new members to be
excluded from replica-set leader election during adding as well as synchronisation
stage by setting vote and priority equal to Zerofor all new instances
2
Wait until all new region replicas get sync with the primary or in other words
wait until stateStr becomes SECONDARY state for all new region instances
rs.status()
3
Update DNS records
Update route53 host to point to new instance (n instances of destination
region). To be on a safer side, redeploy all client applications using mongoDb
to refresh DNS entries in case of any caching.
Note: We are using private Ip’s (all instances belonging to the same vpc or vpc
peering is setUp) for internal communication and DNS hostname will be used by
clients for connecting with mongo clusters.
4
Remove existing region replicas
Remove replicasof the source region one by one until only primary left.
rs.remove(hostname)
5
Initiate leader election
Stepping Down current primary to elect new primary in destination region
rs.stepDown(stepDownSecs, secondaryCatchUpPeriodSecs)
Above command instructs the primary of the replica set to become a secondary.
After the primary steps down, eligible secondaries will hold an election for primary.
This will ensure that existing primary will become secondary and the primary will
be elected in the new region.
-- stepDownSecs: The number of seconds to step down the primary, during which
time the step down member is ineligible for becoming primary. If you specify a
non-numeric value, the command uses 60 seconds.
--secondaryCatchUpPeriodSecs: The number of seconds that mongod will wait
for an electable secondary to catch up to the primary. Default is 10 sec.
6
After leader election
Take-Away!
7
MongoDb clusters can indeed be migrated to another region without
downtime. However the replica set cannot process write operations until the
election completes successfully. The median time before a cluster elects a
new primary should not typically exceed 12 seconds. Your application
connection logic should include tolerance for automatic fail overs and the
subsequent leader elections
If you are using MongoDb > 4.2, you will get retryable writes by default. Refer
Docfor more info
The replica set can continue to serve read queries during leader election if
such queries are configured to run on secondaries
https://eng.blackbuck.com/mongodb-cluster-migration-with-zero-downtime/