0
0
MongoDBquery~10 mins

Hash-based sharding in MongoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Hash-based sharding
Start: Insert Document
Extract Shard Key Value
Apply Hash Function
Compute Hash Value
Modulo by Number of Shards
Determine Target Shard
Store Document in Target Shard
End
Documents are assigned to shards by hashing the shard key value, then using the hash to pick the shard.
Execution Sample
MongoDB
shardKey = "user_id"
doc = {"user_id": 12345, "name": "Alice"}
hashValue = hash(doc[shardKey])
shardNumber = hashValue % 3
store(doc, shardNumber)
This code hashes the user_id to decide which of 3 shards stores the document.
Execution Table
StepActionInputHash ValueModulo ResultTarget Shard
1Extract shard key valueuser_id=12345
2Apply hash function1234567890
3Modulo by number of shards67890 % 3678900
4Determine target shardModulo result=0Shard 0
5Store documentdoc in Shard 0Shard 0
💡 Document stored in Shard 0 based on hash modulo 3 result
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
shardKey"user_id""user_id""user_id""user_id""user_id""user_id"
doc{}{"user_id":12345,"name":"Alice"}{"user_id":12345,"name":"Alice"}{"user_id":12345,"name":"Alice"}{"user_id":12345,"name":"Alice"}{"user_id":12345,"name":"Alice"}
hashValue67890678906789067890
shardNumber000
Key Moments - 3 Insights
Why do we use a hash function on the shard key value?
The hash function converts the shard key into a number that evenly distributes documents across shards, as shown in execution_table step 2.
What does the modulo operation do in hash-based sharding?
Modulo limits the hash value to the number of shards, deciding the exact shard to store the document, as seen in execution_table step 3.
Can two different shard key values end up in the same shard?
Yes, different keys can hash to values that modulo to the same shard number, so multiple keys can be stored in one shard (execution_table steps 3 and 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the hashValue after applying the hash function?
A12345
B67890
C0
D3
💡 Hint
Check the 'Hash Value' column at step 2 in the execution_table.
At which step is the target shard determined?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look for the step where 'Target Shard' column is filled in execution_table.
If the number of shards changes from 3 to 4, how does the modulo result change for the same hashValue?
AIt becomes 1
BIt remains 0
CIt becomes 2
DIt becomes 3
💡 Hint
Calculate 67890 % 4 and compare with the modulo result in execution_table step 3.
Concept Snapshot
Hash-based sharding assigns documents to shards by:
1. Extracting the shard key value.
2. Applying a hash function to it.
3. Using modulo by shard count to pick the shard.
This evenly distributes data and balances load.
Full Transcript
Hash-based sharding in MongoDB works by taking the shard key from a document, applying a hash function to convert it into a numeric hash value, then using modulo division by the number of shards to decide which shard stores the document. This method helps distribute data evenly across shards. The execution steps show extracting the shard key 'user_id' with value 12345, hashing it to 67890, then modulo 3 gives 0, so the document is stored in Shard 0. Variables like shardKey, doc, hashValue, and shardNumber change step by step as the document moves through the process. Key points include why hashing is used for distribution, how modulo picks the shard, and that different keys can map to the same shard. The quiz questions check understanding of hash values, step order, and effects of changing shard count.