MongoDB MongoDB Sharding and Horizontal Scaling 1 — Questions and Answers
Question 1: What are the three main components of a MongoDB sharded cluster?
- Primary, secondary, arbiter
- Mongos, config servers, and shards (Correct answer)
- Router, coordinator, and replica
- Balancer, indexer, and data node
Correct answer: Mongos, config servers, and shards
A MongoDB sharded cluster consists of: (1) mongos — the query router that clients connect to; (2) config servers — store cluster metadata and chunk distribution; and (3) shards — each shard is a replica set holding a subset of the sharded data.
Question 2: What is a shard key in MongoDB sharding?
- A unique index on every shard
- A field or combination of fields that determines how documents are distributed across shards (Correct answer)
- An encryption key for shard-level data security
- A password for shard authentication
Correct answer: A field or combination of fields that determines how documents are distributed across shards
The shard key is a field (or compound fields) whose values determine which shard stores each document. MongoDB divides the shard key value range into chunks and distributes these chunks across shards. Choosing the right shard key is critical for balanced distribution and query efficiency.
Question 3: What is a monotonically increasing shard key and why is it problematic?
- A shard key with random values that causes uneven distribution
- A shard key like a timestamp or auto-increment ID where all new inserts go to the same 'max' chunk, creating a hot spot (Correct answer)
- A shard key that can only be used on a single shard
- A shard key that changes over time causing documents to migrate
Correct answer: A shard key like a timestamp or auto-increment ID where all new inserts go to the same 'max' chunk, creating a hot spot
A monotonically increasing shard key (e.g., ObjectId, timestamp) causes all new inserts to go to the shard holding the upper-bound chunk, creating a write hot spot. This shard receives all write traffic while others sit idle. Hashed shard keys or compound keys solve this problem.
Question 4: What is the role of the balancer in a MongoDB sharded cluster?
- Routing client queries to the correct shard
- Automatically moving chunks between shards to maintain even data distribution (Correct answer)
- Managing replica set elections within each shard
- Compressing data across shards
Correct answer: Automatically moving chunks between shards to maintain even data distribution
The MongoDB balancer is a background process that monitors chunk counts per shard. When imbalances exceed a threshold, it migrates chunks from overloaded shards to underloaded ones. Migrations occur automatically and transparently to the application.
Question 5: What is a targeted (routed) query vs a scatter-gather query in a sharded cluster?
- A targeted query uses indexes; a scatter-gather query does not
- A targeted query includes the shard key and goes to one shard; a scatter-gather query broadcasts to all shards (Correct answer)
- A targeted query reads from secondaries; scatter-gather reads from primaries
- A targeted query is synchronous; scatter-gather is asynchronous
Correct answer: A targeted query includes the shard key and goes to one shard; a scatter-gather query broadcasts to all shards
A targeted query includes the shard key in the filter, allowing mongos to route it to exactly one (or a few) shard(s). A scatter-gather query lacks the shard key and must be broadcast to all shards, with results merged at mongos — much more expensive at scale.
Question 6: What is a hashed shard key in MongoDB and when should you use it?
- A shard key encrypted for security purposes
- A shard key where MongoDB hashes the field values to distribute documents evenly, ideal for monotonically increasing fields (Correct answer)
- A compound shard key using a hash of multiple fields
- A shard key that uses SHA-256 for uniqueness guarantees
Correct answer: A shard key where MongoDB hashes the field values to distribute documents evenly, ideal for monotonically increasing fields
A hashed shard key applies a hash function to the shard key values before distributing documents. This ensures even distribution even for monotonically increasing fields (like ObjectId or timestamps), preventing hot spots. The trade-off is that range queries are inefficient.
What are the three main components of a MongoDB sharded cluster?