Troubleshoot and resolve a MongoDB replication lag issue

Instruction: Describe the steps you would take to identify the cause of a replication lag in a MongoDB replica set and outline how you would address each potential issue. Include considerations for network issues, hardware limitations, and configuration errors.

Context: This question tests the candidate's ability to diagnose and resolve complex issues within MongoDB's replication mechanism. It challenges them to demonstrate their troubleshooting skills, understanding of MongoDB's architecture, and their ability to ensure high availability and data consistency across distributed systems.

Updated

Official answer

Measure replication lag and its trend before changing settings. Secondaries fetch oplog entries from a sync source and apply them asynchronously. Separate inability to fetch promptly from inability to apply the fetched work; network problems, disk/CPU pressure, workload spikes and competing activity can have different remedies.

Compare primary/secondary operation timestamps, resource saturation, logs and sync-source health. Look for long-running operations, expensive writes, insufficient I/O capacity or a secondary serving too much read traffic. Check whether all secondaries are affected or only one failure domain.

The oplog window is retention history available for catching up. Enlarging it may protect a slow or disconnected member from falling outside that history; it does not make operation application faster or eliminate fetching. A member that has fallen beyond available history may need a planned resynchronization.

Address the measured bottleneck, reduce avoidable workload and ensure capacity for bursts. Schedule maintenance with voting-majority and backup requirements in mind. Do not hide lag by weakening required read/write guarantees or declaring stale secondary reads current.

Set alerts against the application's freshness and recovery objectives, and verify that lag recovers under representative load after remediation. Replica-set failover and restore procedures should be tested separately.

MongoDB synchronization, replication-lag guidance

Related Questions