beginnerMongoDB — Indexing, Replica Sets & Sharding
What is a MongoDB replica set and how does automatic failover work?
A replica set is a group of mongod instances that maintain the same dataset. Typically: 1 primary + 2 secondaries. The primary accepts all writes. Secondaries replicate via the oplog (operation log) and can serve reads. Failover: if the primary becomes unreachable (heartbeat timeout ~10s), the remaining members hold an election. Each eligible secondary votes; the secondary with the most up-to-date
Ready to master this question?
Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.
Sign in to generate a response