I have read Mongo Docs and some posts, but it's not clear enough to me. I don't understand how it works when Mongo is distributed on different hosts
I want to develop a cluster with replica sets (so an app can stand network and database issues) on different hosts (EC2 instances). Let's assume this scenario (based on this tutorial ):
Instance A: Primary node on 10.0.0.1
Instance B: Secondary node on 10.0.0.2
Instance C: Arbiter on 10.0.0.3
My app is working normal and is connected to A (the IP 10.0.0.1 is configured as an environment variable). Suddenly Mongo inside A crashes due to any reason, then Mongo does its stuff, and C chooses B as the new Primary. What will happen with my app that is connected to A? Does Mongo redirect to B automatically? What happens when the whole instance (A) is down? Do I need to add some additional code to handle this?
I have been working only with single mongo instance, but this time I want to do something bigger and more consistent, and this confuses me. I appreciate your answers.
... Mongo inside A crashes due to any reason, then Mongo does its stuff, and C chooses B as the new Primary. What will happen with my app that is connected to A? Does Mongo redirect to B automatically? What happens when the whole instance (A) is down? Do I need to add some additional code to handle this?
The replica set has three nodes - A, B and an arbiter C. When the node A goes down, the secondary node B gets elected as the primary. The app connected to A earlier, after the node B is elected as primary, connects to B automatically. See Primary with a Secondary and an Arbiter.
All applications connect to MongoDB database (mongod instance) via (or thru) a driver software. For example, the mongo shell utility has its own driver program. In case your app is a NodeJS or Java app, there are respective driver softwares. These driver softwares are replica-set aware - means that the driver knows that it is connecting to a replica set's primary node, and as the mongod crashed it cannot access (read or write to) that primary and waits till a new primary is elected (it can take few tens of secs to elect a new primary), and then connects to the new primary. All this happens automatically. So, the application developer need not do any special programming, in the app, to connect to the new primary in case a primary node becomes unavailable.
In general, the driver program also has the functions: connecting to the MongoDB database and converting the application's data formats to BSON format for storing in the MongoDB database (and back). Note that data is stored in MongoDB database in BSON format).