MongoDB MongoDB Sharding and Horizontal Scaling 2 — Questions and Answers
Question 1: What is a chunk in MongoDB sharding?
- A compressed block of documents stored on disk
- A contiguous range of shard key values that is stored on a single shard (Correct answer)
- A batch of write operations sent to a shard
- A unit of replication between shards
Correct answer: A contiguous range of shard key values that is stored on a single shard
A chunk is a contiguous range of shard key values assigned to a specific shard. As data grows, MongoDB splits large chunks and the balancer migrates chunks between shards. The default chunk size is 128 MB in recent MongoDB versions.
Question 2: What is zone sharding (also called tag-aware sharding) in MongoDB?
- Automatically creating zones based on geographic IP addresses
- Associating ranges of shard key values with specific shards to control data placement (Correct answer)
- Encrypting data zones within a shard
- Partitioning data by collection size into separate zones
Correct answer: Associating ranges of shard key values with specific shards to control data placement
Zone sharding allows you to define ranges of shard key values (zones) and associate them with specific shards. This is used for data sovereignty (keeping data in a specific region), tiered storage, or workload isolation — ensuring certain documents always reside on designated shards.
Question 3: What is the minimum number of config servers required for a production MongoDB sharded cluster?
- 1
- 2
- 3 (Correct answer)
- 5
Correct answer: 3
Production MongoDB sharded clusters require 3 config servers deployed as a replica set (CSRS — Config Server Replica Set). Three config servers ensure high availability of cluster metadata. Using a single config server is only suitable for testing.
Question 4: What happens when you shard a collection in MongoDB that already has data?
- Existing documents are immediately distributed to all shards
- MongoDB creates an initial chunk and begins distributing data during subsequent balancer runs (Correct answer)
- The operation fails; sharding must be done on empty collections
- All existing documents are moved to shard 0
Correct answer: MongoDB creates an initial chunk and begins distributing data during subsequent balancer runs
When you enable sharding on an existing collection, MongoDB creates an initial chunk containing all existing documents on the primary shard. The balancer then gradually migrates chunks to other shards over time. Initial sync doesn't happen instantaneously.
Question 5: What is a jumbo chunk in MongoDB sharding and why is it a problem?
- A chunk larger than 1 GB that causes memory issues
- A chunk that cannot be split because all documents share the same shard key value, preventing rebalancing (Correct answer)
- A chunk that spans multiple shards simultaneously
- A chunk containing more than 10,000 documents
Correct answer: A chunk that cannot be split because all documents share the same shard key value, preventing rebalancing
A jumbo chunk forms when all documents within a chunk share the same shard key value, preventing further splitting. The balancer cannot move jumbo chunks, causing imbalanced data distribution. Choosing a high-cardinality shard key prevents jumbo chunk formation.
Question 6: Which command enables sharding for a specific database in MongoDB?
- db.enableSharding()
- sh.enableSharding('dbName') (Correct answer)
- mongos.shardDatabase('dbName')
- db.adminCommand({ shardCollection: 'dbName' })
Correct answer: sh.enableSharding('dbName')
sh.enableSharding('dbName') enables sharding at the database level, which is required before sharding individual collections. This is run via the mongos router. Then sh.shardCollection('dbName.collectionName', { shardKey: 1 }) shards a specific collection.
What is a chunk in MongoDB sharding?