MongoDB Cluster Migration With Zero Downtime

Over the last few years, our servers at BlackBuck were distributed between various regions of AWS (Amazon Web Services), based on each of the POD's requirements.

Considering the dual benefit of achieving better latencies with S2S call and  cost saving data transfer with the reusable common platform infrastructure, we decided to shift all of our infrastructure to a single region of the AWS.

MongoDB is one of the commonly used databases in BlackBuck for a variety of use cases (be it transactional, configurational and reporting). We needed to minimise the downtime in order to maintain business continuity as it's used in a few tier-1 applications.

This article will document the steps required to migrate your MongoDB cluster to a new AWS region without any downtime. At the end of this article, you’ll have MongoDB migrated to a new region. Without further delay, let’s start.

Problem Statement

Prerequisite


Solution


Step by step migration

Add new replicas (destination region)

--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.

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 Zero for all new instances

rs.status()

After adding new instance as replica


Update DNS records

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.


Reconfiguring replica set

Note: In order to ensure leader get's elected from destination region, we will be setting new region replicas priority higher than old region and give voting rights to new replicas


Remove existing region replicas

rs.remove(hostname)

After removing replica instance from existing region


Initiate leader election

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.

After leader election

Final stage

Take-Away!

References

[rs.add() — MongoDB Manual\ \ \ \

[rs.remove() — MongoDB Manual\ \ \ \

[rs.stepDown() — MongoDB Manual\ \ \ \

[rs.reconfig() — MongoDB Manual\ \ \ \

[Replica Set Member States — MongoDB Manual\ \ \ \

Disqus Comments

We were unable to load Disqus. If you are a moderator please see our troubleshooting guide.

G

Start the discussion…

Comment

Log in with
or sign up with Disqus or pick a name

Disqus is a discussion network

Read full terms and conditions

This comment platform is hosted by Disqus, Inc. I authorize Disqus and its affiliates to:

Acknowledge I am 18 or older

Favoriting means this is a discussion worth sharing. It gets shared to your followers' Disqus feeds, and gives the creator kudos!

Find More Discussions

Share

Be the first to comment.

Load more comments

live.rezync.com

live.rezync.com is blocked

This page has been blocked by an extension

ERR_BLOCKED_BY_CLIENT

Reload

This page has been blocked by an extension

pippio.com

pippio.com is blocked

This page has been blocked by an extension

ERR_BLOCKED_BY_CLIENT

Reload

This page has been blocked by an extension