0
0
MongodbComparisonBeginner · 4 min read

Hashed vs Ranged Sharding in MongoDB: Key Differences and Usage

In MongoDB, hashed sharding distributes data evenly by hashing the shard key, which balances load but limits range queries. Ranged sharding partitions data by continuous ranges of the shard key, enabling efficient range queries but may cause uneven data distribution.
⚖️

Quick Comparison

This table summarizes the main differences between hashed and ranged sharding in MongoDB.

FactorHashed ShardingRanged Sharding
Data DistributionEvenly distributed using hash of shard keyPartitioned by continuous ranges of shard key
Query EfficiencyPoor for range queries, good for equality queriesExcellent for range queries on shard key
Load BalancingAutomatically balanced across shardsMay cause uneven shard sizes if data is skewed
Use CaseHigh write throughput with uniform key distributionRange queries and sorted data access
Chunk SplittingChunks split by hashed valuesChunks split by key ranges
ComplexitySimpler to manage due to automatic balancingRequires monitoring for hotspots and balancing
⚖️

Key Differences

Hashed sharding works by applying a hash function to the shard key's value. This spreads documents evenly across shards regardless of the original key order, which helps prevent hotspots and balances write load well. However, because the data is distributed by hash, range queries on the shard key are inefficient or impossible to target a subset of shards.

In contrast, ranged sharding divides data into chunks based on continuous ranges of the shard key. This allows efficient range queries and sorted scans because related data is stored together on the same shard. But if the shard key values are not uniformly distributed, some shards can become overloaded, causing uneven data and load distribution.

Choosing between them depends on your query patterns and data distribution. Hashed sharding is best when you want even load and your queries mostly target specific keys. Ranged sharding is better when you need to run range queries or sort data by the shard key.

⚖️

Code Comparison

Here is how to enable hashed sharding on a collection in MongoDB:

javascript
sh.enableSharding("myDatabase")
sh.shardCollection("myDatabase.myCollection", { "userId": "hashed" })
Output
Shard collection myDatabase.myCollection on { userId: "hashed" }
↔️

Ranged Sharding Equivalent

Here is how to enable ranged sharding on the same collection using a range shard key:

javascript
sh.enableSharding("myDatabase")
sh.shardCollection("myDatabase.myCollection", { "userId": 1 })
Output
Shard collection myDatabase.myCollection on { userId: 1 }
🎯

When to Use Which

Choose hashed sharding when you want to evenly distribute writes and your queries mostly target specific shard key values without range scans. It is ideal for workloads with high write throughput and uniform key distribution.

Choose ranged sharding when your application requires efficient range queries, sorted data access, or queries that scan ranges of the shard key. Be prepared to monitor and balance shards if data distribution is skewed.

Key Takeaways

Hashed sharding evenly distributes data but limits efficient range queries.
Ranged sharding supports efficient range queries but may cause uneven shard load.
Use hashed sharding for uniform key distribution and high write throughput.
Use ranged sharding when range queries or sorted access on shard key are needed.
Monitor shard balance closely when using ranged sharding to avoid hotspots.