OPTIONS

Manage Sharded Cluster Balancer

This page describes common administrative procedures related to balancing. For an introduction to balancing, see Sharded Collection Balancing. For lower level information on balancing, see Cluster Balancer.

Check the Balancer State

sh.getBalancerState() checks if the balancer is enabled (i.e. that the balancer is permitted to run). sh.getBalancerState() does not check if the balancer is actively balancing chunks.

To see if the balancer is enabled in your sharded cluster, issue the following command, which returns a boolean:

sh.getBalancerState()

New in version 3.0.0: You can also see if the balancer is enabled using sh.status(). The currently-enabled field indicates whether the balancer is enabled, while the currently-running field indicates if the balancer is currently running.

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:

    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.

Schedule the Balancing Window

In some situations, particularly when your data set grows slowly and a migration can impact performance, it is useful to ensure that the balancer is active only at certain times. The following procedure specifies the activeWindow, which is the timeframe during which the balancer will be able to migrate chunks:

1

Connect to mongos using the mongo shell.

You can connect to any mongos in the cluster.

2

Switch to the Config Database.

Issue the following command to switch to the config database.

use config
3

Ensure that the balancer is not stopped.

The balancer will not activate in the stopped state. To ensure that the balancer is not stopped, use sh.setBalancerState(), as in the following:

sh.setBalancerState( true )

The balancer will not start if you are outside of the activeWindow timeframe.

4

Modify the balancer’s window.

Set the activeWindow using update(), as in the following:

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

Replace <start-time> and <end-time> with time values using two digit hour and minute values (i.e. HH:MM) that specify the beginning and end boundaries of the balancing window.

  • For HH values, use hour values ranging from 00 - 23.
  • For MM value, use minute values ranging from 00 - 59.

MongoDB evaluates the start and stop times 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, set the time zone on each server to UTC+-00:00 so that the balancer window is uniformly interpreted.

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.

Do not use the sh.startBalancer() method when you have set an activeWindow.

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, use $unset to clear the activeWindow, as in the following:

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 the following operation to disable the balancer:

    sh.stopBalancer()
    

    If a migration is in progress, the system will complete the in-progress migration before stopping.

  3. To verify that the balancer will not start, issue the following command, which returns false if the balancer is disabled:

    sh.getBalancerState()
    

    Optionally, to verify no migrations are in progress after disabling, issue the following operation in the mongo shell:

    use config
    while( sh.isBalancerRunning() ) {
              print("waiting...");
              sleep(1000);
    }
    

Note

To disable the balancer from a driver that does not have the sh.stopBalancer() or sh.setBalancerState() helpers, issue the following command from the config database:

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

Enable the Balancer

Use this procedure if you have disabled the balancer and are ready to re-enable it:

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

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

    From the mongo shell, issue:

    sh.setBalancerState(true)
    

    From a driver that does not have the sh.startBalancer() helper, issue the following from the config database:

    db.settings.update( { _id: "balancer" }, { $set : { stopped: false } } , { upsert: 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:

If you turn the balancer off while it is in the middle of a balancing round, the shut down is not instantaneous. The balancer completes the chunk move in-progress and then ceases all further balancing rounds.

Before starting a backup operation, confirm that the balancer is not active. You can use the following command to determine if the balancer is active:

!sh.getBalancerState() && !sh.isBalancerRunning()

When the backup procedure is complete you can reactivate the balancer process.

Disable Balancing on a Collection

You can disable balancing for a specific collection with the sh.disableBalancing() method. You may want to disable the balancer for a specific collection to support maintenance operations or atypical workloads, for example, during data ingestions or data exports.

When you disable balancing on a collection, MongoDB will not interrupt in progress migrations.

To disable balancing on a collection, connect to a mongos with the mongo shell and call the sh.disableBalancing() method.

For example:

sh.disableBalancing("students.grades")

The sh.disableBalancing() method accepts as its parameter the full namespace of the collection.

Enable Balancing on a Collection

You can enable balancing for a specific collection with the sh.enableBalancing() method.

When you enable balancing for a collection, MongoDB will not immediately begin balancing data. However, if the data in your sharded collection is not balanced, MongoDB will be able to begin distributing the data more evenly.

To enable balancing on a collection, connect to a mongos with the mongo shell and call the sh.enableBalancing() method.

For example:

sh.enableBalancing("students.grades")

The sh.enableBalancing() method accepts as its parameter the full namespace of the collection.

Confirm Balancing is Enabled or Disabled

To confirm whether balancing for a collection is enabled or disabled, query the collections collection in the config database for the collection namespace and check the noBalance field. For example:

db.getSiblingDB("config").collections.findOne({_id : "students.grades"}).noBalance;

This operation will return a null error, true, false, or no output:

  • A null error indicates the collection namespace is incorrect.
  • If the result is true, balancing is disabled.
  • If the result is false, balancing is enabled currently but has been disabled in the past for the collection. Balancing of this collection will begin the next time the balancer runs.
  • If the operation returns no output, balancing is enabled currently and has never been disabled in the past for this collection. Balancing of this collection will begin the next time the balancer runs.

New in version 3.0.0: You can also see if the balancer is enabled using sh.status(). The currently-enabled field indicates if the balancer is enabled.

Was this page helpful?

Yes No

Thank you for your feedback!

We're sorry! You can Report a Problem to help us improve this page.