🌐 Replication & Sharding in MongoDB

As applications grow, a single MongoDB server eventually can't keep up β€” either it becomes a single point of failure, or it simply runs out of capacity to store and serve data. MongoDB solves these two problems with two distinct mechanisms: replication for high availability, and sharding for horizontal scale. This tutorial covers both in depth, along with backup and disaster recovery strategies.

Information

Examples use the official MongoDB Node.js Driver and the mongosh shell for administrative commands.

πŸ“– 1. Introduction

Replication protects against data loss and downtime by keeping multiple copies of data in sync. Sharding distributes data across multiple servers to handle datasets and workloads too large for a single machine. These techniques are complementary β€” a production sharded cluster is typically built from multiple replica sets.

Scaling MongoDB
Replication
Sharding
High availability
Automatic failover
Horizontal scale
Distributed data

πŸ›‘οΈ 2. High Availability

High availability means the database stays accessible even when individual servers fail. MongoDB achieves this through replica sets, which maintain multiple synchronized copies of the same data.

Tip

A production MongoDB deployment should never run as a single standalone node β€” always use at least a three-member replica set.

πŸ‘₯ 3. Replica Sets

A replica set is a group of mongod instances that maintain the same data set. One member acts as the primary, accepting all writes, while the others act as secondaries, replicating data from the primary.

Initializing a three-member replica set

// Initiating a replica set from the shell
rs.initiate({
  _id: "rs0",
  members: [
    { _id: 0, host: "mongo1:27017" },
    { _id: 1, host: "mongo2:27017" },
    { _id: 2, host: "mongo3:27017" }
  ]
});

πŸ‘‘ 4. Primary Node

The primary is the only member that accepts write operations. All changes are recorded in its oplog and streamed to secondaries for replication.

Note

A replica set has exactly one primary at any given time β€” if it becomes unreachable, the remaining members elect a new one.

πŸͺž 5. Secondary Nodes

Secondaries continuously replicate the primary's data by applying operations from its oplog. They can optionally serve read operations, depending on the configured read preference.

Directing a read to a secondary

// Reading from a secondary
const results = await collection
  .find({ status: "completed" })
  .readPreference("secondary")
  .toArray();

βš–οΈ 6. Arbiter

An arbiter is a lightweight replica set member that participates in elections but holds no data, used to maintain an odd number of voting members without the cost of a full data-bearing node.

Caution

Arbiters are best used sparingly β€” in most production deployments, a third full data-bearing node is preferred for better fault tolerance.

πŸ—³οΈ 7. Elections

When the primary becomes unreachable, the replica set holds an election among the remaining members to choose a new primary, typically completing in a few seconds.

Election Trigger
Primary unreachable
Secondaries call for election
Majority vote
New primary elected

Important

An election requires a majority of voting members to be reachable β€” this is why replica sets should always have an odd number of voting members.

πŸ”„ 8. Automatic Failover

When a primary fails, MongoDB automatically promotes a secondary to primary through an election, allowing the application to resume writes with minimal manual intervention.

Tip

Combine automatic failover with retryWrites: true in the driver so in-flight writes are transparently retried against the new primary.

πŸ“– 9. Read Preferences

Read preference controls which replica set member(s) serve read operations β€” trading off consistency for read scalability or reduced latency.

ModeBehavior
primaryReads only from the primary (default, most consistent)
primaryPreferredPrimary if available, else a secondary
secondaryReads only from secondaries
secondaryPreferredSecondary if available, else the primary
nearestReads from the member with lowest network latency

✍️ 10. Write Concerns

Write concern specifies how many replica set members must acknowledge a write before it's considered successful, directly affecting durability guarantees.

Setting a majority write concern

// Require acknowledgment from a majority of members
await collection.insertOne(
  { name: "Mouse" },
  { writeConcern: { w: "majority", wtimeout: 5000 } }
);

Warning

Using w: 1 (acknowledgment from the primary only) risks data loss if the primary fails before the write replicates to secondaries.

⏱️ 11. Replication Lag

Replication lag is the delay between a write occurring on the primary and it being applied on a secondary. Reading from a lagging secondary can return stale data.

Caution

Monitor replication lag closely when using secondary reads for anything beyond eventually-consistent, low-stakes queries like analytics or reporting.

πŸ“œ 12. Oplog

The oplog (operations log) is a special capped collection on the primary that records every write operation, forming the basis of replication β€” secondaries continuously tail and apply it.

Inspecting oplog status

// Checking oplog size and window (from the shell)
rs.printReplicationInfo();

Note

The oplog's size determines how long a secondary can be offline before it falls too far behind to catch up via normal replication.

πŸ”ƒ 13. Initial Sync

When a new or recovering member joins a replica set, it performs an initial sync β€” copying all data from an existing member before it can begin applying oplog entries.

Tip

Initial sync can be resource-intensive on large datasets β€” schedule new member additions during lower-traffic periods when possible.

🧩 14. Sharding

Sharding partitions a collection's data across multiple servers (shards), each holding a subset of the total dataset, allowing storage and throughput to scale horizontally.

Sharded Cluster
mongos (query router)
Config servers (cluster metadata)
Shards
Shard 1 (replica set)
Shard 2 (replica set)
Shard 3 (replica set)

πŸ”‘ 15. Shard Keys

The shard key is the field (or fields) MongoDB uses to distribute documents across shards. Choosing a good shard key is one of the most consequential decisions in a sharded deployment.

Sharding a collection by customerId

// Enabling sharding and choosing a shard key
sh.enableSharding("shop");
sh.shardCollection("shop.orders", { customerId: "hashed" });

Important

A poor shard key β€” one with low cardinality or a monotonically increasing value β€” can create "hot" shards that receive disproportionate traffic.

πŸ—„οΈ 16. Config Servers

Config servers store the sharded cluster's metadata β€” which chunks of data live on which shards β€” and must themselves run as a replica set for redundancy.

Note

Every mongos router consults the config servers to determine where to route each query.

🚦 17. Query Router (mongos)

mongos acts as the entry point for client applications, routing queries to the appropriate shard(s) and merging results transparently β€” applications connect to mongos as if it were a single database.

Connecting through mongos routers

const client = new MongoClient("mongodb://mongos1:27017,mongos2:27017");

🧱 18. Chunk Management

Data within a sharded collection is divided into chunks β€” contiguous ranges of shard key values β€” which MongoDB automatically splits and migrates as data grows.

Tip

Chunk splitting and migration happen automatically, but understanding them helps diagnose uneven data distribution across shards.

βš–οΈ 19. Balancer

The balancer is a background process that migrates chunks between shards to keep data distribution even, running automatically unless explicitly disabled.

Managing the balancer

// Checking balancer status
sh.getBalancerState();

// Temporarily disabling the balancer (e.g. during maintenance)
sh.stopBalancer();

Caution

Balancing activity consumes I/O and network resources β€” consider scheduling a balancing window during off-peak hours for write-heavy clusters.

πŸ—ΊοΈ 20. Zone Sharding

Zone sharding associates ranges of shard key values with specific shards, commonly used to keep data geographically local for compliance or latency reasons.

Pinning Indian customer data to a specific shard zone

sh.addShardToZone("shard-india", "IN");
sh.updateZoneKeyRange(
  "shop.orders",
  { region: "IN", customerId: MinKey },
  { region: "IN", customerId: MaxKey },
  "IN"
);

πŸ“ˆ 21. Scaling Strategies

  • Start with replication alone until read/write throughput or dataset size genuinely require sharding.
  • Use read replicas (secondary reads) to scale read-heavy workloads before introducing sharding complexity.
  • Choose shard keys based on actual query and write patterns, not convenience.
  • Consider zone sharding for data residency or latency-sensitive geographic distribution.

πŸ’Ύ 22. Backup & Recovery

Regular backups protect against data loss from corruption, accidental deletion, or catastrophic failure β€” replication alone does not protect against a bad write propagating to every replica.

MethodUse Case
mongodump / mongorestoreLogical backups for smaller datasets
Filesystem snapshotsFast, consistent backups of large datasets
Atlas continuous backupsManaged, point-in-time recovery

Danger

A bad deleteMany({}) or corrupted write replicates to every member of a replica set β€” replication is not a substitute for backups.

πŸ†˜ 23. Disaster Recovery

A solid disaster recovery plan defines a target Recovery Point Objective (RPO) and Recovery Time Objective (RTO), and includes regularly tested restore procedures β€” not just backups that are never verified.

Best Practice

Periodically perform a full restore drill in a non-production environment to confirm backups are actually usable when a real incident occurs.

🌟 24. Best Practices

  • Run replica sets with an odd number of voting members, ideally spread across failure domains.
  • Use w: "majority" write concern for critical data to avoid loss during failover.
  • Choose shard keys with high cardinality and even write distribution.
  • Monitor replication lag, oplog window, and balancer activity continuously.
  • Maintain and regularly test backup and restore procedures.

⚠️ 25. Common Mistakes

  1. Running a single standalone mongod in production with no replication.
  2. Choosing a monotonically increasing shard key (like a timestamp), causing hot shard problems.
  3. Relying on replication alone as a backup strategy.
  4. Ignoring replication lag when reading from secondaries, leading to stale data surprises.
  5. Under-provisioning the oplog size, shrinking the window available for secondaries to catch up.

Best Practice

Treat shard key selection as a near-permanent decision β€” changing it later typically requires resharding or migrating an entire collection.

❓ 26. Frequently Asked Questions

A minimum of three data-bearing members is recommended for production, allowing the set to tolerate one member failure while still maintaining a majority for elections.

No. Sharding and replication solve different problems and are typically used together β€” each shard in a production cluster is usually its own replica set.

Since MongoDB 5.0, resharding is supported, but it's a resource-intensive operation best avoided by choosing the right key upfront.

πŸ“Œ 27. Summary

>>"Replication keeps you online when a server fails; sharding keeps you fast when your data outgrows one."

Summary

Replication and sharding together form the backbone of a production-grade MongoDB deployment. Continue with transactions and data modeling patterns to complete a well-rounded understanding of MongoDB at scale.