- Sharding >
- Manage Sharded Cluster Balancer
Manage Sharded Cluster Balancer¶
On this page
This page describes provides common administrative procedures related to balancing. For an introduction to balancing, see Shard Balancing. For lower level information on balancing, see Cluster Balancer.
Check the Balancer Lock¶
To see if the balancer process is active in your cluster, do the following:
Issue the following command to switch to the Config Database Contents:
Use the following query to return the balancer lock:
When this command returns, you will see output like the following:
This output confirms that:
- The balancer originates from the
mongos
running on the system with the hostnamemongos0.example.net
. - The value in the
state
field indicates that amongos
has the lock. For version 2.0 and later, the value of an active lock is2
; for earlier versions the value is1
.
Optional
You can also use the following shell helper, which returns a boolean to report if the balancer is active:
Schedule the Balancing Window¶
In some situations, particularly when your data set grows slowly and a migration can impact performance, it’s useful to be able to ensure that the balancer is active only at certain times. Use the following procedure to specify a window during which the balancer will be able to migrate chunks:
Issue the following command to switch to the Config Database Contents:
Use an operation modeled on the following example
update()
operation to modify the balancer’s window:Replace
<start-time>
and<end-time>
with time values using two digit hour and minute values (e.gHH:MM
) that describe the beginning and end boundaries of the balancing window. These times will be evaluated relative to the time zone of each individualmongos
instance in the sharded cluster. If yourmongos
instances are physically located in different time zones, use a common time zone (e.g. GMT) to ensure that the balancer window is interpreted correctly.For instance, running the following will force the balancer to run between 11PM and 6AM local time only:
Note
The balancer window must be sufficient to complete the migration of all data inserted during the day.
As data insert rates can change based on activity and usage patterns, it is important to ensure that the balancing window you select will be sufficient to support the needs of your deployment.
Remove a Balancing Window Schedule¶
If you have set the balancing window and wish to remove the schedule so that the balancer is always running, issue the following sequence of operations:
Disable the Balancer¶
By default the balancer may run at any time and only moves chunks as needed. To disable the balancer for a short period of time and prevent all migration, use the following procedure:
Issue one of the following operations to disable the balancer:
Later, issue one the following operations to enable the balancer:
Note
If a migration is in progress, the system will complete
the in-progress migration. After disabling, you can use the
following operation in the mongo
shell to determine if
there are no migrations in progress:
The above process and the sh.setBalancerState()
,
sh.startBalancer()
, and sh.stopBalancer()
helpers provide
wrappers on the following process, which may be useful if you need to
run this operation from a driver that does not have helper functions:
Issue the following command to switch to the Config Database Contents:
Issue the following update to disable the balancer:
To enable the balancer again, alter the value of “stopped” as follows:
Disable Balancing During Backups¶
If MongoDB migrates a chunk during a backup, you can end with an inconsistent snapshot of your sharded cluster. Never run a backup while the balancer is active. To ensure that the balancer is inactive during your backup operation:
- Set the balancing window so that the balancer is inactive during the backup. Ensure that the backup can complete while you have the balancer disabled.
- manually disable the balancer for the duration of the backup procedure.
Confirm that the balancer is not active using the
sh.getBalancerState()
method before starting a backup
operation. When the backup procedure is complete you can reactivate
the balancer process.