0
0
DynamoDBquery~30 mins

Write sharding in DynamoDB - Mini Project: Build & Apply

Choose your learning style9 modes available
Write Sharding in DynamoDB
📖 Scenario: You are building a high-traffic web application that stores user activity logs in DynamoDB. To handle many simultaneous writes without slowing down, you want to distribute the writes evenly across multiple partitions using write sharding.
🎯 Goal: Create a DynamoDB table schema and implement write sharding by assigning shard IDs to distribute writes evenly. You will set up the data structure, configure the number of shards, write the logic to assign shard IDs, and finalize the table key design.
📋 What You'll Learn
Create a DynamoDB table schema with a partition key and sort key
Define a variable for the number of shards
Write logic to assign a shard ID to each write based on user ID
Complete the table key design to include the shard ID for even write distribution
💡 Why This Matters
🌍 Real World
Write sharding helps large-scale applications handle many simultaneous writes by spreading data across multiple partitions, preventing bottlenecks.
💼 Career
Understanding write sharding is important for backend developers and database engineers working with NoSQL databases like DynamoDB to optimize performance and scalability.
Progress0 / 4 steps
1
Create DynamoDB table schema
Create a DynamoDB table schema with a partition key called ShardId (string) and a sort key called UserId (string). Define the table name as UserActivity.
DynamoDB
Need a hint?

Use ShardId as the partition key and UserId as the sort key in your table schema.

2
Define number of shards
Define a variable called num_shards and set it to 10 to represent the total number of shards for write distribution.
DynamoDB
Need a hint?

Set num_shards to 10 to split writes into 10 partitions.

3
Assign shard ID for each write
Write a function called get_shard_id that takes a user_id string and returns a shard ID string by computing hash(user_id) % num_shards. The shard ID should be a string of the number.
DynamoDB
Need a hint?

Use the built-in hash() function and modulo operator to assign shard IDs.

4
Complete table key design with shard ID
Complete the write operation by using the ShardId from get_shard_id(user_id) as the partition key and UserId as the sort key in the item to write to DynamoDB.
DynamoDB
Need a hint?

Use the get_shard_id function to set the ShardId key in the item dictionary.