I have a MonogDB replica set with 2 members and an arbiter. The problem is when the primary node goes down and mongo is selecting a new primary I have some data loss. I believe this is something I can control on the Java driver level.Please help me find the right settings so I don't have any data loss when failover occurs
2
votes
2 Answers
2
votes
If you want to ensure your write operations are only acknowledged when received by primary and at least one secondary use this. It will prevent data loss in case your primary goes down before sync with the secondaries (of course this has some performance costs).
WriteResult result = collection.insert(..., WriteConcern. REPLICAS_SAFE);
More details on write concern are in the MongoDB docs.
Additional Note
As you only have two members in your set all write operations will fail as there is no majority after you loose one node. To avoid this you need to upgrade your arbiter to a full-fledged member.