How to Fix MongoDB Error 138 (RemoteOplogStale)
You get mongodb error 138 when a replica set node falls too far behind the oplog. To fix this RemoteOplogStale failure, resync the stale member from a healthy node or restore a fresh backup. Replica sets rely on a shared log of write actions to stay in sync. Each secondary node reads this log from a sync source to copy new changes. When a secondary waits too long, the sync source writes over those older log entries. The database engine then stops replication on that node and reports a stale opl
Your coding agents run with your access.
Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.
You get mongodb error 138 when a replica set node falls too far behind the oplog. To fix this RemoteOplogStale failure, resync the stale member from a healthy node or restore a fresh backup.
Replica sets rely on a shared log of write actions to stay in sync. Each secondary node reads this log from a sync source to copy new changes. When a secondary waits too long, the sync source writes over those older log entries. The database engine then stops replication on that node and reports a stale oplog state.
What causes mongodb error 138 in replica sets
The official error name for code 138 is RemoteOplogStale. It means the remote sync source no longer holds the oplog entry that the local node needs next. The oplog is a capped collection that keeps a rolling history of writes. Once the oplog hits its size limit, new writes push out the oldest records.
Network drops are a common trigger for this state. If a link goes down for hours, a busy primary node keeps taking writes from your app. By the time the link comes back up, the primary has cycled through its whole oplog. Long maintenance windows or stopped secondary hosts cause the exact same gap.
Slow disk reads or high write spikes also cause a secondary to lag. If the secondary cannot apply writes as fast as the primary takes them, the gap grows. High load from heavy read jobs on a secondary can also slow down its sync rate. Finally, an undersized oplog leaves very little room for spikes in write traffic.
How to diagnose and fix RemoteOplogStale safely
Start by checking the replication lag and oplog window across your cluster nodes. You do not need full admin rights just to inspect cluster health and collection stats. MongoDB uses role-based rules that grant sets of actions down to the collection level. The built-in read role grants actions like find, listCollections, listIndexes, dbStats, and collStats on non-system collections.
Keep diagnostic users scoped to read actions so engineers do not run accidental writes during an incident. Avoid giving out roles with insert, update, remove, or dropCollection rights when someone only needs to check metrics. Once you confirm which secondary node threw the RemoteOplogStale code, you must plan a resync.
MongoDB Authentication & Authorization + End-to-End Encryption: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
A stale node cannot catch up on its own because the missing write history is gone. You have two safe ways to bring the member back into the replica set. First, you can wipe the data folder on the stale node and let it run an initial sync. The node will copy all data from a healthy member and build fresh indexes.
Second, if your data set is huge, an initial sync over the network might take too long. In that case, restore the stale node from a recent disk snapshot or backup. After the restore finishes, start the node so it can pull the remaining oplog entries. Both paths require a user or automation task with proper admin roles during the repair step.
How to prevent future oplog sync failures
After the node recovers, check why the oplog rolled over so fast. Compare your peak write volume against the time window your current oplog size holds. Sizing the oplog to hold many hours or days of writes gives your team a safer buffer. That buffer lets nodes survive routine patching or brief network cuts without going stale.
You should also set up alerts for replication lag before the lag reaches the oplog time limit. Catching a slow node early lets you fix disk bottlenecks or move heavy read traffic away. Good monitoring and scoped database roles keep your replica set healthy and safe during repairs.
Inspect MongoDB wire traffic in flight
Built-in MongoDB roles govern collection access at setup time. They cannot score command risk per call or mask sensitive document values on the fly. By design, hoop.dev sits inline in the MongoDB protocol next to your server as a sidecar for your MongoDB. On every production command from mongosh, drivers, or AI agents, hoop.dev decides whether to pass, mask, block, or hold it for human review.
First, hoop.dev guardrails block bad deletes, drops, and write pipelines or hold them for approval. Next, hoop.dev data masking rewrites sensitive values in returned documents in memory and logs each decision. Explore the repo on GitHub.
Frequently asked questions
What does RemoteOplogStale mean in MongoDB?
RemoteOplogStale is the official name for error code 138. It means a secondary node fell behind and its sync source already overwrote the needed oplog records.
Can a stale secondary node recover without a resync?
No, the node cannot recover by itself once the required oplog entries are gone. You must perform a full initial sync or restore the node from a recent backup.
Which role privileges should engineers use to check collection stats?
Give engineers a role with read actions such as dbStats, collStats, listCollections, and listIndexes. This lets them inspect database state without having insert, update, or remove privileges.
Related guides
In this post