I would wish to discuss the concept of replication and replica set in MongoDB
.
How do we get availability and fault tolerance? And by that we mean if that node goes down, we want to still be able to use the system. And if the primary nodes goes down and we lose it entirely for some reason, let's say there's a lost between backups or a hardware damage making the system unusable. And so what we do to solve both those problems is we introduce replication.
A replica set refers to a set of mongod
and mongo
nodes that act together and all mirror each other in terms of data. There is one primary and the other nodes are secondaries. But, that selection is dynamic. And the data written to the primary will asynchronously replicate to the secondaries. The application and drivers stay connected to the primary and will and can only write to the primary. Say, if primary goes down, one of the secondaries will perform an election to elect a new primary. To elect a new primary, we have to have a strict majority of the original number of nodes. So, since the original number of nodes here was 3, we need 2 nodes to elect a new primary and that's the number we have. So, if one went down, then anyone else can become primary. And in that case, the app will connect to the primary for the rights, through the driver. All transparently.
Later if the down servers gets up, it will join the replica set as a secondary. And the minimum number of nodes is 3 - because if we have less than 3, then what would remain would not be a majority of that set of the original set. And so, there would be no way to elect the new primary. So, we would go with no primary, which means we could no longer take rights.