Replica Set Primary

The primary is the only member in the replica set that receives write operations. MongoDB applies write operations on theprimaryand then records the operations on the primary’soplog.Secondarymembers replicate this log and apply the operations to their data sets.

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

主节点 - 图1

All members of the replica set can accept read operations. However, by default, an application directs its read operations to the primary member. SeeRead Preferencefor details on changing the default 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 Electionsfor more details.

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

主节点 - 图2

| [1] | Insome circumstances, two nodes in a replica set may_transiently_believe that they are the primary, but at most, one of them will be able to complete writes with{w:"majority"}write concern. The node that can complete{w:"majority"}writes is the current primary, and the other node is a former primary that has not yet recognized its demotion, typically due to anetwork partition. When this occurs, clients that connect to the former primary may observe stale data despite having requested read preferenceprimary, and new writes to the former primary will eventually roll back. |
| :—- | :—- |