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
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
Partners
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.
| Member | Holds data | Role |
|---|---|---|
| Primary | Yes | Receives all writes and records them in the oplog |
| Secondary | Yes | Copies the oplog; can be elected; can serve reads if the client asks |
| Arbiter | No | One vote in elections; cannot become primary |
| Hidden or delayed secondary | Yes | Kept out of application reads, or kept behind on purpose, for reporting, backup or recovery |
| Priority 0 member | Yes | Never 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.
| Voting members | Majority needed to elect | Members you can lose |
|---|---|---|
| 3 | 2 | 1 |
| 4 | 3 | 1 |
| 5 | 3 | 2 |
| 6 | 4 | 2 |
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).
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.
| Setting | Result in a failover | Source |
|---|---|---|
| w: majority, the implicit default | A majority of data-bearing voting members have the write in their oplog before it is acknowledged | write concern |
| w: 1 | Acknowledged by the primary alone; if it fails before secondaries copy it, the write is rolled back | rollbacks |
| Primary, secondary and arbiter set | The default becomes w: 1 when the non-arbiter members do not exceed the voting majority | write concern |
| Rolled-back data | Written 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.
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.
Step down the primary
Run rs.stepDown(). It steps down only if an electable secondary is up to date, which avoids rollbacks (rs.stepDown).
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.
Not a fit if
- ✕You need more write throughput than a single primary: see MongoDB sharded cluster design.
- ✕You want MongoDB to run it for you in its own cloud: see Atlas pricing.
- ✕Nobody will be on call to answer a failover.
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.
Related pages
- Learn · MongoDB lifecycleMongoDB upgrade path: 5.0 to 8.0, with EOL datesMongoDB upgrade path from 5.0: versions to step through, FCV steps, key changes and end-of-life dates from MongoDB's lifecycle page. Checked 4 Dec 2026.
- Use caseDesign a sharded MongoDB cluster for IoTShard key choice, shard count and process count for IoT telemetry on MongoDB, with the 14-process minimum and what turnkey sharding leaves out.
- ComparisonMongoDB Atlas pricing explained (2026)MongoDB Atlas pricing from MongoDB's own page: Flex and M10 to M80 tiers, backup, transfer and uplifts, plus a self-run cost comparison. Checked 4 Dec 2026.
- MongoDBMongoDB hosting: managed replica sets, shardingMongoDB hosting run 24/7 by OSStend engineers: replica sets and sharded clusters with HA, backups, point-in-time recovery, monitoring and upgrades.
- Deployment · Your data centerPostgreSQL on Kubernetes, managed 24/7Run PostgreSQL on Kubernetes and Kafka on your own cluster, co-managed by OSStend through an operator with supported charts and images, 24/7.
- PricingDatabase as a service pricing: dedicated, tailoredDatabase as a service pricing from OSStend: dedicated, tailored clusters priced by engine, size, SLA tier and location. Enterprise only, with no free tier.
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





