MongoDB Oplog

Intermediate
7 min read
Updated: Nov 2026
Getting Started

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

ReasonBenefit
Powers replicationWithout the oplog, secondaries would have no way to copy the primary's changes.
Enables catch-upA secondary that was offline briefly can resume from where it stopped.
Supports change streamsChange streams are built on the oplog data.
Useful for diagnosisThe oplog size and window tell you how much downtime a member can tolerate.

How To Use It

  1. Connect to a replica set member with mongosh.
  2. Run rs.printReplicationInfo() to see oplog size, used size, and the time window.
  3. Run rs.printSecondaryReplicationInfo() to see replication lag on secondaries.
  4. Switch to the local database with use local.
  5. Look at the latest entry: db.oplog.rs.find().sort({ $natural: -1 }).limit(1).
  6. Insert a document on the primary and find the matching entry with op: "i".
  7. If the window is short for your write volume, plan a larger oplog size.

mongosh Examples

mongosh
// 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

TopicDetail
local.oplog.rsThe oplog collection on replica set members
CappedFixed size; oldest entries are overwritten when it is full
Idempotent entriesApplying the same entry multiple times gives the same result
tsTimestamp of the operation, used to track replication progress
opOperation type: i insert, u update, d delete, c command, n no-op
nsNamespace (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 windowTime between the first and last oplog entries; your recovery headroom
ResizeChange 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.

MongoDB Replica Set Elections →

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.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful