Replica Set Primary

The primary is the only member in the replica set that receives writeoperations. MongoDB applies write operations on the primary andthen records the operations on the primary’s oplog. Secondary members replicate this log and applythe operations to their data sets.

In the following three-member replica set, the primary accepts allwrite operations. Then the secondaries replicate the oplog to apply totheir data sets.

Diagram of default routing of reads and writes to the primary.

All members of the replica set can accept read operations. However, bydefault, an application directs its read operations to the primarymember. See Read Preference for details on changing thedefault read behavior.

The replica set can have at most one primary.[1] If the current primary becomes unavailable,an election determines the new primary. SeeReplica Set Elections for more details.

In the following 3-member replica set, the primary becomes unavailable.This triggers an election which selects one of the remainingsecondaries as the new primary.

Diagram of an election of a new primary. In a three member replica set with two secondaries, the primary becomes unreachable. The loss of a primary triggers an election where one of the secondaries becomes the new primary

[1]In some circumstances, two nodes in a replica setmay transiently believe that they are the primary, but at most, oneof them will be able to complete writes with { w:"majority" } write concern. The node that can complete{ w: "majority" } writes is the currentprimary, and the other node is a former primary that has not yetrecognized its demotion, typically due to a network partition.When this occurs, clients that connect to the former primary mayobserve stale data despite having requested read preferenceprimary, and new writes to the former primary willeventually roll back.