Search code examples
mongodbreplicationfailover

What if MongoDB failover takes place when replication isn't finished yet?


I'm new to MongoDB, I have one question:

I'm setting up a MongoDB test env, with one mongos, 3 conf servers, and 2 shards (3 servers as a replication set for a shard). Let's say, for reasons I have a big lag of replication (e.g. secondary is backing up, or there's a network issue) This time, the primary server is down, what will happen?

Auto fail-over selects one secondary db as the new primary, but what about the data that hasn't been replicated yet? Are we going to lose data? If so, what can we do to get data back, and what needs to be done, to avoid such an issue?


Solution

  • during this time, the primary server is down, what will happen ?

    No writes will be allowed

    Auto fail-over select one secondary db as new primary, how about these data havnt replicated yet ?

    If data has no replicated from the primary to the secondary which then becomes primary then a rollback will occur when the primary comes back into the set as a secondary: http://docs.mongodb.org/manual/core/replica-set-rollbacks/

    Of course as to whether you lose data or not depends on whether the write went to journal and/or data files and whether the member just left the set or crashed. If the member crashed before the write could go to journal then there is a chance that the write could be lost, yes.

    how can we do to get data back and what need to be done, to avoid such issue.

    You can use w=majority for most cases but this will still have certain pitfalls within edge cases that just cannot be taken care of, for example if the primary fails over before the write can be propogated to other members etc.