Introducing Dragonfly Cloud! Learn More

Question: What is the minimum replica set configuration in MongoDB?

Answer

In MongoDB, a replica set is a group of mongod instances that maintain the same data set. Replica sets provide redundancy and high availability, and are the basis for all production deployments. But what is the minimum viable configuration for a MongoDB replica set?

Minimum Configuration

The minimum recommended configuration for a production replica set is three members:

  1. Primary: This node receives all write operations. The primary records all changes to its operation log (oplog).
  2. Secondary: This node replicates the primary's oplog and applies the operations to itself in an asynchronous process. A replica set can have one or more secondaries.
  3. Arbiter: In some situations, instead of adding another secondary, you might add an arbiter. Arbiters do not hold data; they participate in elections for primary but do not contribute to fault tolerance. Thus, it's common to have two data-bearing nodes and one arbiter in environments where cost or resources are limited.

However, for environments where durability and data redundancy are critical, avoid using arbiters, and opt for at least three data-bearing nodes.

Example Configuration

// Initiating a minimum replica set with one primary, one secondary, and one arbiter. rs.initiate( { _id : \"myReplicaSet\", members: [ { _id: 0, host: \"mongodb0.example.net:27017\" }, { _id: 1, host: \"mongodb1.example.net:27017\" }, { _id: 2, host: \"mongodb2.example.net:27017\", arbiterOnly: true } ] })

This code initiates a replica set with the specified members. Note the arbiterOnly: true setting for the third member, designating it as an arbiter.

Considerations

  • Data Redundancy and Availability: While three members are the minimum recommended setup, increasing the number of data-bearing nodes can improve redundancy and availability.
  • Write Concerns: Properly configure your write concerns to ensure data consistency according to your application's requirements.
  • Read Distribution: Use read preferences to distribute read traffic among the primary and secondaries, optimizing performance and resource utilization.

In summary, while the absolute minimum replica set configuration could technically consist of just a primary and a secondary, MongoDB recommends including at least one additional member (either a secondary or an arbiter) to achieve the benefits of automatic failover and election. For most production environments, opting for three data-bearing nodes provides a good balance between cost, redundancy, and availability.

Was this content helpful?

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book

Start building today 

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.