A replica set is a group of MongoDB nodes (typically 3 or 5) that maintain the same data set. One node is elected Primary and receives all writes; the remaining nodes are Secondaries that continuously replicate from the primary. If the primary fails, an automatic election promotes a secondary to primary within 10–12 seconds.
// Minimum recommended replica set: 3 nodes (1 primary + 2 secondaries) // Replica set configuration (run on primary): rs.initiate({ _id: "myReplicaSet", members: [ { _id: 0, host: "mongo1:27017", priority: 2 }, // preferred primary { _id: 1, host: "mongo2:27017", priority: 1 }, { _id: 2, host: "mongo3:27017", priority: 1 } ] }) // Check replica set status: rs.status() // full status including lag, oplog, last heartbeat rs.conf() // current configuration rs.isMaster() // deprecated; use db.hello() in 5.0+ db.hello() // current primary, all members, connection info