MongoDB Elections
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
| Reason | Benefit |
|---|---|
| Automatic failover | If the primary fails, the set recovers without manual intervention. |
| Prevents split brain | Requiring a majority means two primaries cannot be elected in separate network partitions. |
| Tunable behavior | Priority lets you prefer certain servers, such as ones in your main data center. |
| Predictable maintenance | rs.stepDown() allows planned changes with a controlled election. |
How To Use It
- Connect to a replica set and run rs.status() to see who is primary.
- Run rs.conf() to review members, priority, and votes.
- Simulate planned maintenance with rs.stepDown() on the primary.
- Run rs.status() again and watch another member become PRIMARY.
- Check that your application driver reconnects by using a replica set connection string.
- Enable retryable writes so brief elections do not surface as errors.
- Review member count and priorities so a majority can always form.
mongosh Examples
// 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
| Topic | Detail |
|---|---|
| Heartbeat | Members ping each other every 2 seconds |
| electionTimeoutMillis | Default 10000 ms of silence before a secondary calls for an election |
| Majority | More than half of voting members; needed to elect a primary |
| Priority | Higher-priority members are preferred; priority 0 can never become primary |
| votes | Each member has 0 or 1 vote; a set can have up to 7 voting members |
| Arbiter | Votes in elections but holds no data |
| Up to date | A 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.
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.
8 people found this page helpful
