MongoDB failure scenario

mongodb

Design Question- During data insertion, if MongoDB failed, can I try multiple retries? I have read somewhere it will retry only one time.

  • What will happen if retry fail?
  • Can I keep the failed record somewhere in server memory?

Best Answer

During data insertion, if MongoDB failed, can I try multiple retries? I have read somewhere it will retry only one time.

The Retryable Writes feature in MongoDB 3.6+ allows compatible drivers to automatically retry certain write operations that may be affected by a transient interruption like a network error or replica set election.

Drivers currently only make one retry attempt if the retryWrites option is true. This feature is not designed for persistent network errors where multiple retry attempts are likely to be unsuccessful. The Retryable Writes specification addresses this question: Why are write operations only retried once?.

What will happen if retry fail?

The driver will return a write exception which your application can handle. For example, you could choose to make several retry attempts. For more info on retry strategies, see: How To Write Resilient MongoDB Applications.

Can I keep the failed record somewhere in server memory?

This is part of the exception handling logic in your application. A write resulting in an exception has not been successfully acknowledged by your MongoDB server deployment, so your application will have to decide how to handle appropriately for your use case. The original data/objects will still be available in your application context (subject to the normal scoping/lifecycle rules of your programming language).