Navigation
This version of the documentation is archived and no longer supported.
  • Sharding >
  • Manage Sharded Cluster Balancer

Manage Sharded Cluster Balancer

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:

  1. Connect to any mongos in the cluster using the mongo shell.

  2. Issue the following command to switch to the Config Database Contents:

    use config
    
  3. Use the following query to return the balancer lock:

    db.locks.find( { _id : "balancer" } ).pretty()
    

When this command returns, you will see output like the following:

{   "_id" : "balancer",
"process" : "mongos0.example.net:1292810611:1804289383",
  "state" : 2,
     "ts" : ObjectId("4d0f872630c42d1978be8a2e"),
   "when" : "Mon Dec 20 2010 11:41:10 GMT-0500 (EST)",
    "who" : "mongos0.example.net:1292810611:1804289383:Balancer:846930886",
    "why" : "doing balance round" }

This output confirms that:

  • The balancer originates from the mongos running on the system with the hostname mongos0.example.net.
  • The value in the state field indicates that a mongos has the lock. For version 2.0 and later, the value of an active lock is 2; for earlier versions the value is 1.

Optional

You can also use the following shell helper, which returns a boolean to report if the balancer is active:

sh.getBalancerState()

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:

  1. Connect to any mongos in the cluster using the mongo shell.

  2. Issue the following command to switch to the Config Database Contents:

    use config
    
  3. Use an operation modeled on the following example update() operation to modify the balancer’s window:

    db.settings.update({ _id : "balancer" }, { $set : { activeWindow : { start : "<start-time>", stop : "<stop-time>" } } }, true )
    

    Replace <start-time> and <end-time> with time values using two digit hour and minute values (e.g HH:MM) that describe the beginning and end boundaries of the balancing window. These times will be evaluated relative to the time zone of each individual mongos instance in the sharded cluster. If your mongos 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:

    db.settings.update({ _id : "balancer" }, { $set : { activeWindow : { start : "23:00", stop : "6:00" } } }, true )
    

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:

use config
db.settings.update({ _id : "balancer" }, { $unset : { activeWindow : true } })

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:

  1. Connect to any mongos in the cluster using the mongo shell.

  2. Issue one of the following operations to disable the balancer:

    sh.stopBalancer()
    
  3. Later, issue one the following operations to enable the balancer:

    sh.startBalancer()
    

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:

use config
while( db.locks.findOne({_id: "balancer"}).state ) {
       print("waiting..."); sleep(1000);
}

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:

  1. Connect to any mongos in the cluster using the mongo shell.

  2. Issue the following command to switch to the Config Database Contents:

    use config
    
  3. Issue the following update to disable the balancer:

    db.settings.update( { _id: "balancer" }, { $set : { stopped: true } } , true );
    
  4. To enable the balancer again, alter the value of “stopped” as follows:

    db.settings.update( { _id: "balancer" }, { $set : { stopped: false } } , true );
    

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:

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.