liveBook · Manning

Chunk presplitting

As explained in MongoDB 8.0 in Action, Third Edition

Chunk presplitting is the proactive division of an empty sharded collection into planned shard-key ranges before bulk data ingestion.

Chunk presplitting proactively divides an empty sharded collection into planned shard-key ranges before bulk ingestion. It can improve throughput when automatic chunk creation and balancing cannot distribute incoming data quickly enough, especially with large loads or monotonically changing shard keys. section 9.3.9 section 9.6

Use presplitting selectively and account for these operational conditions:

  • Presplit only an empty collection; manually splitting populated data can create unpredictable chunk sizes and ineffective balancing. section 9.3.9
  • Choose split points that represent meaningful shard-key ranges, then distribute the resulting chunks across shards. section 9.3.9
  • Prefer the automatic balancer in ordinary situations, using manual migration when the cluster cannot distribute ranges within the required balancing window. section 9.3.9

Partitioning routes before ingestion

For a collection sharded by src_airport, dst_airport, and airline.name, define split points for routes such as JFK–LHR with British Airways, LAX–NRT with Japan Airlines, and DXB–SYD with Emirates. Run the split command in the admin database, then assign each resulting range to a shard with moveChunk. section 9.3.9

{ "src_airport": 1, "dst_airport": 1, "airline.name": 1 }.

Use the displayed compound shard key to identify the fields that define the planned chunk ranges. 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
    });
});

Use the split-point script to divide the empty collection at meaningful route and airline combinations. section 9.3.9

// Define the shards for distributing the chunks
var shards = ["shard0000", "shard0001", "shard0002", "shard0003"];
var splitPoints = [ /* Route data split points */ ];

// Determine the bounds for each chunk
splitPoints.forEach(function(point, index) {
    var lowerBound = { "src_airport": MinKey,
➥"dst_airport": MinKey, "airline.name": MinKey };
    var upperBound = { "src_airport": MaxKey,
➥"dst_airport": MaxKey, "airline.name": MaxKey };
    if (index > 0) {
        lowerBound = splitPoints[index - 1];
    }
    if (index < splitPoints.length) {
        upperBound = point;
    }

    // Manually assign a chunk to a shard using the moveChunk command
    db.adminCommand({
        moveChunk: "sample_training.routes",
        find: lowerBound,
        to: shards[index % shards.length],
        bounds: [lowerBound, upperBound]
    });
});

Use the range bounds and moveChunk commands to assign successive chunks across the shard list. section 9.3.9

How MongoDB 8.0 in Action, Third Edition covers it

How deep it goes
Explains split points, chunk bounds, manual movement, and the empty-collection constraint, but defers populated-collection procedures.
Examples
Uses airline routes, compound split points, mongosh commands, chunk bounds, and moveChunk assignments.
What it assumes
Assumes familiarity with sharded collections, shard keys, chunks, shards, and administrative commands.
What you can do after
Apply it via mongosh to split an empty collection and assign planned ranges across shards.
How it connects
Connects sharding with shard keys, chunks, automatic balancing, manual migration, and monotonically changing shard keys.

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.