What is Replication in MongoDB?
Replication in MongoDB means running a replica set — multiple mongod instances holding the same data. One primary handles all writes; secondaries replicate the oplog asynchronously and serve reads if desired.
Primary & secondaries — typically 3+ nodes; majority needed for elections.
Automatic failover — if the primary dies, a secondary is elected within seconds.
oplog — capped collection of write operations; secondaries tail it to stay in sync.
Read preference — route reads to primary, secondary, or nearest based on app needs.
Arbiters — vote-only nodes; cheap way to break election ties.
Required for production — single-node deployments have no HA.
Know the structure: Primary (writes) + Secondaries (replicate, optional reads) + Arbiter (voting only). Automatic failover: ~10-12 seconds. oplog is the replication mechanism (capped collection of write operations).
Minimum 3 members for automatic failover (or 2 + arbiter). Read preference controls where reads go.