This is article completely summary of official mongo site : http://docs.mongodb.org/manual/core/replica-set-high-availability/
Replica Set
- Replica set provides High Availability using automatic failover. Failover allows secondary members to become primary if primary is unavailable. Failover, in most situations does not require manual intervention.
- Replicas set members keep the same data set but they are independent.
- To select Primary Server there will be an election between other servers. Place a majority of voting members and all the members that can become primary in this facility.
Replica Set Elections
- Replica sets use elections to determine which set member will become primary. Election occurs after initiating a replica set, and also any time the primary becomes unavailable
- The primary is the only member in the set that can accept write operations.
- Elections are part of the failover process
- While an election is in process, the replica set has no primary and cannot accept writes.
Factors and Conditions that Affect Elections
Heartbeats
- Replica set members send heartbeats (pings) to each other every two seconds. If a heartbeat does not return within 10 seconds, the other members mark the delinquent member as inaccessible.
Priority Comparisons
- The Priority setting affects elections. Priorities specify higher values to make a member more eligible to become primary and lower values to make the member less eligible to become primary.
- Priority 0 = Cannot become primary and do not seek election
Optime
- The timestamp of the last operation that a member applied from the oplog.
Connections
- A replica set member cannot become primary unless it can connect to a majority of the members in the replica set
Election Triggering Events
- Replica sets hold an election any time there is no primary. Specifically, the following:
- The initiation of new replica set.
- A secondary loses contact with a primary. Secondaries call for elections when they cannot see a primary.
- A primary step down.
o
After receiving the replSetStepDown command
o
If one of the current secondaries is eligible
for election and has a higher priority
o
If primary cannot contact a majority of the
members of the replica set.
Note: When a primary steps down, it closes all open client
connections, so that client don’t attempt to write data to a secondary. This
helps client maintain an accurate view of the replica set and helps prevent
rollbacks.
Participation in Elections
- By default, all members have a priority of 1 and have an equal chance of becoming primary. In the default, all members also can trigger an election.
- Only members in the following states can vote: PRIMARY, SECONDARY, RECOVERING, ARBITER, and ROLLBACK.
- Non-voting members hold copies of the replica set’s data and can accept read operations from client applications
- Non-voting members do not vote in elections, but can “veto” an election and become primary.