MongoDB problems

MongoDB Replication Lag: How Long Before the Secondary Falls Off the Oplog

2 December 2026

A secondary copies the primary's oplog and applies it. Lag is the time between the last write on the primary and the last write the secondary applied. Lag alone is not the danger: the danger is lag that grows until the entry the secondary needs has left the primary's oplog. From then on the member is too stale to catch up and needs a full initial sync.

How far behind, and how much room is left

// lag of every secondary
rs.printSecondaryReplicationInfo()

// the window of the oplog: the room a member has
rs.printReplicationInfo()

The room left is the sync source's oplog window minus the lag. A secondary 3 hours behind on an oplog that holds 5 hours has 2 hours, less if the lag still grows. With the lag in seconds from rs.status():

const s = rs.status();
const p = s.members.find(m => m.stateStr === "PRIMARY");
s.members.filter(m => m.stateStr === "SECONDARY")
  .map(m => ({ name: m.name, syncSource: m.syncSourceHost,
               lagSeconds: (p.optimeDate - m.optimeDate) / 1000 }))

The causes

CauseSign
A write burst: a bulk load or a large updateManyLag rises with the primary's write rate and falls after.
A secondary on slower disks or less memoryThe same secondary lags every busy hour.
An index buildLag on every secondary at the same time.
Reads sent to the secondaryLag rises with its read traffic.
The networkLag of a member in another region, while the others are close.

The fix, in order

  1. Work out the time left. Hours of room means time to find the cause; minutes means making the oplog larger now.
  2. Make the oplog larger on the sync source. db.adminCommand({ replSetResizeOplog: 1, size: 51200 }) takes effect at once, without a restart; the size is in megabytes. minRetentionHours keeps entries for a time, whatever the size.
  3. Stop the burst. Pause the bulk job, or load in batches with a majority write concern so the writer waits for the secondaries.
  4. Give the secondary the primary's hardware. A secondary that may become primary needs the same disks and memory.
  5. Resync only when it is too stale. A member that fell off the oplog stays in RECOVERING. Stop it, empty its data directory and start it again for an initial sync, or seed it from a copy.

See it coming

Radar Engine, the detection layer of dbCove, works out the time left before each secondary needs a full resync, with no agent on the server:

DetectorWhen it fires
Replication lagWatch from a minute or the member's usual lag. Warning at 5 minutes, when the lag uses half the source's oplog window, or with 24 hours or less left. Critical with 6 hours or less left, when it already fell off the oplog, or when majority writes wait on it.
Oplog window shortThe primary's oplog holds much less time than usual, so every member has less room.
Flow controlThe primary holds writes back because the majority commit point lags.

The finding names the member, its lag, its sync source, the share of the oplog window it uses and the time left before a full resync, with the date. dbCove AI explains why that member lags, from its own history.

Questions

How do I check replication lag in MongoDB?
rs.printSecondaryReplicationInfo() in mongosh prints how far each secondary is behind the primary. rs.status() gives each member's optimeDate.

What does "too stale to catch up" mean?
The oplog entry the secondary needs next is no longer in its sync source's oplog. The member cannot catch up by replication and needs an initial sync.

How do I make the MongoDB oplog larger?
Run replSetResizeOplog on each member with the size in megabytes. It needs no restart. Start with the secondaries, then the primary.

Related problems

Sources