What is a replica set in MongoDB?

Instruction: Describe the concept of a replica set and its role in MongoDB.

Context: This question tests the candidate's knowledge of MongoDB's replication mechanism. Candidates should explain what a replica set is, its components, and its importance in ensuring data availability and redundancy. This includes understanding how replica sets provide automatic failover and data recovery, which are crucial for maintaining high availability in MongoDB deployments.

Updated

Official answer

A replica set is a group of MongoDB processes that maintain a common dataset. The primary accepts ordinary writes and records changes in its oplog. Data-bearing secondaries asynchronously fetch and apply operations; they can lag behind the primary. Some configurations include an arbiter, which votes without holding data.

Eligible members can elect a new primary when the old primary is unavailable and election requirements are met. This improves availability but is not uninterrupted service: writes can fail or pause during detection and election. Drivers must handle topology changes, retries and ambiguous outcomes appropriately.

Write concern determines the requested acknowledgment level. Read preference selects where reads go; read concern affects the read guarantees. Reading from a secondary does not inherently provide the freshest data. Choose these settings together with the application's consistency requirements, and use supported sessions/causal consistency where needed.

Place members across appropriate failure domains, retain a voting majority during maintenance, and monitor replication lag and the oplog window. Test application behavior during failover, including writes whose acknowledgment was lost. Replicas protect against some node failures but propagate logical mistakes; maintain separate backups and verified restores.

MongoDB replication, replica-set synchronization

Related Questions