liveBook · Manning

Balancer

As explained in MongoDB 8.0 in Action, Third Edition

The MongoDB balancer is the cluster component that distributes sharded data among shards to maintain balanced data placement.

The MongoDB balancer distributes sharded data across shards so each shard carries a more uniform share of a collection’s data, while respecting configured zones. section 9.3.8

Operational behavior and guidance:

  • The config server replica set’s primary manages balancing, which usually remains transparent to applications but can cause slight performance effects. section 9.3.8
  • A shard participates in only one migration at a time; a cluster with n shards can run up to n/2 migrations concurrently. section 9.3.8
  • Balancing rounds begin when the data-size difference between the most- and least-loaded shards exceeds the configured threshold. section 9.3.8
  • Schedule a balancing window to reduce disruption during peak production traffic, and verify that the balancer is running before relying on it. section 9.3.8
  • Prefer automatic range transfers; use manual presplitting or migration for empty collections, high-volume ingestion, or workloads the balancer cannot distribute within its window. section 9.3.9
use config

Connect to mongos, switch to the config database, and configure the balancer window with the related start, stop, and status commands. section 9.3.8

Presplitting route data

For an empty sample_training.routes collection, split around meaningful source-airport, destination-airport, and airline combinations such as JFK–LHR with British Airways, then distribute the resulting ranges when necessary. section 9.3.9

// Split points to segment the collection into chunks
➥ // based on common routes and airlines
var splitPoints = [
    { "src_airport": "JFK", "dst_airport": "LHR",
➥"airline.name": "British Airways" },
    { "src_airport": "LAX", "dst_airport": "NRT",
➥"airline.name": "Japan Airlines" },
    { "src_airport": "DXB", "dst_airport": "SYD",
➥"airline.name": "Emirates" }
];

// Loop through the split points and apply the 'split' command
splitPoints.forEach(function(point) {
    db.adminCommand({
        split: "sample_training.routes",
        middle: point
    });
});

Apply route-based split points with the split command before considering manual range placement. section 9.3.9

How MongoDB 8.0 in Action, Third Edition covers it

How deep it goes
Explains size-based triggers, migration limits, balancing windows, and manual exceptions, but not balancer internals beyond config-server management.
Examples
Uses balancing windows and route-based chunk splitting for sample_training.routes.
What it assumes
Assumes sharded collections, shards, chunks, and shard keys; teaches balancing behavior and administration within that context.
What you can do after
Operate balancing windows and evaluate when presplitting or manual range migration is justified.
How it connects
Connects sharding with chunks, shard keys, zones, config server replica sets, and manual chunk migration.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of MongoDB 8.0 in Action, Third Edition. The section links point to the parts of the book it draws on.