All Products
Search
Document Center

Data Transmission Service:How do I resolve uneven data distribution in a sharded MongoDB cluster

Last Updated:Sep 23, 2026

Enabling the balancer and performing pre-splitting can effectively resolve issues where most data is written to a single shard (data skew).

Enable the balancer

If the balancer is disabled, or the configured balancer window has not yet been reached, you can enable the balancer or temporarily remove the balancer window to start data balancing immediately.

  1. Connect to the sharded MongoDB cluster instance.

  2. In the command window of the mongos node, switch to the config database.

    use config
  3. Run the following command based on your requirements.

    • Enable the balancer

      sh.setBalancerState(true)
    • Temporarily remove the balancer window

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

Pre-splitting

MongoDB supports both ranged sharding and hashed sharding. Pre-splitting distributes chunk values across multiple shard nodes as evenly as possible, which helps balance the load during DTS data synchronization or migration.

Hashed sharding

Use the numInitialChunks parameter to perform pre-splitting quickly and easily. The default value is number of shards × 2, and the maximum value is number of shards × 8192. For more information, see sh.shardCollection().

sh.shardCollection("phonebook.contacts", { last_name: "hashed" }, false, {numInitialChunks: 16384})

Ranged sharding

  • If the source MongoDB also uses a sharded cluster architecture, you can query the data in config.chunks to obtain the chunk ranges of the corresponding sharded collections in the source MongoDB, and use them as a reference for the <split_value> in subsequent pre-splitting commands.

  • If the source MongoDB is a replica set, you can determine the specific range of the shard key only by running the find command, and then design appropriate split points based on that range.

    # Get the minimum value of the shard key
    db.<coll>.find().sort({<shardKey>:1}).limit(1)
    # Get the maximum value of the shard key
    db.<coll>.find().sort({<shardKey>:-1).limit(1)

Command syntax

Note

The following example uses the splitAt command. For more information, see sh.splitAt(), sh.splitFind(), and Split Chunks in a Sharded Cluster.

sh.splitAt("<db>.<coll>", {"<shardKey>":<split_value>})

Example

sh.splitAt("test.test", {"id":0})
sh.splitAt("test.test", {"id":50000})
sh.splitAt("test.test", {"id":75000})

After you complete pre-splitting, you can run the sh.status() command on the mongos node to verify the results.