How does members[n].priority affect which MongoDB replica set member becomes primary?
answer
- A number from 0 to 1000, default 1
- Zero means never primary
- It is a preference, not a bypass of votes
- Raising it triggers an election on its own
- Pairs with a rule about votes zero
basics
~20 sPriority expresses a preference, not a guarantee. A member with priority 0 can never be elected and cannot call an election. A member with higher priority than the current primary calls a priority takeover once its oplog is nearly caught up.
solid answer
~50 s`members[n].priority` is a number from 0 to 1000, defaulting to 1, that biases which member the set prefers as primary. It never bypasses the vote: a candidate still needs a majority of voting members whatever its priority. Two behaviours follow from it. A member with `priority: 0` is ineligible — it replicates and, if `votes` is 1, it votes, but it can neither be elected nor call an election, which is how read-only, backup or remote-site members are pinned as secondaries. And when a secondary has *higher* priority than the sitting primary, it will call an election to take over once its oplog is within about ten seconds of the primary's, stepping the current primary down. That is priority takeover, and it is why raising a member's priority causes a failover you did not explicitly request. MongoDB also requires `priority: 0` on any member configured with `votes: 0`.
code
javascript · 5 linescfg = rs.conf();
cfg.members[0].priority = 2; // preferred primary
cfg.members[1].priority = 1; // normal candidate
cfg.members[2].priority = 0; // remote DR copy, never primary
rs.reconfig(cfg); // this triggers a takeover electiongo deeper
Recall that priority defaults to 1, ranges from 0 to 1000, and that a member set to 0 can never become primary while still replicating data.
Explain priority takeover — a higher-priority secondary calls an election once caught up — and the configuration rule that a member with zero votes must also have zero priority.
Show the operational consequence: a priority change is a failover, with a write-unavailable window and rollback exposure, and a flapping high-priority node can move the primary repeatedly.
Own the topology intent — which members are eligible at all, where you want writes to land relative to clients, and why priority steers but never guarantees placement.
## What priority is Every member of a replica set carries a `priority` in its configuration entry — `members[n].priority` in `rs.conf()`. It is a number between 0 and 1000, and it defaults to 1, so a set built from defaults has all members equally preferred. Priority does not decide elections. Votes decide elections, and a candidate always needs a strict majority of the voting members regardless of how high its priority is. What priority does is shape *who stands* and *who the set converges on* when more than one member is eligible. ## Priority zero: ineligibility Setting `priority: 0` makes a member permanently ineligible for the primary role. Such a member: - performs initial sync and tails the oplog like any secondary, - can serve reads for clients using a non-primary read preference, - still votes in elections if its `votes` is 1, - can never be elected primary, and never calls an election of its own. This is the standard way to pin a member into a supporting role. A member in a distant region kept as a disaster-recovery copy, a member reserved for backups or analytics, a member on hardware too weak to carry the write workload — all get `priority: 0` so that a random failover never lands the primary somewhere you did not intend. MongoDB also enforces the pairing in the other direction: a member with `votes: 0` **must** have `priority: 0`. A member that cannot vote must not be able to trigger or win an election either, so the configuration is rejected if you set one without the other. ## Higher priority: takeover The more surprising behaviour is at the other end. The set does not merely tolerate a high-priority member; it actively converges on it. If a secondary's priority is higher than the current primary's, that secondary will call an election to displace the sitting primary. It does not do this immediately or blindly — it waits until its own oplog is close enough to the primary's, within roughly ten seconds, so that taking over does not throw away a large amount of recent history. Once that condition holds, it stands for election, and if it wins the majority the current primary steps down. This is called **priority takeover**, and it has two important consequences in practice: 1. **Raising a member's priority causes a failover.** An `rs.reconfig()` that bumps one member above the others is not a passive preference change; within seconds it produces an election, a brief write-unavailable window, and possibly a rollback of writes the old primary had not replicated to a majority. Treat a priority change as a scheduled operation, not a config tidy-up. 2. **The primary comes back to the preferred node.** After a failover to a lower-priority member, the set will move the primary role back to the high-priority member once that member is healthy and caught up. That is often exactly what you want — keeping the primary near the bulk of the clients or on the beefiest hardware — but it means one bad node can cause repeated flapping if it keeps failing and recovering. ## Priority is a preference, never a guarantee A high-priority member that is unreachable by a majority of voters wins nothing. A high-priority member whose oplog is far behind will not stand until it catches up. And if the high-priority member is down, the set elects the best eligible member available — priority does not make the set wait. The corollary is that priority cannot be used as a correctness mechanism. It cannot pin writes to a region, it cannot guarantee that a particular node holds the primary role at any given moment, and it must not be relied on for anything more than steering. ## Priority versus votes These two fields are frequently conflated. They are orthogonal: - `votes` (0 or 1, at most 7 voting members in a set) determines whether the member counts toward the majority arithmetic. - `priority` (0–1000) determines whether the member may become primary and how strongly the set prefers it. A member can vote but be ineligible (`votes: 1, priority: 0`) — a common configuration for a tie-breaking data-bearing node in a third location. The reverse, being eligible but non-voting, is not allowed. ## Designing with priority A typical layout gives the members in the main application region the default priority or slightly above, gives a remote disaster-recovery member `priority: 0` so a network event never moves writes across an ocean by accident, and leaves the rest at the default so any of them can take over. Distinct priorities among the main-region members express a preference order for which machine you would rather run writes on; identical priorities say you genuinely do not care and let the fastest responder win. When you do need to move the primary deliberately, `rs.stepDown()` on the current primary is the controlled tool — it gives secondaries a catch-up window and hands over cleanly — rather than shuffling priorities and waiting for a takeover to fire.
- You raise one member's priority with rs.reconfig(). What happens next?A priority takeover. Once that member's oplog is close enough to the primary's — within roughly ten seconds — it calls an election, and if it wins the majority the sitting primary steps down. So the reconfig produces a real failover: a short write-unavailable window, dropped connections, and possible rollback of writes the old primary had not replicated to a majority. Schedule it like any other failover.
- Can a member with priority 0 vote in an election?Yes, provided its `votes` is 1. Priority and votes are independent in that direction: a `priority: 0, votes: 1` member counts toward the majority and helps a candidate win, but can never be elected itself and never calls an election. The reverse is forbidden — MongoDB requires `priority: 0` whenever `votes` is 0.
- Why prefer rs.stepDown() over priority juggling to move the primary?`rs.stepDown()` is an explicit, bounded operation: the primary gives secondaries a catch-up period, then steps down for a defined interval so it is not immediately re-elected. Priority changes achieve the move indirectly, take effect whenever the takeover conditions happen to be met, and leave the set permanently biased afterwards — so the next recovery flaps the primary back again.
saying these in an interview costs you the question
- Says the highest-priority member is elected without needing votes
- Thinks priority 0 members cannot vote
- Believes a priority change takes effect passively without a failover
- Treats priority as a guarantee of where writes land
- Sets votes 0 while leaving priority at 1