Reputation: 393
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
Upvotes: 2
Views: 928
Reputation: 6243
During a primary election any writes will result in exceptions and you'll have to retry those writes or relay any messages back to the user. There is no built in logic for retries so you'll have to write your own retry handlers.
Upvotes: 1
Reputation: 17453
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.
Upvotes: 2