简体   繁体   中英

MongoDB Java Driver Replica Set Failover

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM