0
0
DynamoDBquery~10 mins

Write sharding in DynamoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Write sharding
Start Write Request
Determine Shard Key
Select Shard Table/Partition
Write Data to Selected Shard
Confirm Write Success
End Write Request
Write sharding splits data writes across multiple partitions or tables to spread load and improve performance.
Execution Sample
DynamoDB
function writeData(item) {
  const shardKey = hash(item.userId) % 3;
  const tableName = 'UserDataShard' + shardKey;
  dynamoDB.put({TableName: tableName, Item: item});
}
This code chooses a shard based on userId hash and writes the item to that shard's table.
Execution Table
StepInput Item userIdShard Key CalculationSelected Shard TableWrite ActionResult
1userId=101hash(101)%3=2UserDataShard2Write item to UserDataShard2Success
2userId=205hash(205)%3=1UserDataShard1Write item to UserDataShard1Success
3userId=309hash(309)%3=0UserDataShard0Write item to UserDataShard0Success
4userId=412hash(412)%3=1UserDataShard1Write item to UserDataShard1Success
5userId=523hash(523)%3=2UserDataShard2Write item to UserDataShard2Success
6----All writes completed successfully
💡 All items written to their respective shards; no more writes pending.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
shardKey-21012N/A
tableName-UserDataShard2UserDataShard1UserDataShard0UserDataShard1UserDataShard2N/A
Key Moments - 2 Insights
Why do we use hash(userId) % 3 to select the shard?
Using hash(userId) % 3 evenly distributes writes across 3 shards, preventing overload on a single shard as shown in execution_table rows 1-5.
What happens if we write all data to one shard?
All writes would go to one shard causing slow performance and possible throttling, unlike the balanced writes shown in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what shard table is selected for userId=309?
AUserDataShard1
BUserDataShard2
CUserDataShard0
DUserDataShard3
💡 Hint
Check row 3 in the execution_table under 'Selected Shard Table'
At which step does the shardKey equal 1 for the first time?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the 'Shard Key Calculation' column in execution_table rows 1-4
If we increase shards to 4, how would the shardKey calculation change?
Ahash(userId) * 4
Bhash(userId) % 4
Chash(userId) % 3
Dhash(userId) + 4
💡 Hint
Shard key uses modulo operation with number of shards as divisor, see code in execution_sample
Concept Snapshot
Write Sharding splits data writes across multiple shards.
Use a shard key (like hash(userId) % number_of_shards) to pick shard.
Write data to selected shard to balance load.
Improves write throughput and reduces bottlenecks.
Each shard is a separate partition or table.
Full Transcript
Write sharding in DynamoDB means splitting write requests across multiple shards or tables. Each write uses a shard key, often a hash of a unique identifier like userId, modulo the number of shards. This calculation decides which shard to write to. The write then goes to that shard's table or partition. This spreads the load evenly, avoiding bottlenecks. The execution table shows how different userIds map to different shards and writes succeed. Variables shardKey and tableName change per write to reflect the shard chosen. Beginners often wonder why modulo is used; it ensures even distribution. Also, writing all data to one shard causes slowdowns, which sharding prevents. The quiz checks understanding of shard selection and shard key calculation. The snapshot summarizes the key points for quick review.