Lab 15: MongoDB Replica Sets

Time: 45 minutes | Level: Advanced | DB: MongoDB 7

Overview

MongoDB Replica Sets provide automatic failover, data redundancy, and read scaling. A replica set consists of one primary and multiple secondaries, coordinated via a Raft-like election protocol.


Step 1: Launch Three MongoDB Nodes

docker network create mongo-repl-net

# Start 3 MongoDB nodes with replica set enabled
for i in 1 2 3; do
  docker run -d \
    --name mongo-node${i} \
    --network mongo-repl-net \
    --hostname mongo-node${i} \
    mongo:7 \
    --replSet "rs0" \
    --bind_ip_all
  echo "Started mongo-node${i}"
done

# Wait for all to be ready
for i in 1 2 3; do
  for j in $(seq 1 30); do
    docker exec mongo-node${i} mongosh --quiet --eval "db.runCommand({ping:1})" 2>/dev/null | grep -q "ok.*1" && break || sleep 2
  done
  echo "mongo-node${i} ready"
done

📸 Verified Output:


Step 2: Initialize the Replica Set

📸 Verified Output:


Step 3: rs.conf() and rs.status() Deep Dive

📸 Verified Output:


Step 4: Write and Read Operations

📸 Verified Output:


Step 5: writeConcern and readPreference

📸 Verified Output:

💡 w: 'majority' is the recommended writeConcern for durability — writes are acknowledged only after a majority of nodes persist them. This prevents data loss on primary failure.


Step 6: Oplog — Operations Log

📸 Verified Output:


Step 7: Special Member Types — Hidden and Delayed

📸 Verified Output:

💡 A delayed member with 1-hour delay gives you a 1-hour window to recover from accidental drops/deletes by simply copying data from the delayed secondary before it applies the destructive operation.


Step 8: Capstone — Automatic Failover

📸 Verified Output:


Summary

Component
Command
Purpose

Initialize

rs.initiate({...})

Bootstrap new replica set

Add member

rs.add('host:port')

Add secondary to running set

Status

rs.status()

Member states, health, lag

Configuration

rs.conf()

Member settings (priority, hidden, delay)

Reconfig

rs.reconfig(cfg)

Apply configuration changes

writeConcern

{w: 'majority'}

Wait for majority acknowledgment

readPreference

secondaryPreferred

Route reads to secondaries

Oplog

local.oplog.rs

Replication operations log

printReplicationInfo

rs.printReplicationInfo()

Oplog stats and replication lag

Key Takeaways

  • Minimum 3 nodes for fault tolerance — 2 nodes can't form majority after 1 fails

  • w: 'majority' writeConcern prevents data loss on primary failure

  • priority=0 + hidden=true = analytics/backup member (no app traffic)

  • Delayed members provide a time window to recover from accidental operations

  • Automatic failover takes ~10-30 seconds — applications must handle temporary errors

Last updated