MongoDB Elections

Intermediate
8 min read
Updated: Nov 2026
Getting Started

What Is Elections?

A replica set has one primary at a time. When the primary becomes unreachable, or steps down, the remaining members hold an election to choose a new one. A candidate must win votes from a majority of voting members. Members exchange heartbeats every two seconds, and a secondary that does not hear from the primary within the election timeout (10 seconds by default) calls an election.

Why It Matters

ReasonBenefit
Automatic failoverIf the primary fails, the set recovers without manual intervention.
Prevents split brainRequiring a majority means two primaries cannot be elected in separate network partitions.
Tunable behaviorPriority lets you prefer certain servers, such as ones in your main data center.
Predictable maintenancers.stepDown() allows planned changes with a controlled election.

How To Use It

  1. Connect to a replica set and run rs.status() to see who is primary.
  2. Run rs.conf() to review members, priority, and votes.
  3. Simulate planned maintenance with rs.stepDown() on the primary.
  4. Run rs.status() again and watch another member become PRIMARY.
  5. Check that your application driver reconnects by using a replica set connection string.
  6. Enable retryable writes so brief elections do not surface as errors.
  7. Review member count and priorities so a majority can always form.

mongosh Examples

mongosh
// Current roles
rs.status().members.map(m => ({ name: m.name, state: m.stateStr }))

// Look at priorities and votes
rs.conf().members.map(m => ({ host: m.host, priority: m.priority, votes: m.votes }))

// Planned failover: primary steps down for 60 seconds
rs.stepDown(60)

// Prefer a specific member as primary
cfg = rs.conf()
cfg.members[0].priority = 2
rs.reconfig(cfg)

// Connection string with retryable writes
// mongodb://h1:27017,h2:27017,h3:27017/shop?replicaSet=rs0&retryWrites=true

Edge Cases & Pitfalls

Two data-bearing members is fragile

With two voting members, losing one leaves no majority. Add a third member.

Writes pause during an election

There is no primary for a few seconds. Use retryable writes and driver retry logic.

Stale candidates lose

A member whose oplog is behind may not win votes, even with high priority.

Arbiters reduce durability

They vote but store no data, which can complicate majority write concern. Prefer a data-bearing member.

Network partitions

Only the side holding a majority can elect a primary. The minority side steps down to secondary.

Cheat Sheet

TopicDetail
HeartbeatMembers ping each other every 2 seconds
electionTimeoutMillisDefault 10000 ms of silence before a secondary calls for an election
MajorityMore than half of voting members; needed to elect a primary
PriorityHigher-priority members are preferred; priority 0 can never become primary
votesEach member has 0 or 1 vote; a set can have up to 7 voting members
ArbiterVotes in elections but holds no data
Up to dateA candidate must have an oplog at least as current as the voters it asks
rs.stepDown()Ask the primary to step down and trigger an election
rs.status()Show each member's state, such as PRIMARY or SECONDARY
rs.conf()Show configuration including priorities and votes

Key Takeaways

A replica set needs a majority of voting members to elect a primary. Use an odd number of voting members (three is the common minimum) so a single failure never blocks elections.

Frequently Asked Questions

What is a replica set election?

A voting process in which members choose a new primary when the current primary is unavailable or steps down.

How long does an election take?

Detecting failure takes up to the election timeout (10 seconds by default), and the election itself is usually very quick after that.

How many votes are needed?

A majority of voting members. In a three-member set, two votes are needed.

Why does a replica set need an odd number of members?

An odd count avoids ties and makes the majority clear. An even count tolerates no more failures than one fewer member.

Can I control which member becomes primary?

You can set priority values. A higher-priority member is preferred, and priority 0 members can never become primary.

What does rs.stepDown do?

It tells the primary to become a secondary for a period, triggering an election for a new primary.

Next: MongoDB Backup

Continue the MongoDB learning path.

MongoDB Backup →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful