Contents
How are shard key values distributed in MongoDB?
MongoDB divides the span of shard key values (or hashed shard key values) into non-overlapping ranges of shard key values (or hashed shard key values). Each range is associated with a chunk, and MongoDB attempts to distribute chunks evenly among the shards in the cluster.
Why does MongoDB split documents into smaller chunks?
MongoDB will split large chunks into smaller ones if, and only if, the shard keys are different. If you have too many documents with the same shard key you end up with jumbo chunks.
Why are Jumbo chunks bad for MongoDB?
If you have too many documents with the same shard key you end up with jumbo chunks. Jumbo chunks are bad not only because they cause the data to be unevenly distributed but also because once they grow too large you cannot move them between shards at all. 3.
Why are unique indexes not allowed in MongoDB?
The unique index constraints mean that: For a to-be-sharded collection, you cannot shard the collection if the collection has other unique indexes. For an already-sharded collection, you cannot create unique indexes on other fields.
How is sharding done in MongoDB 4.0.3?
Starting in MongoDB 4.0.3, the shard collection operation can perform an initial chunk creation and distribution for empty or non-existing collections if zones and zone ranges have been defined for the collection. Initial creation and distribution of chunk allows for faster setup of zoned sharding.
How does the sharded cluster balancer work in MongoDB?
To address uneven chunk distribution for a sharded collection, the balancer migrates chunks from shards with more chunks to shards with a fewer number of chunks. The balancer migrates the chunks until there is an even distribution of chunks for the collection across the shards. For details about chunk migration, see Chunk Migration Procedure.
How big is a chunk in a sharded cluster?
The default chunk size for a sharded cluster is 64 megabytes. This default chunk size works well for most deployments; however, if you notice that automatic migrations have more I/O than your hardware can handle, you may want to reduce the chunk size.
Why is the _ ID field not part of the shard key?
Because the _id key is not part of the shard key, the collection could have a document with _id value 1 in shard A and another document with _id value 1 in shard B. If the _id field is not the shard key nor the prefix of the shard key, MongoDB expects applications to enforce the uniqueness of the _id values across the shards.
Why are chunks in MongoDB become indivisible?
If the majority of documents contain only a subset of those values, then the chunks storing those documents become a bottleneck within the cluster. Furthermore, as those chunks grow, they may become indivisible chunks as they cannot be split any further. This reduces or removes the effectiveness of horizontal scaling within the cluster.
How are sharded collections distributed in a MongoDB cluster?
Sharded collections are partitioned and distributed across the shards in the cluster. Unsharded collections are stored on a primary shard. Each database has its own primary shard. You must connect to a mongos router to interact with any collection in the sharded cluster.
Can a client connect to more than one shard in Mongo?
Clients should never connect to a single shard in order to perform read or write operations. You can connect to a mongos the same way you connect to a mongod, such as via the mongo shell or a MongoDB driver. MongoDB supports two sharding strategies for distributing data across sharded clusters.
When to create a supporting Index in MongoDB?
When sharding an empty collection, MongoDB creates the supporting index if the collection does not already have an appropriate index for the specified shard key. See Shard Key Indexes.