QA
Intermediate LevelMongoDB & Mongoose

How do MongoDB Replica Sets and the Oplog enable High Availability?

❓ Interview Prompt: Explain Replica Set architecture (Primary node, Secondary nodes, Arbiter), Oplog (operations log), asynchronous replication, and automatic failover.

A Replica Set in MongoDB is a group of 'mongod' processes that maintain the same data set, providing high availability and disaster recovery:

Components:

•
Primary Node: Receives ALL write operations. Writes state changes to a special capped collection: the Oplog ('local.oplog.rs').
•
Secondary Nodes: Replicate the Primary's Oplog asynchronously and apply operations to their local datasets.
•
Automatic Failover: If the Primary fails, Secondaries elect a new Primary automatically within seconds!

Code Implementation Example

typescript
import mongoose from 'mongoose';

// Connect to MongoDB Replica Set with seed list
const replicaSetURI = 'mongodb://node1:27017,node2:27017,node3:27017/prod_db?replicaSet=rs0';

async function connectReplicaSet() {
  await mongoose.connect(replicaSetURI);
  console.log('Connected to High-Availability Replica Set!');
}
💡Connecting Node.js driver to a 3-node MongoDB Replica Set.
🎯 Interview Key Takeaway

Replica Sets provide high availability by replicating the Primary node's Oplog asynchronously to Secondary nodes.

#Replica Sets#Oplog#High Availability#Replication#Intermediate
Back to all MongoDB & Mongoose Questions→

More MongoDB & Mongoose Interview Questions

Beginner LevelMongoDB & Mongoose

How does the MongoDB Aggregation Pipeline work? Explain $match, $group, $lookup, and $project.

Beginner LevelMongoDB & Mongoose

What is MongoDB and how does the BSON document data model work?

Beginner LevelMongoDB & Mongoose

What is the structure of a MongoDB ObjectId and how is it generated?