Help Center/ Document Database Service/ FAQs/ Database Usage/ How Do I Manage the Balancer?
Updated on 2024-04-29 GMT+08:00

How Do I Manage the Balancer?

Scenario

The balancer occupies system resources of nodes in a cluster to migrate chunks, which may deteriorate the read and write performance of the cluster. If you have enabled data sharding and have high requirements on instance performance, or the service traffic has peak and off-peak differences, to avoid negative impact on your businesses, perform the following operations:

Constraints

  • The balancer applies only to cluster instances.
  • DDS enables the balancer by default.

Enabling the Balancer

  1. Connect to a cluster instance using Mongo Shell.
  2. After connecting to a dds mongos node, run the following command to switch to the config database:

    use config

  3. Run the following command to enable the balancer:

    sh.setBalancerState(true)

Disabling the Balancer

  1. Connect to a cluster instance using Mongo Shell.
  2. After connecting to a dds mongos node, run the following command to switch to the config database:

    use config

  3. Run the following command to check the status of the balancer:

    while( sh.isBalancerRunning() ) {
              print("waiting...");
              sleep(500);
    } 
    • If the command output is empty, the balancer is not executing any task. In this case, you can proceed to disable the balancer.
    • If the command output is waiting, the balancer is migrating chunks. In this case, you cannot disable the balancer. Otherwise, data may become inconsistent.

  4. If the command output in 3 is empty, run the following command to disable the balancer:

    sh.stopBalancer()

Scheduling a Balancing Window

To prevent chunk migration from affecting your businesses, you can perform the following operations to schedule a balancing window.

Before scheduling a balancing window, ensure that the balancer is enabled by referring to Enabling the Balancer.

  1. Connect to a cluster instance using Mongo Shell.
  2. After connecting to a dds mongos node, run the following command to switch to the config database:

    use config

  3. Run the following commands to schedule a balancing window:

    db.settings.update(
       { _id: "balancer" },
       { $set: { activeWindow : { start : "<start-time>", stop : "<stop-time>" } } },
       { upsert: true }
    )
    • <start-time>: indicates the beginning of the balancing window. Specify the time in the HH:MM format (UTC time). For HH values, use hour values ranging from 00 to 23. For MM value, use minute values ranging from 00 to 59.
    • <stop-time>: indicates the end of the balancing window. Specify the time in the HH:MM format (UTC time). For HH values, use hour values ranging from 00 to 23. For MM value, use minute values ranging from 00 to 59.

    You can run the sh.status() command to check a balancing window schedule. The following command output in Figure 1 shows that the balancing window is 22:00 to 24:00.

    Figure 1 Checking a balancing window schedule

Removing a Balancing Window Schedule

If you have set the balancing window and wish to remove the schedule so that the balancer is always running, run the following command:

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