Optimizing Sharded Clusters
1. Understanding Balancer Process
| Aspect | Detail |
|---|---|
| Owner | Runs on primary config server |
| Trigger | Imbalance threshold (chunk count diff) |
| Operation | One migration at a time per shard pair |
2. Starting and Stopping Balancer
| Method | Detail |
|---|---|
| sh.startBalancer() | Enable |
| sh.stopBalancer() | Disable |
| sh.getBalancerState() | Current state |
3. Configuring Balancer Window
db.settings.update({_id:"balancer"},
{$set:{activeWindow:{start:"01:00", stop:"06:00"}}}, {upsert:true});
| Aspect | Detail |
|---|---|
| Window | Limit balancing to off-peak hours |
4. Understanding Chunk Migration
| Phase | Detail |
|---|---|
| Clone | Copy docs |
| Catch-up | Replay incoming writes |
| Commit | Atomic metadata switch |
| Cleanup | Orphan deletion on source |
5. Splitting Chunks Manually
| Method | Detail |
|---|---|
| sh.splitAt(ns, middle) | Split at exact key |
| sh.splitFind(ns, query) | Split chunk containing query |
6. Moving Chunks
| Method | Detail |
|---|---|
| sh.moveChunk(ns, find, to) | Manual migration |
| moveRange 6.0+ | Move by key range |
7. Checking Shard Status
| Method | Detail |
|---|---|
| sh.status() | Cluster topology + chunks per shard |
| db.collection.getShardDistribution() | Per-shard storage stats |
8. Monitoring Shard Distribution
| Metric | Use |
|---|---|
| Chunk count per shard | Detect imbalance |
| Data size per shard | True load proxy |
| Migration history | changelog collection on config DB |
9. Avoiding Jumbo Chunks
| Cause | Mitigation |
|---|---|
| Low cardinality key | Choose better shard key / refine |
| Single huge doc | Refactor schema |
| Jumbo flag | Clear after manual split (4.4+) |
10. Understanding Broadcast vs Targeted Queries
| Type | Trigger | Cost |
|---|---|---|
| Targeted | Query includes shard key | Low; one shard |
| Broadcast (scatter-gather) | No shard key | High; all shards |