Explain the Oplog
The MongoDB Oplog (Operations Log)
The Oplog is the engine of replication in MongoDB. Located in the local.oplog.rs collection, it is a circular capped collection that records all write operations applied to a replica set.
Oplog Mechanics & Architecture
| Characteristic | Oplog Implementation | Significance |
|---|---|---|
| Storage Engine | Capped collection (fixed maximum disk size) | Overwrites oldest entries when full; zero fragmentation |
| Idempotency | Operations are converted to idempotent actions | Applying an entry multiple times yields the exact same state (e.g. $inc becomes $set) |
| Replication Process | Secondaries tail the primary's oplog using tailable cursors | Asynchronous, streaming replication with minimal latency |
| Oplog Window | Time duration between the oldest and newest oplog entry | Defines how long a secondary can stay offline before requiring a full initial sync |
Key Production Considerations
- Oplog Sizing: If heavy batch writes exhaust the oplog faster than secondaries can replicate, secondaries fall into
RECOVERINGstate. - Change Streams Dependency: Change Streams read directly from the oplog to publish real-time change events.