Best Practices – Mongo Speed vs Distant WAN

Best Practices – Mongo Speed vs Distant WAN

Yes — MongoDB replica sets across WANs can significantly impact performance, especially for read and write operations that wait on acknowledgments or are sensitive to replication lag. Here’s how to speed up your MongoDB replica set:


Best Practices:

1. Yes, remove distant secondaries if:

  • They have high replication lag
  • You don’t need them for HA or backups
  • They’re hurting write latency (e.g., w:majority slows down if they lag)

2. Use readPreference: nearest to direct reads to the lowest-latency replica:

db.getMongo().setReadPref("nearest");

Also possible in connection strings:

mongodb://host1,host2,host3/db?readPreference=nearest

3. Use tagSets to define locality and prioritize nearby replicas:

In rs.conf():

members: [
  { _id: 0, host: "ny1:27017", tags: { "dc": "ny", "rack": "1" } },
  { _id: 1, host: "sf1:27017", tags: { "dc": "sf", "rack": "2" } },
  ...
]

Then in client:

readPreference: {
  mode: "nearest",
  tagSets: [{ "dc": "ny" }]
}

4. Avoid w:majority writes when WAN secondaries are slow:

Use:

writeConcern: { w: 1, j: true }

This ensures journaling but avoids cross-datacenter quorum waits.

5. Increase heartbeatTimeoutSecs and electionTimeoutMillis in WAN environments:

rs.conf().settings = {
  electionTimeoutMillis: 10000,
  heartbeatTimeoutSecs: 10
}

🔍 DETAILED RECOMMENDATIONS

Remove or downgrade WAN secondaries

  • Consider using priority: 0, votes: 0 to exclude distant members from elections or write quorum:
rs.add({ host: "wanhost:27017", priority: 0, votes: 0 });

Enable compression over WAN:

In MongoDB 3.4+:

net:
  compression:
    compressors: zlib,snappy

Use hidden secondaries for backups in WAN:

rs.add({ host: "backup-remote:27017", hidden: true, priority: 0 });

Optimize oplog window:

Ensure secondaries have enough disk and processing speed to avoid falling behind.


Testing Performance

Check replication lag:

rs.printSlaveReplicationInfo()

Monitor read latency:

Run queries with .explain("executionStats") and check the executionTimeMillis.


SUMMARY

StrategyImprovesNotes
Remove WAN secondariesWrite speed, availabilityAvoids slow replication drag
readPreference: nearestRead speedHits local or low-latency node
tagSetsRead speed, controlTarget specific data centers
Reduce writeConcernWrite speedAvoid w:majority on WAN
Use hidden, non-voting WANsStabilityAvoids slow elections
Increase heartbeat/election timeoutsStabilityPrevents false failovers

Leave a Comment