Introducing Dragonfly Cloud! Learn More

Question: How do you configure replica set member priority 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. Each member of a replica set can have a priority setting which influences both the election for primary and secondary roles within the replica set.

The priority of a replica set member determines its eligibility to become a primary node. Higher priority values increase the likelihood of a member being elected as primary. The priority value can range from 0 to 1000, with most members typically having a default value of 1.

To change the priority of a replica set member, you must reconfigure the replica set with an updated configuration document that specifies the desired priorities.

Here's a step-by-step guideline on how to update the priority of a replica set member:

  1. Connect to the current primary member of your replica set through the mongo shell.

  2. Retrieve the current replica set configuration:

    var cfg = rs.conf()
  3. Modify the priority value for the desired member(s). In this example, changing the priority of the member with index 1 to 2:

    cfg.members[1].priority = 2
  4. Apply the new configuration:

    rs.reconfig(cfg)

Note:

  • Setting a member's priority to 0 means it will never be elected as primary, but it can still vote in elections. This is useful for read-only nodes or nodes that should not become primary under normal circumstances.
  • Care should be taken when adjusting priorities, as it may impact the stability and performance of your replica set, especially during failovers or when adding/removing nodes.
  • After updating the configuration, it might take some time for the changes to propagate and elections to occur, depending on your replica set's settings and current state.

Remember, it's crucial to understand your application's requirements and test any configuration changes in a staging environment before applying them to your production database.

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.