6 min read
•Question 12 of 27hardWhat is sharding in MongoDB?
Understanding horizontal scaling.
What You'll Learn
- What sharding is
- Shard key selection
- Architecture components
What is Sharding?
Sharding distributes data across multiple machines for horizontal scaling. Each shard contains a subset of the data.
When to Shard
- Data too large for single server
- High write throughput needed
- Working set exceeds RAM
- Geographic data distribution
Sharded Cluster Components
code.jsJavaScript
mongos (Router)
|
Config Servers
/ | \
Shard1 Shard2 Shard3- mongos: Query router
- Config servers: Cluster metadata
- Shards: Data partitions (replica sets)
Shard Key
code.jsJavaScript
// Enable sharding on database
sh.enableSharding("mydb");
// Shard a collection
sh.shardCollection("mydb.users", { region: 1 });
// Hashed shard key (better distribution)
sh.shardCollection("mydb.events", { _id: "hashed" });Shard Key Selection
Good Shard Keys
- High cardinality
- Even distribution
- Query isolation (queries target single shard)
Bad Shard Keys
- Low cardinality (few unique values)
- Monotonically increasing (timestamp, ObjectId)
- Frequently updated fields
Query Routing
code.jsJavaScript
// Targeted query (includes shard key)
db.users.find({ region: "US", name: "John" });
// Routes to specific shard
// Scatter-gather (no shard key)
db.users.find({ name: "John" });
// Queries all shards