In typical database environment we need to have back up of our database because if we don’t have if in any case our database goes down it will be disaster and typically we have our database back up in a timely interval and then once something goes wrong we restore our data from the latest back up we have.
But in NoSQL environment more specifically MongoDB we have something called Replica set where the minimum servers is 3 and they are Primary, Secondary and Arbiter server we can have more than one secondary server depending upon our requirement, let’s look into each one of them in detail.
Primary Database Server:
Primary is the one and only writable instance in a replica set , that means if any application or client want to make changes to the database they have to be connected to primary database and only issue commands to primary database, you cannot directly write data to secondary database if you attempt to do so you will be failed.
Secondary Database Server:
In secondary database you can have multiple database servers which means you have the power over scalability and you can scale your database as much as you want by adding new Secondary database and secondary database in MongoDB is only readable from the primary database.
If in any case the primary database failed to start or went down one of your secondary database will take place of primary database and how this works is by voting when in any time a primary database went down there will be election automatically and the more vote a secondary database got that would take place of primary database.
Arbiter Database Server:
As I mentioned earlier that in Mongo when a primary went down it will automatically be replaced by one of primary server and the question here is which secondary server will take place of primary server, in this case what Mongo does is it holds an election which ever Mongo secondary get more votes it will be the primary.
An if you have an even number of server (one Primary, one Secondary) if in any case the primary goes down then the secondary won’t become the primary and the reason is because you have to have the majority of the votes in order to become primary.
And if we have a replica set of 3 servers and something went wrong which caused our primary server to break down here what we left is 2 servers once the election started those two server vote for each other and the election would be tie as in election you need to have majority of the votes and in this case it’s not happening because it’s 50/50.
What Mongo does is it have a special kinds of Server Arbiter server that its sole purpose of existing is to vote which breaks the ties it does not have any data stored in it, it only exist to vote and break the tie so that the servers functions normally even if after a primary break down in some points and start replicating the data.
Hope you find this use full in the next blog post I will be showing you how to configure this please visit tomorrow for that post, and do let me know if you want me to write about any specific subject.
What is Replica Set in MongoDB