Skip to content
LESSON 43 / 50Advanceddistributed

The Replication Oplog & Resolving Replication Lag

The Oplog (operations log) is a capped collection recording idempotent state modifications; monitor and resolve replication lag to avoid stale reads and failover rollbacks.

Interactive Engine Simulator

Replica Set Raft Consensus & Heartbeat Election Lab

Simulate 3-node replica set heartbeats, failovers, oplog replication, and write concern guarantees.

Consensus Term
Term 1
Monotonically increasing
Voting Quorum
2 / 3 Majority
Strict floor(N/2) + 1
Heartbeat Interval
2000 ms
10s election timeout
Write Concern
node-1PRIMARY
Host:mongo-node-1:27017
Oplog Sequence:#1045
Voted Term:Term 1
Accepts all driver write and read traffic (ReadPreference: primary).
node-2SECONDARY
Host:mongo-node-2:27017
Oplog Sequence:#1045
Voted Term:Term 1
Tails primary local.oplog.rs in background thread.
node-3SECONDARY
Host:mongo-node-3:27017
Oplog Sequence:#1045
Voted Term:Term 1
Tails primary local.oplog.rs in background thread.
REPL_LOG:Replica set stable on Term 1. Primary node-1 actively serving writes.

1. Formal Definition & Architecture Intent

The Oplog (local.oplog.rs) is a capped collection on every replica set member that records a rolling log of all write operations that modify database state. Secondaries continuously poll their sync source and apply oplog entries asynchronously. Replication Lag is the time delay between when a write is committed on the Primary and when it is applied on a Secondary.

2. Explain Like I'm 5 (Mental Model)

The primary is an architect drawing building blueprints. The secondaries are builders following the architect's instructions. Replication lag is the time it takes the builder to catch up when the architect draws faster than the builder can lay bricks.

3. Real-World Architecture Example

Diagnosing a secondary node that has fallen 45 seconds behind the primary.

Sample BSON Documents:
Collection: db.rs.printSecondaryReplicationInfo()
{
  "source": "mongo-2:27017",
  "syncedTo": "2026-03-01T14:20:15Z",
  "oplogWindow": "72 hours",
  "replicationLag": "45 seconds"
}

Clients issuing reads with secondaryPreferred read preference risk reading stale data that is 45 seconds out of date.

4. MQL Implementation Steps

Step 1: Check replication lag and oplog window in mongosh
rs.printReplicationInfo(); // Primary oplog size and retention window
rs.printSecondaryReplicationInfo(); // Lag in seconds per secondary member
Monitors the time gap between primary write timestamp and secondary sync.

5. Critical Engineering Rules

Rule / ConstraintUnderlying Reason & Impact
IdempotencyAll oplog entries are idempotent: applying the same entry multiple times produces the exact same result ($inc becomes $set).
Oplog Falling Off (Stale)If a secondary falls further behind than the total oplog window (e.g. down for 3 days on a 2-day oplog), it enters RECOVERING state and requires resyncing.

6. Deep Dive: How $inc Transforms into Idempotent $set in the Oplog

Consider the update: db.accounts.updateOne({ _id: 1 }, { $inc: { balance: 50 } }). If balance was 100, the primary does NOT record "$inc 50" in the oplog (which would cause corruption if replayed twice). Instead, the primary computes the final balance (150) and writes an idempotent $set into the oplog: { "o": { "$v": 2, "$set": { "balance": 150 } } }.

// Inspect recent raw oplog entry:
db.getSiblingDB("local").oplog.rs.find().sort({ $natural: -1 }).limit(1);
Production Warning: If a secondary falls off the oplog window, it requires an initial sync from scratch.

7. Architectural Trade-offs

Advantages
  • •Idempotent log ensures that network retries or out-of-order replays never corrupt data
  • •Powers Change Streams, Change Data Capture (CDC), and point-in-time backup restores
  • •Resizable dynamically without restarting mongod in MongoDB 4.4+
Disadvantages & Gotchas
  • •Heavy bulk write ingestion can outpace secondary disk write IOPS, causing replication lag spikes
  • •Undersized oplog windows risk secondaries falling off during temporary network outages

8. Technical Interview Questions

Q1: Why are operations recorded in the MongoDB replication oplog converted to idempotent operations?

Oplog operations are idempotent (applying them multiple times yields the exact same state) so that secondaries can safely replay entries after network disconnects, restarts, or crash recovery without data corruption. For example, non-idempotent operations like $inc: { count: 1 } are translated into deterministic $set: { count: 42 } entries in the oplog.

9. Staff-Level Ready Answer

"The Oplog (local.oplog.rs) is a capped collection recording all database mutations as idempotent operations (converting $inc to $set). Secondaries continuously replicate and apply oplog entries. Replication lag occurs when secondary write IOPS or network throughput cannot match primary ingestion rates."

10. Quick Knowledge Check

How does MongoDB transform a non-idempotent $inc operation when recording it into the replication Oplog?