Current Situation
- we have a MongoDB-Cluster with 1 primary node and 2 secondary nodes
- our Spring-Boot application is using the Spring-Data-MongoDB framework to read/write from/to the cluster
Problem
- in some circumstances the MongoDB cluster will change the primary node (for example during the resizing of the cluster)
- this fail-over phase will affect our Spring-Boot application
- when some reads or writes are still ongoing and the fail-over happens, we receive an exception, because the mongoDB-Server is not reachable anymore for our application
- we have to deal with this state somehow
Questions
1. What is the best way to handle those faile-over states ?
- I've come across the following documentation:
- would it be sufficient to set the
retryReadsandretryWritesflag to true and specify the primary node and the secondary nodes in the connection url? Or should we catch the connection-exception (or alternatively listen to some fail-over-event) and handle those cases by ourself ? - we also have to deal with the following problem: what happens if only 50 % of some bulk-write data got successfully written to the primary node and the other 50 % not ? How handle those cases ideally ?
- this leads us to the second question ...
2. How to detect the fail-over event in Spring-Boot ?
- for our application a possible solution would be to automatically detect the failover state of the MongoDB-Cluster and than just trigger a restart of our Spring-Boot application.
- is there a way to listen to a specific MongoDB-event via spring-data-mongodb in order deal with the case that the primary node has changed?
- alternatively: is there a specific exception we should catch and handle?
I hope somebody can help us here. Thank you in advance!