NoSQL Databases

MongoDB Replica Set Operations: Adding Members, Failover, and Lag Diagnosis

Day-to-day MongoDB replica set operations: adding/removing members, triggering elections, diagnosing replication lag, and managing oplog size.

JusDB Team
Published February 12, 2025
5 min read

MongoDB replica sets provide automatic failover and data redundancy. This guide covers day-to-day operational tasks: adding/removing members, triggering elections, and diagnosing replication lag.

Replica Set Overview

text
PRIMARY  ──► SECONDARY1
         └─► SECONDARY2 (hidden: true for backups)

Check Replica Set Status

javascript
rs.status()
rs.printReplicationInfo()     // lag on primary
rs.printSecondaryReplicationInfo()  // lag on each secondary

Add a New Member

javascript
rs.add({ host: "mongo3:27017", priority: 1, votes: 1 })

// Add a hidden member for backups (no reads, no votes for primary)
rs.add({ host: "mongo-backup:27017", hidden: true, priority: 0 })

Remove a Member

javascript
rs.remove("mongo3:27017")

Force an Election (Step Down Primary)

javascript
// Step down primary for 60 seconds, triggering election
rs.stepDown(60)
Warning: rs.stepDown() causes a brief write outage while the new primary is elected (typically < 12 seconds). Plan for application retry logic.

Diagnosing Replication Lag

javascript
// Check oplog window on primary
db.getReplicationInfo()

// Check each member's lag
rs.status().members.forEach(m => {
  if (m.optime) print(m.name, m.optimeDate)
})

Oplog Size — Increase for High-Write Workloads

javascript
// Check current oplog size
db.printReplicationInfo()

// Resize oplog (MongoDB 4.4+)
db.adminCommand({ replSetResizeOplog: 1, size: 51200 })  // 50 GB

Read Preference for Secondaries

javascript
// Allow reading from secondaries in mongosh
db.getMongo().setReadPref('secondaryPreferred')

// Connection string
// mongodb://host1,host2,host3/mydb?replicaSet=rs0&readPreference=secondaryPreferred

Key Takeaways

  • Use rs.printSecondaryReplicationInfo() to spot lagging members immediately
  • Hidden members with priority: 0 are ideal for backup workloads
  • Increase oplog size proactively on high-write clusters to maintain recovery windows
  • Always have an odd number of voting members to prevent ties in elections

JusDB Can Help

MongoDB replica set operations require expertise to do safely in production. JusDB's NoSQL specialists can help you optimize your replica set configuration.

Share this article

JusDB Team

Official JusDB content team

Keep reading

High Performance with MongoDB: A Top-Down Tuning Guide

A top-down playbook for high-performance MongoDB: measure with the profiler and explain(), model for access patterns, index by the ESR rule, keep the working set in the WiredTiger cache, pool connections, and scale reads with secondaries and sharding — with flow diagrams for each layer.

MongoDB14 minJun 6, 2026
Read

MongoDB Explained (2026): Replica Sets, Sharding, Atlas & Production Patterns

Complete MongoDB guide covering document modeling, aggregation pipelines, sharding, replication, and Atlas deployment. Learn when MongoDB is the right choice for your application.

MongoDB5 minMay 13, 2026
Read

ScyllaDB vs Apache Cassandra: Performance and Operational Differences

Compare ScyllaDB and Apache Cassandra — throughput, latency, operational complexity, and when to switch

Cassandra11 minJan 22, 2026
Read