Cost estimation for access patterns in DynamoDB - Time & Space Complexity
When using DynamoDB, it is important to understand how the cost of accessing data grows as your data or requests increase.
We want to know how the number of operations changes when we use different ways to get data.
Analyze the time complexity of the following DynamoDB access pattern.
// Query items by partition key
const params = {
TableName: "Orders",
KeyConditionExpression: "CustomerId = :cid",
ExpressionAttributeValues: {
":cid": { S: "12345" }
}
};
const result = await dynamodb.query(params).promise();
This code fetches all orders for one customer using their ID as the partition key.
Look for repeated actions that affect cost.
- Primary operation: Reading items matching the partition key.
- How many times: Once per item returned in the query result.
As the number of orders for a customer grows, the work to fetch them grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 reads |
| 100 | 100 reads |
| 1000 | 1000 reads |
Pattern observation: The number of reads grows directly with the number of items returned.
Time Complexity: O(n)
This means the time to get data grows in a straight line with the number of matching items.
[X] Wrong: "Querying by partition key always takes the same time no matter how many items match."
[OK] Correct: The query reads each matching item, so more items mean more work and time.
Understanding how data access cost grows helps you design better queries and explain your choices clearly in real projects.
"What if we changed the query to use a filter after fetching all partition keys? How would the time complexity change?"
Practice
Solution
Step 1: Understand DynamoDB cost factors
DynamoDB charges mainly based on how many read and write requests you perform per second.Step 2: Identify the main cost driver
While backups and indexes can affect cost, the primary cost is from read/write capacity usage.Final Answer:
The number of read and write operations per second -> Option AQuick Check:
Cost depends on read/write operations [OK]
- Thinking table count directly affects cost
- Confusing backup size with operational cost
- Assuming indexes alone drive cost
Solution
Step 1: Recall RCU calculation for strongly consistent reads
One RCU allows reading up to 4 KB per second for strongly consistent reads.Step 2: Calculate RCUs for 3 KB item
Since 3 KB is less than 4 KB, it requires 1 RCU.Final Answer:
1 RCU per 3 KB read -> Option DQuick Check:
Strongly consistent read 4 KB = 1 RCU [OK]
- Confusing strongly consistent with eventually consistent reads
- Miscalculating item size rounding
- Assuming 1 RCU per KB
Solution
Step 1: Calculate RCUs per read for eventually consistent reads
One RCU allows reading 8 KB per second for eventually consistent reads, which is half the cost of strongly consistent reads.Step 2: Calculate RCUs needed for 6 KB item
6 KB item requires 2 RCUs for strongly consistent reads (since 6 KB > 4 KB, round up to 2). For eventually consistent reads, it costs half, so 1 RCU per read.Step 3: Calculate total RCUs for 100 reads
100 reads x 1 RCU = 100 RCUs.Step 4: Correct calculation
Since 6 KB is more than 4 KB, for eventually consistent reads, each read consumes ceil(6/4) = 2 RCUs * 0.5 = 1 RCU per read. So total 100 RCUs.Final Answer:
100 RCUs -> Option BQuick Check:
6 KB item eventually consistent read = 1 RCU per read [OK]
- Using strongly consistent read calculation for eventually consistent
- Not rounding up item size to nearest 4 KB
- Multiplying incorrectly by number of reads
Solution
Step 1: Understand WCU calculation for item size
One WCU allows writing 1 KB per second. Larger items require more WCUs, rounded up.Step 2: Identify problem with fixed 1 WCU per write
If item size > 1 KB, fixed 1 WCU underestimates needed capacity, causing throttling or errors.Final Answer:
Underestimating WCUs for items larger than 1 KB -> Option CQuick Check:
WCU depends on item size, fixed 1 WCU is wrong [OK]
- Assuming 1 WCU fits all item sizes
- Confusing read and write capacity units
- Ignoring item size when estimating capacity
Solution
Step 1: Calculate WCUs needed
Each 2 KB write requires 2 WCUs (ceil(2/1 KB)). 50 writes/sec x 2 = 100 WCUs.Step 2: Calculate RCUs needed
Each 3 KB strongly consistent read requires 1 RCU (ceil(3/4 KB)). 100 reads/sec x 1 = 100 RCUs.Step 3: Hourly cost
WCUs: 100 x $0.00065 = $0.065. RCUs: 100 x $0.00013 = $0.013. Total: $0.078/hr.Step 4: Monthly cost
$0.078 x 24 hrs/day x 30 days = $56.16.Final Answer:
$56.16 -> Option AQuick Check:
Calculate WCUs and RCUs x price x time [OK]
- Not rounding item size to nearest KB
- Mixing hourly and monthly rates
- Ignoring strongly consistent read cost
Ready to prove it?
Pass the test and this pattern moves unknown
