MongoDB index build on primary causes secondaries to fall behind as oplog wraps
Environment
MongoDB 4.4 replica set (3 nodes) on Linux VMs with WiredTiger, collection with ~500M documents, primary handles write traffic during nightly batch imports while a createIndex is running on a large field.
Symptoms
During the index build on the primary, secondaries' replication lag grows from seconds to hours; rs.status() on secondaries shows replication lag increasing and apply delay. Secondary eventually reports RECOVERING or fails to catch up. Primary's CPU and disk IO are high; clients experience increased write latency.
Observations
rs.printReplicationInfo() reports oplog window smaller than duration of index build-related operations; the index build generates a large number of write operations that are replicated. Attempts to restart secondary do not help — it must perform a resync if it falls too far behind. The index build was initiated as a foreground operation in the operational window; build progress shows many resources consumed on primary. There is no recent backup taken prior to starting the index build.
Already attempted
Tried stepping down primary to let index build run on a different node — cluster remained unstable. Attempted to increase oplog size on the fly (temporary growth via file expansion not supported without recreation), attempted to throttle writes during index build (application-level change) with limited effect. Killed the index build to stop activity, but partial index state and writes left the cluster with inconsistent performance.
Question
How can we recover secondaries without full resync and safely build the index without causing replication failure? What reversible steps avoid data loss and long downtime?
Verified community troubleshooting
No answers yet..
Incident analysis
Most likely causes
Summary
Suggested Fixes
Continue troubleshooting
Related troubles
Matched from the title, description and tags