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.
Replica Set Raft Consensus & Heartbeat Election Lab
Simulate 3-node replica set heartbeats, failovers, oplog replication, and write concern guarantees.
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.
{
"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
rs.printReplicationInfo(); // Primary oplog size and retention window rs.printSecondaryReplicationInfo(); // Lag in seconds per secondary member
5. Critical Engineering Rules
| Rule / Constraint | Underlying Reason & Impact |
|---|---|
| Idempotency | All 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);7. Architectural Trade-offs
- •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+
- •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
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?