MongoDB Oplog
What Is Oplog?
The oplog (operations log) is a special capped collection, local.oplog.rs, on each replica set member. When the primary applies a write, it records an entry describing the change. Secondaries continuously read the primary's oplog and replay those entries to stay in sync. Oplog entries are idempotent, so replaying them more than once produces the same result.
Why It Matters
| Reason | Benefit |
|---|---|
| Powers replication | Without the oplog, secondaries would have no way to copy the primary's changes. |
| Enables catch-up | A secondary that was offline briefly can resume from where it stopped. |
| Supports change streams | Change streams are built on the oplog data. |
| Useful for diagnosis | The oplog size and window tell you how much downtime a member can tolerate. |
How To Use It
- Connect to a replica set member with mongosh.
- Run rs.printReplicationInfo() to see oplog size, used size, and the time window.
- Run rs.printSecondaryReplicationInfo() to see replication lag on secondaries.
- Switch to the local database with use local.
- Look at the latest entry: db.oplog.rs.find().sort({ $natural: -1 }).limit(1).
- Insert a document on the primary and find the matching entry with op: "i".
- If the window is short for your write volume, plan a larger oplog size.
mongosh Examples
// Oplog size and window
rs.printReplicationInfo()
// How far behind are secondaries?
rs.printSecondaryReplicationInfo()
// Peek at the newest entries
use local
db.oplog.rs.find().sort({ $natural: -1 }).limit(3)
// Find recent inserts into shop.orders
db.oplog.rs.find({ ns: "shop.orders", op: "i" }).sort({ $natural: -1 }).limit(1)
// Increase the oplog to 16 GB (value in MB), run on each member
db.adminCommand({ replSetResizeOplog: 1, size: 16384 }) Edge Cases & Pitfalls
Oplog too small
Heavy write loads can shrink the replication window to minutes. A secondary offline longer than that must resync fully.
Do not write to the oplog
It is managed by MongoDB. Never insert, update, or delete entries manually.
Only on replica set members
Standalone servers have no oplog. They do not replicate.
Large updates produce many entries
Multi-document writes and bulk operations can fill the oplog quickly.
Cheat Sheet
| Topic | Detail |
|---|---|
| local.oplog.rs | The oplog collection on replica set members |
| Capped | Fixed size; oldest entries are overwritten when it is full |
| Idempotent entries | Applying the same entry multiple times gives the same result |
| ts | Timestamp of the operation, used to track replication progress |
| op | Operation type: i insert, u update, d delete, c command, n no-op |
| ns | Namespace (database.collection) the operation affected |
| rs.printReplicationInfo() | Show oplog size and the time range it covers |
| rs.printSecondaryReplicationInfo() | Show how far each secondary is behind |
| Replication window | Time between the first and last oplog entries; your recovery headroom |
| Resize | Change the size with replSetResizeOplog |
Key Takeaways
The oplog is capped: old entries are overwritten as new ones arrive. If a secondary falls behind longer than the replication window covered by the oplog, it can no longer catch up and needs a full resync.
Frequently Asked Questions
What is the oplog in MongoDB?
A capped collection named local.oplog.rs that records data-changing operations so secondaries can replay them.
Where is the oplog stored?
In the local database on each replica set member.
What happens when the oplog is full?
Because it is capped, the oldest entries are overwritten by new ones.
What is the replication window?
The time span between the oldest and newest oplog entries. A secondary offline longer than this cannot catch up from the oplog.
Can I change the oplog size?
Yes. Use the replSetResizeOplog command on each member to increase or decrease it.
Does a standalone MongoDB have an oplog?
No. Only replica set members maintain an oplog.
Next: MongoDB Replica Set Elections
Continue the MongoDB learning path.
CODETOFUN NEWS
Stay Ahead in Tech
Get the latest IT news, programming updates, and technology trends directly on your phone.
Your daily dose of tech, straight to your feed.
8 people found this page helpful
