Use the database profiler to see if there are slow queries or long-running operations that correspond to the incidences of lag. 

• Appropriate Write Concern 

If you are performing a large data ingestion or bulk load operation that requires a large number of writes to the primary, particularly with unacknowledged write concern (page 78), the secondaries will not be able to read the oplog fast enough to keep up with changes. 

To prevent this, require write acknowledgment or journaled write concern (page 78) after every 100, 1,000, or an another interval to provide an opportunity for secondaries to catch up with the primary. 

For more information see: 

– Replica Acknowledge Write Concern (page 80) 

– Replica Set Write Concern (page 84) 

– Oplog Size (page 593) 

## Test Connections Between all Members 

All members of a replica set must be able to connect to every other member of the set to support replication. Always verify connections in both “directions.” Networking topologies and firewall configurations can prevent normal and required connectivity, which can block replication. 

Consider the following example of a bidirectional test of networking: 

## Example 

Given a replica set with three members running on three separate hosts: 

• m1.example.net 

• m2.example.net 

• m3.example.net 

1. Test the connection from m1.example.net to the other hosts with the following operation set m1.example.net : 

mongo --host m2.example.net --port 27017 

mongo --host m3.example.net --port 27017 

2. Test the connection from m2.example.net to the other two hosts with the following operation set from m2.example.net , as in: 

mongo --host m1.example.net --port 27017 

mongo --host m3.example.net --port 27017 

You have now tested the connection between m2.example.net and m1.example.net in both directions. 

3. Test the connection from m3.example.net to the other two hosts with the following operation set from the m3.example.net host, as in: 

mongo --host m1.example.net --port 27017 

mongo --host m2.example.net --port 27017 

