0
0
DynamoDBquery~5 mins

Write sharding in DynamoDB

Choose your learning style9 modes available
Introduction

Write sharding helps spread data writes across multiple partitions to avoid slowdowns. It keeps your database fast and responsive.

When many users write data at the same time to the same table.
When you want to avoid a single hotspot slowing down your database.
When your application needs to handle a large number of writes per second.
When you want to improve write throughput by distributing load.
When you want to scale your database writes smoothly as your app grows.
Syntax
DynamoDB
PartitionKey = BaseKey + ShardId
ShardId = random number or hash value to spread writes
Use a shard identifier appended or prepended to your base key to distribute writes.
Shards can be numbers or strings chosen to balance load evenly.
Examples
This example adds a shard number to the user ID to spread writes across shards.
DynamoDB
PartitionKey = "User#" + UserId + "#Shard" + ShardNumber
Using a hash function and modulo operator to assign items to shards evenly.
DynamoDB
PartitionKey = hash(ItemId) % NumberOfShards
Sample Program

This inserts an order record with a shard ID in the partition key to distribute writes.

DynamoDB
aws dynamodb put-item --table-name DynamoDBTable --item '{"PartitionKey": {"S": "Order#123#Shard1"}, "SortKey": {"S": "2024-06-01"}, "Data": {"S": "Order details here"}}'
OutputSuccess
Important Notes

Choose the number of shards based on your expected write volume.

Too few shards can cause hotspots; too many can complicate queries.

Write sharding requires your application to know how to generate shard keys.

Summary

Write sharding spreads write load by adding shard IDs to partition keys.

This helps avoid slowdowns from too many writes on one partition.

It improves database performance and scalability for heavy write workloads.