Near time in making ha (highly available) design, database using MongoDB, test scenario is 1 Master 2 from
The test found that when the data is written to MongoDB in bulk (100w), the primary node goes down, the current request throws an exception, the next request is automatically switched to the slave node, and 100w of data has 30w+ data synchronized to the slave node. Illustrates that MongoDB can throw an exception if the primary node goes down at the time of the crud execution and does not guarantee the consistency of the data, that is, 100w data is either all inserted or not inserted.
Therefore, add a layer of data Access Layer (DAO) between the application layer and MongoDB, and after catching an exception in DAO, clear the state of the error operation (such as purging some of the inserted data, etc.) and re-execute it to achieve full transparency to the application.
Think of a sentence: "Add a layer, you can solve the problem"