When the primary node fails, a coordinated failover process begins automatically. First, the secondary nodes detect that the primary is unreachable through the absence of heartbeat messages. Replica set members send heartbeat pings every two seconds, and if a member does not respond within 10 seconds, it is marked as unreachable.
Second, once secondaries confirm the primary is down, they initiate an election. Any secondary can call for an election if it detects the primary is unavailable. The election process involves members voting for which secondary should become the new primary.
Third, during the election, members evaluate candidates based on priority settings, data recency, and availability. Members with outdated data or lower priority will not receive votes. The election must reach a majority consensus, so in a three-member replica set, at least two members must vote for the same candidate.
Fourth, once a candidate receives votes from the majority of members, it transitions to the primary role and begins accepting write operations. The entire election process typically completes in a few seconds, though it can take up to 30 seconds in some scenarios.
Fifth, applications using MongoDB replica set connection strings automatically discover the new primary through the connection driver. The driver maintains a list of replica set members and can redirect operations to the new primary without manual intervention. There may be a brief period where write operations fail during the election, so applications should implement retry logic.
Finally, when the failed original primary recovers and rejoins the replica set, it becomes a secondary and syncs data from the new primary.
Example code
// Timeline of failover:
// Time 0: Primary node crashes
// Time 2-10s: Secondaries detect primary unreachable (heartbeat timeout)
// Time 10s: Secondary initiates election
// Time 10-15s: Election process, members vote
// Time 15s: New primary elected, begins accepting writes
// Time 16s: Applications reconnect to new primary
// Application connection with automatic failover
const client = new MongoClient(
'mongodb://host1:27017,host2:27017,host3:27017/?replicaSet=myRS',
{
retryWrites: true, // Automatic retry on failover
w: 'majority' // Wait for replication
}
)
// After failover completes
// Old primary (when recovered): joins as secondary
// New primary: handles all writes
// Other secondary: continues replicating