Condition keys for row-level security in DynamoDB - Time & Space Complexity
When using condition keys for row-level security in DynamoDB, we want to know how the time to check permissions grows as data grows.
We ask: How does adding more rows or conditions affect the time to verify access?
Analyze the time complexity of the following DynamoDB policy condition snippet.
{
"Condition": {
"ForAllValues:StringEquals": {
"dynamodb:LeadingKeys": ["${aws:username}"]
}
}
}
This snippet restricts access so users can only access rows where the partition key matches their username.
Look for repeated checks or loops in the condition evaluation.
- Primary operation: Comparing the requested partition key to the user's username.
- How many times: Once per request, independent of number of rows. DynamoDB uses the key to directly find rows.
Because DynamoDB uses the partition key to find rows, checking the condition does not scan all rows. The permission check happens once per request.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 (key comparison) |
| 100 | 1 (key comparison) |
| 1000 | 1 (key comparison) |
Pattern observation: The time is constant and does not depend on data size.
Time Complexity: O(1)
This means checking the condition per request is very fast and does not depend on total data size.
[X] Wrong: "The condition check scans all rows to find matches."
[OK] Correct: DynamoDB uses the partition key to directly access rows, so it does not scan all data.
Understanding how condition keys work helps you explain efficient data access and security in real systems.
"What if the condition used a non-key attribute instead of the partition key? How would the time complexity change?"