0
0
MongoDBquery~10 mins

Oplog and replication mechanism in MongoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Oplog and replication mechanism
Primary Node receives write
Primary writes change to Oplog
Secondary nodes read Oplog entries
Secondaries apply changes locally
Secondaries update data to match Primary
Clients read from Primary or Secondaries
The primary node records all data changes in the oplog. Secondary nodes read these changes and apply them to stay in sync.
Execution Sample
MongoDB
1. Primary inserts document
2. Primary writes insert to oplog
3. Secondary reads oplog entry
4. Secondary applies insert
5. Secondary data matches primary
Shows how a write on primary is recorded and replicated to secondaries via the oplog.
Execution Table
StepActionOplog StateSecondary StateResult
1Primary inserts document {name:'Alice'}EmptyEmptyPrimary data has Alice
2Primary writes insert to oplogOplog: insert {name:'Alice'}EmptyOplog records insert
3Secondary reads oplog entryOplog: insert {name:'Alice'}EmptySecondary sees insert
4Secondary applies insertOplog: insert {name:'Alice'}Data: {name:'Alice'}Secondary data updated
5Secondary data matches primaryOplog: insert {name:'Alice'}Data: {name:'Alice'}Replication complete
6No new oplog entriesOplog: insert {name:'Alice'}Data: {name:'Alice'}Replication idle
💡 No new oplog entries to apply; secondaries are up to date
Variable Tracker
VariableStartAfter Step 2After Step 4Final
Primary DataEmpty{name:'Alice'}{name:'Alice'}{name:'Alice'}
OplogEmptyinsert {name:'Alice'}insert {name:'Alice'}insert {name:'Alice'}
Secondary DataEmptyEmpty{name:'Alice'}{name:'Alice'}
Key Moments - 2 Insights
Why does the secondary node read from the oplog instead of directly from the primary data?
The oplog records all changes in order, so secondaries can apply changes step-by-step to stay consistent. This is shown in execution_table rows 2 to 4.
What happens if the oplog is empty when the secondary tries to read?
If the oplog has no new entries (row 6), the secondary has no changes to apply and stays synchronized with the primary.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the state of the secondary data after step 3?
AEmpty
B{name:'Alice'}
COplog entry present
DPrimary data
💡 Hint
Check the 'Secondary State' column at step 3 in the execution_table
At which step does the secondary apply the insert operation?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look for the action 'Secondary applies insert' in the execution_table
If the primary writes a second insert to the oplog, what will happen next?
APrimary deletes old oplog entries
BSecondary ignores it
CSecondary reads and applies the new oplog entry
DSecondary resets data
💡 Hint
Replication mechanism means secondaries read all new oplog entries to stay updated
Concept Snapshot
MongoDB replication uses an oplog (operation log) on the primary.
Primary writes all data changes to the oplog.
Secondaries read oplog entries in order.
Secondaries apply these changes locally.
This keeps all nodes synchronized automatically.
Full Transcript
In MongoDB replication, the primary node records every data change in a special log called the oplog. This log keeps a list of all operations like inserts, updates, and deletes. Secondary nodes watch this oplog and read new entries as they appear. They then apply these changes to their own data copies. This process ensures that all secondary nodes stay in sync with the primary. The execution table shows step-by-step how a document inserted on the primary is recorded in the oplog and then replicated to a secondary. Variables track the state of data on primary, oplog contents, and secondary data after each step. Key moments clarify why secondaries read the oplog and what happens when no new changes exist. The quiz tests understanding of these steps and the replication flow. Overall, the oplog and replication mechanism provide a reliable way to keep multiple MongoDB nodes consistent.