The balancer module in the sharded cluster can balance data to ensure that the number of chunks in each shard is roughly the same. The balancer operation involves chunk migration, so it also consumes hardware resources on the shard server.
Users can configure the balancer window to specify which time period for data balancing in the background. By specifying the balancing operation during off-peak hours, the impact on the online service can be effectively avoided.
Settings, steps and methods are as follows:
Use mongo shell to connect to the mongos node.
Switch to the config database (use config).
Confirm that the balancer switch is on (sh.getBalancerState()).
Set the balancer time window.
db.settings.update(
{ _id: "balancer" },
{ $set: { activeWindow : { start : "<start time>", stop : "<end time>" } } },
{ upsert: true }
)Where "start time" and "end time" are both in the form of HH:MM, such as 02:30.