Lesson 8 of 9 · 25m
Sharding, Capacity, and the Cost of Horizontal Growth
Treat sharding as an operational commitment driven by shard keys, routing, balancing, and workload distribution rather than a simple scale button.
Sharding is where MongoDB stops being merely a database and becomes a distributed systems lifestyle. Done for the right reason, it lets you scale write throughput and data beyond one replica set. Done casually, it turns every bad data-modeling decision into a cluster-wide distributed problem. The shard key is the decision you live with.
Know why you are sharding
Good reasons include storage beyond one replica set, write throughput beyond one primary, and data residency isolation. Bad reasons include vague future-proofing, a slow query that lacks an index, and dashboard traffic that should be served from rollups. Sharding does not make unindexed queries fast. It often makes them scatter across shards and slower.
single replica set primary all writes for collection
sharded collection writes routed by shard key
targeted query shard key predicate routes to one or few shards
scatter-gather query no useful shard key predicate, hits many shardsIf hot reads cannot include the shard key, you may need duplication, a different key, or a non-sharded design.
Choose shard keys with cardinality and distribution
A good shard key has high cardinality, even distribution, and matches important query patterns:
sh.shardCollection(
"shop.events",
{ tenantId: 1, createdAt: 1 }
)A monotonically increasing key such as a timestamp alone can route all new writes to one hot shard. A low-cardinality key such as country can create jumbo chunks and imbalance. A random key distributes writes well but may destroy locality for reads.
Jumbo chunks and hot shards are operational symptoms
Chunks are ranges of shard key values that MongoDB balances across shards. If many documents share the same key or range, chunks cannot split effectively. The result is a hot shard that receives disproportionate traffic. Watch chunk distribution, shard-level operations, and balancing behavior:
sh.status()
db.getSiblingDB("config").chunks.find({ ns: "shop.events" }).limit(5)Balancing moves data, which costs I/O and can affect latency. It is not free background noise.
Transactions and aggregation across shards have sharper edges
Multi-shard transactions are possible, but coordination cost rises. Aggregation may run partly on shards and partly on the mongos router depending on stages. $lookup, large sorts, and high-cardinality groups can become expensive distributed plans. This is another reason sharding amplifies modeling decisions rather than hiding them.
Capacity discipline still applies
Measure per-shard CPU, disk I/O, cache pressure, replication lag, and query fan-out. A cluster can be "green" in aggregate while one shard melts. Design for removal too: deleting or archiving data in huge ranges can create load and fragmentation. TTL indexes help for time-bounded data:
db.events.createIndex({ expiresAt: 1 }, { expireAfterSeconds: 0 })TTL deletion is eventually consistent maintenance, not instantaneous erasure.
Field notes
- You cannot casually change a shard key in older designs; treat it as a migration-level decision.
- Shard key choice is a query-pattern decision first and a distribution decision second.
- Scatter-gather queries may be acceptable for administration but not for hot user-facing paths.
- Test with skewed data. Uniform synthetic data hides almost every real sharding problem.
Was this lesson helpful?