How does MongoDB's configured chunk (range) size affect balancing, and when would you change the default?
answer
- a target size, not a hard cap
- default changed in MongoDB 6.0
- legal values are bounded on both ends
- the balance threshold is measured in it
- not retroactive to existing ranges
basics
~20 sThe default range size is 128 MB in MongoDB 6.0 and later (64 MB before), settable from 1 to 1024 MB. Smaller ranges balance more finely but produce more migrations and more routing metadata; larger ranges migrate less often but distribute more coarsely.
solid answer
~40 sRange size is the target size the balancer uses when it splits data off for a migration, and it is the unit the balance threshold is expressed in. The default is 128 MB on MongoDB 6.0+ (64 MB in earlier versions), and the legal range is 1–1024 MB. You set it cluster-wide by upserting `{_id: "chunksize", value: <MB>}` into `config.settings`, or per collection with `db.adminCommand({configureCollectionBalancing: "db.coll", chunkSize: <MB>})`. Smaller ranges give finer-grained placement and cheaper individual moves, at the cost of many more ranges to migrate, track and cache. Larger ranges cut migration churn and metadata but make each move heavier and let shards drift further apart before the balancer reacts. Changing the setting is not retroactive: existing ranges are only reshaped as they are split or merged later.
code
javascript · 7 lines// cluster-wide default, in megabytes
use config
db.settings.updateOne({ _id: "chunksize" }, { $set: { value: 256 } }, { upsert: true })
// per-collection override, plus on-demand metadata consolidation
db.adminCommand({ configureCollectionBalancing: "app.events", chunkSize: 256 })
db.adminCommand({ configureCollectionBalancing: "app.events", defragmentCollection: true })go deeper
Know that MongoDB has a configurable target size for the ranges it distributes across shards, and that the default is fine for almost every deployment.
Be ready to state the default and the legal bounds, name where the setting lives, and explain the granularity-versus-overhead trade-off in both directions.
Show that you would reach for scheduling or defragmentation before resizing, and that you know the change is not retroactive so its effect appears only as ranges are reshaped.
Frame range size as one knob in a migration budget alongside the balancer window, per-collection settings and shard count, and insist on evidence — measured migration volume and imbalance — before anyone changes a cluster-wide default.
## What the setting actually controls The configured range size (still spelled `chunksize` in the settings document) is a **target**, not a hard cap. It governs how large a slice the balancer carves off when it needs to move data, and it is the yardstick for the balance threshold: MongoDB 6.0+ treats a collection as balanced when the difference between the most- and least-loaded shard is under roughly three times this value. Double the range size and you double the imbalance the cluster will tolerate before doing anything. The default is **128 MB** on MongoDB 6.0 and later; earlier versions defaulted to 64 MB. The allowed values run from **1 MB to 1024 MB**. ## Setting it Cluster-wide, it is a document in the `config` database: ```javascript use config db.settings.updateOne({ _id: "chunksize" }, { $set: { value: 256 } }, { upsert: true }) ``` Since MongoDB 6.0 you can also scope it to one collection, which is far more useful in a mixed cluster where a huge append-only events collection and a small reference collection have completely different needs: ```javascript db.adminCommand({ configureCollectionBalancing: "app.events", chunkSize: 256 }) ``` Neither form rewrites existing ranges. The new value applies as ranges are split for future migrations, or merged by defragmentation. If you want existing metadata consolidated now, `configureCollectionBalancing` with `defragmentCollection: true` merges contiguous ranges owned by the same shard. ## The trade-off in both directions **Smaller ranges** mean more of them. Placement gets finer — the balancer can even out shards in small increments, and each individual migration copies less data, holds its critical section briefly and is easier to fit inside a maintenance window. The costs are volume and bookkeeping: more `config.chunks` documents, a larger routing table for every `mongos` and shard to cache and refresh, more balancer rounds, and more total overhead because each migration carries fixed costs (metadata commit, router refreshes, range-deletion scheduling) regardless of how little data it moves. **Larger ranges** mean fewer, heavier moves. Metadata stays small and the balancer is quieter, which is attractive on write-heavy clusters where migration traffic competes with the application for disk and cache. But each move copies more data, occupies its donor/recipient pair for longer, and blocks writes to that range for the duration of the commit critical section. Placement is coarser, so shards can sit further apart, and very large ranges are more likely to run into the indivisible-range problem where a single shard-key value cannot be split at all. ## When you would actually change it Most clusters never touch it, and "we tuned chunk size" is rarely the fix for a real problem. Reasons that hold up: - **Very large documents or very few distinct shard-key values**: a smaller range size can give the balancer something to work with, though a shard key with too little cardinality is a modelling problem, not a sizing one. - **Migration traffic hurting the workload**: raising the size reduces the number of moves, though scheduling with a balancer window is usually a better lever. - **A bulk load or backfill**: operators sometimes tune size alongside pre-splitting so the initial spread happens without a long migration tail. - **Metadata bloat**: a collection that accumulated an enormous number of tiny ranges over years is better served by defragmentation than by shrinking the size further. ## How to talk about it The honest framing is that range size trades *balance granularity* against *migration and metadata overhead*, and that the balance threshold is denominated in it. Anyone who claims a single "correct" value, or that shrinking the range size improves query performance, is guessing: routing cost depends on whether the query targets one shard, not on how many ranges exist.
- Does raising the configured range size immediately merge the existing ranges of a collection?No. The setting applies to future splits and merges; existing ranges are left as they are. To consolidate current metadata you run db.adminCommand({configureCollectionBalancing: "db.coll", defragmentCollection: true}), which merges contiguous ranges owned by the same shard. That defragmentation is itself background work and competes with normal traffic.
- Can two collections in the same cluster use different range sizes?Yes, since MongoDB 6.0. configureCollectionBalancing takes a per-collection chunkSize, which overrides the cluster-wide config.settings value. That is useful when one collection is a huge append-only log and another is a small, frequently rebalanced dimension collection with very different migration economics.
saying these in an interview costs you the question
- Calls the configured size a hard maximum a range can never exceed
- Thinks changing the setting rewrites existing ranges immediately
- Claims smaller ranges make queries faster
- Names a default without saying which MongoDB version it belongs to
- Treats range-size tuning as the fix for a low-cardinality shard key