Return values on write in DynamoDB - Time & Space Complexity
When we write data to DynamoDB and ask it to return values, the time it takes can change depending on what we ask back.
We want to understand how the cost grows when we ask DynamoDB to return different amounts of data after a write.
Analyze the time complexity of the following code snippet.
const params = {
TableName: "Users",
Key: { "UserId": "123" },
UpdateExpression: "set Age = :age",
ExpressionAttributeValues: { ":age": 30 },
ReturnValues: "ALL_NEW"
};
dynamodb.update(params, (err, data) => {
if (err) console.log(err);
else console.log(data.Attributes);
});
This code updates a user's age and asks DynamoDB to return all the new attributes after the update.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: DynamoDB reads the updated item attributes to return them.
- How many times: This happens once per write request.
When you ask DynamoDB to return more attributes after a write, it reads more data to send back.
| Input Size (attributes returned) | Approx. Operations |
|---|---|
| 10 | Reads 10 attributes |
| 100 | Reads 100 attributes |
| 1000 | Reads 1000 attributes |
Pattern observation: The work grows roughly in direct proportion to how many attributes you ask to be returned.
Time Complexity: O(n)
This means the time to return values after a write grows linearly with the number of attributes returned.
[X] Wrong: "Returning values after a write always takes the same time no matter how much data is returned."
[OK] Correct: Returning more attributes means DynamoDB reads and sends more data, so it takes more time.
Understanding how return values affect write time helps you explain trade-offs clearly and shows you know how database operations scale.
"What if we changed ReturnValues from ALL_NEW to NONE? How would the time complexity change?"