Learn · MongoDB operations

MongoDB replica sets in production: elections, failover and what breaks

A replica set keeps copies of your data on several servers and elects a new primary when one fails. This guide covers how many members you need, how an election and failover behave, what happens to writes that were not replicated, and what to watch.

45 minutes with an engineer: we review your workload and sketch the design.

  • Docs read 4 Dec 2026
  • OSStend runs MongoDB replica sets
How do you patch or upgrade a replica set without downtime?

A MongoDB replica set is a group of mongod processes that hold the same data: one primary takes writes and the others copy its oplog. Three data-bearing members is the recommended minimum. If the primary is unreachable for 10 seconds, a secondary calls an election, and MongoDB says the median time to elect a new primary typically does not exceed 12 seconds. Docs read 4 December 2026.

Customers

  • Microsoft logo
  • Henry Schein logo
  • Enbridge logo
  • Gojek logo

Partners

  • Broadcom logo
  • Redis logo
  • VMware Tanzu logo
  • OSSeva logo

What is a MongoDB replica set, and what are its members?

A replica set is a group of mongod instances that maintain the same data set. Several data-bearing members hold copies, and one and only one is the primary. Secondaries copy the primary's oplog asynchronously. An arbiter can vote in elections but holds no data. Members can also be hidden, delayed or given priority 0.

MemberHolds dataRole
PrimaryYesReceives all writes and records them in the oplog
SecondaryYesCopies the oplog; can be elected; can serve reads if the client asks
ArbiterNoOne vote in elections; cannot become primary
Hidden or delayed secondaryYesKept out of application reads, or kept behind on purpose, for reporting, backup or recovery
Priority 0 memberYesNever becomes primary; for a second data center or a cold standby

Source: the manual pages on replication and replica set members, docs read 4 Dec 2026.

How many members should a replica set have?

Three data-bearing members: one primary and two secondaries. MongoDB calls this the recommended minimum and says to use an odd number of voting members. A set can have up to 50 members but only 7 voting. Fault tolerance is members minus the majority needed, so four members tolerate one failure, the same as three.

Illustrative shape: real designs start from your metrics.
Voting membersMajority needed to electMembers you can lose
321
431
532
642

Source: replica set deployment architectures, docs read 4 Dec 2026. Each member should sit on its own machine; outside a rolling upgrade, all members should run the same major version.

Spread members so that the loss of one data center still leaves a majority, and use an odd number of data centers if you can (architectures). A primary-secondary-arbiter set saves a data-bearing member but gives one full copy of the data, and MongoDB says it trades redundancy and fault tolerance for lower cost (three-member architectures).

45 minutes with an engineer: we review your workload and sketch the design, then price it.

How does a MongoDB election work, and how long does failover take?

Members send heartbeats every two seconds. If the primary is silent for the electionTimeoutMillis period, 10 seconds by default, an eligible secondary calls an election and needs a majority of votes. MongoDB says the median time to a new primary typically does not exceed 12 seconds. No writes succeed until it completes.

Reads can continue.
The set keeps serving reads configured to run on secondaries while the election runs; writes wait (elections, docs read 4 Dec 2026).
Who wins.
A member cannot become primary unless it has the highest optime of any visible member. Higher priority secondaries call elections sooner and are more likely to win.
Partitions.
A primary that sees only a minority of voting members steps down; the partition holding a majority elects a new primary.
Drivers retry.
Compatible drivers enable retryable writes by default and retry certain writes once, so applications still need tolerance for the election window.
Tuning has a price.
A lower electionTimeoutMillis detects failure faster but can cause more elections during network blips, and more rollbacks of w:1 writes.

What happens to writes during failover, and what is a rollback?

A write that only the old primary had acknowledged can be rolled back when that member rejoins. MongoDB's default write concern is majority, which protects acknowledged writes. With w:1, or on a primary-secondary-arbiter set where the default falls back to w:1, acknowledged writes can be lost to a rollback.

SettingResult in a failoverSource
w: majority, the implicit defaultA majority of data-bearing voting members have the write in their oplog before it is acknowledgedwrite concern
w: 1Acknowledged by the primary alone; if it fails before secondaries copy it, the write is rolled backrollbacks
Primary, secondary and arbiter setThe default becomes w: 1 when the non-arbiter members do not exceed the voting majoritywrite concern
Rolled-back dataWritten to BSON files under dbPath/rollback; the time limit defaults to 24 hours (rollbackTimeLimitSecs)rollbacks

Sources linked in the table, docs read 4 Dec 2026. The docs say rollbacks should be rare, are often the result of a network partition, and grow when secondaries cannot keep up.

Why do secondaries lag, and how big should the oplog be?

Lag comes from network latency, slow disks on the secondary, long operations on the primary and bulk loads with weak write concern. The oplog's size sets how long a secondary can be away: the default is 5% of free disk, between 990 MB and 50 GB, and a member that falls outside the oplog window cannot catch up by replication.

Check it.
rs.printSecondaryReplicationInfo() shows how far each secondary is behind, and rs.printReplicationInfo() shows oplog size and time range (troubleshooting, docs read 4 Dec 2026).
Flow control is on by default.
When majority-committed lag nears flowControlTargetLagSeconds, the primary throttles writes; lag can still occur without load, for example from an unresponsive secondary.
Resize live.
replSetResizeOplog changes the size without a restart, and oplogMinRetentionHours sets a minimum retention in hours (oplog).
Bulk loads.
The docs advise asking for write acknowledgment every few hundred or thousand writes during a bulk load so secondaries can catch up.

From our on-call engineers: what we check on a new replica set

We turn the manual's deployment notes into a checklist before go-live, because the failures that cost teams most are configuration, not code. Each item below restates MongoDB documentation, not a measurement of any customer system.

  • Each member is on its own machine, and members use DNS hostnames, not IP addresses; from 5.0, nodes configured only by IP fail startup validation under split horizon DNS (deploy, docs read 4 Dec 2026).
  • At most one arbiter, and never on a host that also runs the primary or a secondary; the docs warn that multiple arbiters can elect a node that fell behind and lose writes (arbiters).
  • TLS is on, because arbiter votes, heartbeats and configuration exchanges are not encrypted without it.
  • The oplog is sized from the write rate and the longest outage we expect a member to ride out.
  • A failover is rehearsed before go-live, and the application's reconnect and retry behaviour is tested against it.

How do you patch or upgrade a replica set without downtime?

Work through the secondaries one at a time, then step the primary down. MongoDB's maintenance procedure restarts each member as a standalone for the task, then rejoins it, ending with the primary. rs.stepDown() waits up to 10 seconds by default for an electable secondary to catch up, then an election picks the next primary.

  1. One secondary at a time

    Shut it down, do the task, restart it into the set and wait for it to catch up before touching the next.

  2. Step down the primary

    Run rs.stepDown(). It steps down only if an electable secondary is up to date, which avoids rollbacks (rs.stepDown).

  3. Maintain the old primary

    Once it is a secondary, treat it as the others. Writes pause only for the election.

Procedure from the manual's maintenance tutorial, docs read 4 Dec 2026. Upgrades between major versions and end-of-life dates are in the MongoDB upgrade path.

When is a replica set enough, and when do you need sharding?

A replica set is enough when one server's storage and write rate hold your data. It gives availability and read copies, not more write capacity. Sharding adds horizontal scale and, in Community, is set up and run by you. OSStend runs both, with named engineers on call 24/7, in its cloud, your account or your data center.

Contact us if

  • Your data and writes fit one primary, and you need failover and a tested restore.
  • You want a design that is written down and a failover rehearsed before go-live.
  • You run in a data center, Kubernetes or OpenShift, or an air-gapped site.
Talk to an engineer

Not a fit if

How OSStend runs MongoDB is on managed MongoDB; where it can run is under Kubernetes deployments. Pricing is quoted per design: pricing. For the comparable PostgreSQL topic, see the PostgreSQL high availability guide.

Questions about MongoDB replica sets

What is a MongoDB replica set?

A group of mongod instances that maintain the same data set, with one primary taking writes and secondaries copying the oplog (replication, docs read 4 Dec 2026).

How many nodes does a MongoDB replica set need?

Three data-bearing members is the recommended minimum, with an odd number of voting members. Up to 50 members are allowed, of which 7 may vote.

How long does MongoDB failover take?

MongoDB says the median time to elect a new primary typically does not exceed 12 seconds with default settings, including the 10-second detection timeout. Your own number comes from a rehearsal on your cluster.

Can a replica set lose data on failover?

Writes acknowledged with w:1 can be rolled back if the primary fails before secondaries copy them. The default write concern is majority, except on a primary-secondary-arbiter set, where it falls back to w:1.

Do I need an arbiter?

Only when cost stops you adding a data-bearing member. An arbiter holds no data, so a primary-secondary-arbiter set keeps one full copy; the docs recommend at most one arbiter.

Is a replica set the same as a sharded cluster?

No. A replica set copies the same data to several members for availability. A sharded cluster splits data across shards, and each shard is itself a replica set. See the sharded cluster design page.

Talk to an engineer

Take your replica set design to an engineer

45 minutes: your member layout, write concern and failover plan are reviewed against the MongoDB manual, and what would be rehearsed before go-live is written down.

  • A design sketch for your workload, not a plan menu
  • A zero-downtime cutover path for what you run today
  • A price for that design, after the review
See pricing →

Your request goes to the OSStend team. No newsletter sign-up.

Get a quoteTalk to an engineer
2Engines Optional
3Where should it run?
4What do you need?

Your request goes to the OSStend team. No newsletter sign-up.