Idempotency tokens in DynamoDB - Time & Space Complexity
When using idempotency tokens in DynamoDB, we want to understand how the time to process requests changes as more requests come in.
We ask: How does the system handle repeated requests efficiently without extra cost?
Analyze the time complexity of this DynamoDB operation using an idempotency token.
// PutItem with idempotency token check
const params = {
TableName: "Orders",
Item: {
"OrderId": { S: "123" },
"IdempotencyToken": { S: "token-abc" },
"Details": { S: "Order details" }
},
ConditionExpression: "attribute_not_exists(IdempotencyToken)"
};
await dynamodb.putItem(params).promise();
This code tries to insert an order only if the idempotency token is new, preventing duplicates.
Look for repeated checks or scans in the operation.
- Primary operation: Single conditional write checking if the token exists.
- How many times: Once per request, no loops or scans involved.
Each request checks the token once, regardless of how many tokens exist.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The time per request stays about the same, growing linearly with the number of requests but not with stored tokens.
Time Complexity: O(1)
This means each request takes about the same time, no matter how many tokens are stored.
[X] Wrong: "Checking idempotency tokens slows down as more tokens are stored because it scans the whole table."
[OK] Correct: The conditional write uses a direct key check, not a scan, so it stays fast even with many tokens.
Understanding how idempotency tokens keep operations fast helps you design reliable and efficient systems, a skill valued in many real-world projects.
What if we replaced the conditional write with a scan to find the token? How would the time complexity change?