liveBook · Manning

Primary node

As explained in MongoDB 8.0 in Action, Third Edition

A primary member is the replica set member that accepts write operations and coordinates replication.

A primary node is the replica set member that accepts writes. It executes database operations, records successful data-changing operations in its oplog, and streams them to secondary members. Secondaries apply those operations asynchronously, so their progress can lag behind the primary. section 9.1.1 section 9.1.3

To inspect the role in mongosh:

  • Run `db.adminCommand("replSetGetStatus")` or `rs.status()` to see each member’s current state. `PRIMARY` identifies the write-accepting member; `SECONDARY` identifies a replicating member. section 9.1.1
  • Run `db.adminCommand("replSetGetConfig")` or `rs.config()` to inspect member settings such as priority and votes. The configuration command requires an M10 or larger Atlas cluster. section 9.1.1
Listing 9.1 Output of the replSetGetStatus command
db.adminCommand("replSetGetStatus").members.map((m) =>
➥({ _id: m._id, name: m.name, state: m.state, stateStr: m.stateStr }))
[
 {
  _id: 0,
  name: 'ac-5dhjxpf-shard-00-00.fpomkke.mongodb.net:27017'
  state: 2,
  stateStr: 'SECONDARY'
 },
 {
  _id: 1,
  name: 'ac-5dhjxpf-shard-00-01.fpomkke.mongodb.net:27017'
  state: 1,
  stateStr: 'PRIMARY'
 },
 {
  _id: 2,
  name: 'ac-5dhjxpf-shard-00-02.fpomkke.mongodb.net:27017'
  state: 2,
  stateStr: 'SECONDARY'
 }
]

The status listing shows one member reporting `PRIMARY` and two reporting `SECONDARY`, making the write role easy to distinguish from the replication roles. section 9.1.1

Replication improves availability and can distribute reads, but every write in a replica set still passes through its primary. If that becomes a write bottleneck, sharding partitions data so writes to different shards can be handled by their respective primaries. Each shard still needs replication for availability. section 9.0

How MongoDB 8.0 in Action, Third Edition covers it

How deep it goes
Explains member states and the primary-to-secondary oplog flow, with shell commands for inspection rather than primary configuration steps.
Examples
Uses replica set status and configuration output alongside oplog entries for session deletions and inserts.
What it assumes
Assumes access to a MongoDB replica set while introducing member states and the oplog before inspecting their output.
What you can do after
Operate/configure it by identifying the primary with mongosh status commands and checking member settings where supported.
How it connects
Connects replica sets, secondary nodes, elections, the oplog, replication lag, and sharding.

Also explained in

Compare how 4 books explain primary node →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of MongoDB 8.0 in Action, Third Edition. The section links point to the parts of the book it draws on.