WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

MongoDB · Intermediate · question 36 of 100

Explain the differences between hashed and ranged-based sharding in MongoDB.?

📕 Buy this interview preparation book: 100 MongoDB questions & answers — PDF + EPUB for $5

Sharding is a process where data is distributed across multiple servers or nodes in a MongoDB cluster in order to improve performance and scalability. In MongoDB, there are two main types of sharding: hashed and ranged-based.

**Hashed Sharding**

In hashed sharding, a hash function is used to evenly distribute the data across the shards. The shard key value is input to the hash function, and the resulting hash value determines which shard the data should be stored on. This means that hashed sharding can help ensure even data distribution and prevent hotspots, as each shard gets an equal amount of data.

For example, let’s say we have a database of customer information where the shard key is the customer’s last name. With hashed sharding, the last name would be input to a hash function, which would generate a hash value that determines which shard the data should be stored on. This means that all customer data would be evenly distributed across the shards.

While hashed sharding can help ensure even data distribution and prevent hotspots, it can make querying a bit more difficult, as queries that rely on the shard key can be slower due to the data being spread out across multiple shards.

**Range-based Sharding**

In ranged-based sharding, data is divided into ranges based on the shard key. Each shard is responsible for a specific range of values based on the shard key. This means that data that falls within a certain range will always be stored on the same shard.

For example, let’s say we have a database of hotel reservations where the shard key is the reservation date. With range-based sharding, we could divide the reservations into monthly ranges and assign each range to a specific shard. This means that all reservations for a specific month would always be stored on the same shard.

Range-based sharding can make querying faster, as queries that rely on the shard key can be executed on a single shard. However, it can lead to hotspots if data is not evenly distributed across the ranges or if new data is added to an already full range.

In summary, hashed sharding can help ensure even data distribution and prevent hotspots but can make querying a bit more difficult, while range-based sharding can make querying faster but can lead to hotspots if data is not evenly distributed. The choice between the two types of sharding largely depends on the specific use case and the characteristics of the data being stored.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic MongoDB interview — then scores it.
📞 Practice MongoDB — free 15 min
📕 Buy this interview preparation book: 100 MongoDB questions & answers — PDF + EPUB for $5

All 100 MongoDB questions · All topics