0
0
MongoDBquery~10 mins

Primary and secondary nodes in MongoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Primary and secondary nodes
Start: Replica Set Initiated
Elect Primary Node
Primary Accepts Writes
Primary Replicates Data
Secondary Nodes Receive Data
Secondaries Serve Reads (optional)
If Primary Fails
Election: New Primary Chosen
Continue Operations
The replica set starts, elects a primary node that handles writes and replicates data to secondary nodes, which can serve reads. If the primary fails, a new primary is elected to keep the system running.
Execution Sample
MongoDB
rs.initiate()
rs.status()
rs.stepDown()
rs.freeze(0)
Commands to start a replica set, check status, force primary to step down, and unfreeze nodes for election.
Execution Table
StepActionPrimary NodeSecondary NodesResult
1Initiate replica setNoneNoneReplica set starts, no primary yet
2Elect primary nodeNode ANodes B, CNode A becomes primary, others secondary
3Primary accepts writeNode ANodes B, CData written to primary
4Primary replicates dataNode ANodes B, CSecondaries replicate data from primary
5Secondaries serve readsNode ANodes B, CSecondaries can serve read requests
6Primary failsNode A (down)Nodes B, CPrimary node goes down
7Election for new primaryNode BNode CNode B elected as new primary
8Operations continueNode BNode CWrites and replication continue with new primary
💡 Replica set maintains availability by electing new primary if current primary fails
Variable Tracker
VariableStartAfter Step 2After Step 6After Step 7Final
Primary NodeNoneNode ANode A (down)Node BNode B
Secondary NodesNoneNodes B, CNodes B, CNodes CNodes C
Data ConsistencyN/AConsistentConsistentConsistentConsistent
Key Moments - 3 Insights
Why does the replica set need to elect a primary node?
Because only the primary node accepts write operations and replicates data to secondaries, ensuring data consistency. See execution_table step 2 where Node A becomes primary.
What happens to writes when the primary node fails?
Writes cannot be accepted until a new primary is elected. Execution_table step 6 shows primary down, and step 7 shows election of new primary Node B.
Can secondary nodes serve read requests?
Yes, secondary nodes can serve reads to reduce load on primary, as shown in execution_table step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, which node is the primary after step 7?
ANode A
BNode C
CNode B
DNo primary
💡 Hint
Check the 'Primary Node' column at step 7 in execution_table
At which step does the primary node fail?
AStep 4
BStep 6
CStep 5
DStep 7
💡 Hint
Look for 'Primary fails' action in execution_table
If the primary node is down, what must happen before writes can continue?
AA new primary must be elected
BSecondaries serve writes immediately
CReplica set shuts down
DPrimary node restarts automatically
💡 Hint
Refer to execution_table steps 6 and 7 about primary failure and election
Concept Snapshot
Primary and secondary nodes in MongoDB replica sets:
- Primary node handles all writes and replicates data.
- Secondary nodes replicate data and can serve reads.
- If primary fails, secondaries hold election to choose new primary.
- This ensures high availability and data consistency.
- Use rs.initiate(), rs.status(), rs.stepDown() to manage nodes.
Full Transcript
In MongoDB replica sets, one node is elected as primary to accept writes and replicate data to secondary nodes. Secondary nodes replicate data from the primary and can serve read requests to balance load. If the primary node fails, the replica set automatically holds an election to choose a new primary, ensuring continuous availability. Commands like rs.initiate() start the replica set, rs.status() shows current node roles, and rs.stepDown() forces the primary to step down to test failover. This process keeps data consistent and the database available even if some nodes fail.