Problem Statement
What triggers chunk migration in a sharded cluster?
Explanation
The MongoDB balancer is a background process that monitors chunk distribution across shards. When it detects that one shard has significantly more chunks than others, it automatically migrates chunks from heavily loaded shards to less loaded ones.
This automatic balancing ensures even data distribution as your data grows. The balancer runs in the background and can be configured to run only during specific time windows to avoid impacting production workloads. Chunk migrations are transparent to applications, though they do consume resources during the migration process.
Code Solution
SolutionRead Only
// Check balancer status
sh.isBalancerRunning()
// View balancer settings
sh.getBalancerState()
// Configure balancer window (run only at night)
use config
db.settings.updateOne(
{ _id: "balancer" },
{ $set: { activeWindow: { start: "23:00", stop: "06:00" } } },
{ upsert: true }
)
// Disable balancer temporarily
sh.stopBalancer()