Introduction

MongoDB replica set member not syncing when initial sync fails or oplog exhausted. This guide provides step-by-step diagnosis and resolution.

Symptoms

Typical error output:

bash
Error: initial sync failed
Oplog exhausted on source node, cannot continue sync
Replica set member stuck in STARTUP2 state

Common Causes

  1. 1.Initial sync interrupted by network error
  2. 2.Oplog on source exhausted before sync complete
  3. 3.Disk I/O insufficient for sync rate
  4. 4.replica set configuration prevents syncing

Step-by-Step Fix

Step 1: Check Current State

sql
rs.status()
db.adminCommand({replSetGetStatus: 1})
rs.syncFrom("primary:27017")

Step 2: Identify Root Cause

sql
-- Check for blocking processes
SELECT * FROM pg_stat_activity WHERE state != 'idle';
SELECT * FROM information_schema.processlist WHERE time > 30;

Step 3: Apply Primary Fix

```sql // Resync from scratch db.adminCommand({resync: 1})

// Or restart member to trigger initial sync rs.stepDown() // Then restart mongod with clean data directory ```

Step 4: Apply Alternative Fix

```sql -- Alternative fix: Check configuration SELECT * FROM pg_settings WHERE name LIKE '%vacuum%';

-- Adjust parameters dynamically ALTER SYSTEM SET autovacuum_vacuum_cost_delay = 10; SELECT pg_reload_conf();

-- Verify the fix SELECT * FROM pg_stat_user_tables WHERE relname = 'target_table'; ```

Step 5: Verify the Fix

sql
rs.status()
db.adminCommand({replSetGetStatus: 1})
-- Check optime of all members

Common Pitfalls

  • Running vacuum during peak hours without resource management
  • Forgetting to analyze after vacuum for statistics update
  • Not monitoring autovacuum progress on large tables
  • Setting cost delay too high for high-churn tables

Best Practices

  • Schedule maintenance windows for vacuum full operations
  • Monitor bloat ratio and autovacuum frequency
  • Tune autovacuum parameters per table based on churn rate
  • Use pg_stat_progress_vacuum to monitor vacuum progress
  • PostgreSQL Autovacuum Not Running
  • PostgreSQL Dead Tuple Accumulation
  • PostgreSQL Transaction ID Wraparound
  • PostgreSQL Table Size Excessive