D · Deployment foundations
Choose replication or sharding for the problem you have
A service that must survive a host failure has a different need from a collection that has outgrown one machine. This guide separates availability, data distribution, and recovery so the architecture follows the actual failure or scale limit.
Two changes that solve different problems
A replica set keeps multiple members with the same data. One member is primary for writes; eligible secondary members replicate the operation log and can participate in an election if the primary is unavailable. This gives the deployment a path to fail over. It does not split one collection across several storage partitions.
Sharding partitions a collection's data across shards using a shard key. A query that includes a useful shard-key constraint can be routed to the relevant shard or shards. A query without it may need work across many shards. Sharding is a distribution and capacity tool, not simply “more copies.” Shards are commonly replica sets, so a deployment can use both mechanisms.
Diagnose the bottleneck before choosing
If a single database host failing is the concern, begin with the availability design and recovery objectives: replication can elect a new primary, but clients must reconnect and applications must tolerate a short interruption. If the working set, storage, or throughput has outgrown a single server, sharding may be relevant—but first profile the query and write patterns to see what is actually saturated.
// On a replica-set member, inspect observed member state:
rs.status().members.map(({ name, stateStr, health }) => ({ name, stateStr, health }))
// On mongos, inspect the sharded-cluster layout:
sh.status()
These are diagnostic commands, not configuration changes. rs.status() and sh.status() answer different questions and are available in the appropriate deployment context; a standalone server does not have replica-set members to report.
For sharding, the shard key matters because it controls distribution and routing. A key with a narrow range of values can concentrate traffic. A monotonically increasing key can send all new writes to one edge of a range-based distribution. A high-cardinality field is not automatically a good key if most application queries do not include it. Evaluate cardinality, write distribution, and the filters your application actually sends.
Tempting answers that fail
“Add a replica and writes will scale horizontally” confuses copies with partitions. Replication mainly provides redundant members and failover; writes normally go to the primary. “Turn on sharding and every query gets faster” ignores scatter-gather work and shard-key design. Neither replica sets nor sharding protects against a logically valid but accidental delete: that operation is replicated too.
Replication is also not a backup. Backups preserve recoverable points in time and need their own retention, access controls, and restore tests. A second copy that immediately receives the same bad write is not an independent recovery point.
A practical decision sequence
- State the requirement: failover, more capacity, or point-in-time recovery.
- Measure where the current deployment is constrained and which operations dominate.
- If distributing data, list the top application queries and verify candidate shard keys against them.
- Test failover or restore in a non-production environment and record the observed recovery time.
Check your understanding
Question: A team needs to recover from accidental deletion. Which feature should it rely on: a replica set, sharding, or a tested backup?
Answer: A tested backup with a suitable recovery point is the relevant recovery mechanism. Replication and sharding can improve availability or distribution, but they also propagate the delete.