Write sharding in DynamoDB - Time & Space Complexity
When we write data to a DynamoDB table using sharding, we split the data across multiple partitions.
We want to understand how the time to write grows as we add more data and shards.
Analyze the time complexity of this write sharding example.
// Pseudocode for write sharding in DynamoDB
for each item in data:
shard_key = hash(item.id) % number_of_shards
dynamodb.put_item(TableName, Item with shard_key)
This code writes each item to a shard determined by hashing its ID.
Look at what repeats as data grows.
- Primary operation: Writing each item to DynamoDB with a shard key.
- How many times: Once per item in the data set.
As the number of items grows, the number of writes grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 writes to shards |
| 100 | 100 writes to shards |
| 1000 | 1000 writes to shards |
Pattern observation: The number of write operations grows directly with the number of items.
Time Complexity: O(n)
This means the time to write grows linearly as you add more items, even when using shards.
[X] Wrong: "Using more shards makes writing faster and reduces time complexity to constant."
[OK] Correct: While shards help distribute load, you still write each item once, so total time grows with data size.
Understanding how sharding affects write time helps you explain scaling strategies clearly and confidently.
What if we batch multiple writes together per shard? How would that change the time complexity?
Practice
Solution
Step 1: Understand write sharding concept
Write sharding adds shard IDs to partition keys to distribute writes evenly.Step 2: Identify the main goal
This distribution avoids hot partitions and improves write performance.Final Answer:
To spread write load across multiple partitions by adding shard IDs -> Option BQuick Check:
Write sharding = spreading writes [OK]
- Confusing write sharding with read caching
- Thinking it encrypts data
- Assuming it handles backups
Solution
Step 1: Recall write sharding method
Write sharding requires adding a shard ID to the partition key to distribute writes.Step 2: Identify correct implementation
Appending a random shard ID to the partition key spreads writes across partitions.Final Answer:
Append a random shard ID to the partition key before writing -> Option CQuick Check:
Shard ID + partition key = write sharding [OK]
- Not adding shard ID at all
- Using sort key as shard ID incorrectly
- Writing all data to one partition
shard_id = random.randint(0, 4)
partition_key = f"user#{user_id}#{shard_id}"
dynamodb_table.put_item(Item={"PK": partition_key, "Data": data})Solution
Step 1: Analyze shard ID usage
The shard_id is a random number from 0 to 4, appended to the partition key.Step 2: Understand partition key effect
This creates 5 different partition keys for the same user, spreading writes across 5 partitions.Final Answer:
Writes are distributed across 5 partitions reducing hot spots -> Option AQuick Check:
Random shard ID 0-4 = 5 partitions [OK]
- Assuming all writes go to one partition
- Thinking partition key format is invalid
- Believing data is restricted by shard ID
Solution
Step 1: Check shard ID range
If the shard ID range is too small, writes cluster on few partitions causing throttling.Step 2: Verify correct key usage
Shard ID must be part of the partition key, not sort key, and must cover enough range.Final Answer:
Shard ID range is too small causing uneven distribution -> Option AQuick Check:
Small shard range = hot partitions [OK]
- Adding shard ID to sort key instead of partition key
- Not updating partition key with shard ID
- Believing DynamoDB disallows sharding
Solution
Step 1: Evaluate write sharding options
Adding a random shard ID (0-9) to partition key spreads writes across 10 partitions.Step 2: Consider read strategy
Reads require querying all shards and aggregating results, which is manageable for balanced load.Step 3: Compare other options
Using no sharding risks throttling; shard ID in sort key does not distribute writes; single partition causes hot spots.Final Answer:
Add a random shard ID (0-9) to the partition key and store user data in each shard; aggregate reads by querying all shards -> Option DQuick Check:
Shard ID in partition key + aggregate reads = balanced writes [OK]
- Not sharding partition key causing hot partitions
- Adding shard ID to sort key only
- Ignoring read complexity after sharding
Ready to prove it?
Pass the test and this pattern moves unknown
