Key-value and document store model in DynamoDB - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using DynamoDB's key-value and document store model, it's important to understand how the time to get or put data changes as the amount of data grows.
We want to know: How does the time to find or save an item change when the database gets bigger?
Analyze the time complexity of the following DynamoDB GetItem operation.
const params = {
TableName: "Users",
Key: { "UserId": "12345" }
};
dynamodb.get(params, function(err, data) {
if (err) console.log(err);
else console.log(data.Item);
});
This code fetches a single user item by its unique key from the DynamoDB table.
In this example, there are no loops or repeated scans over multiple items.
- Primary operation: Direct lookup by key.
- How many times: Exactly once per request.
Because DynamoDB uses a key to directly find the item, the time to get the item stays about the same no matter how many items are in the table.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 lookup |
| 100 | 1 lookup |
| 1000 | 1 lookup |
Pattern observation: The number of operations does not increase as the table grows.
Time Complexity: O(1)
This means the time to get an item by key stays constant, no matter how big the database is.
[X] Wrong: "Getting an item by key takes longer as the table gets bigger because there are more items to look through."
[OK] Correct: DynamoDB uses an index on the key to jump directly to the item, so it does not scan through all items.
Understanding that key-value lookups are constant time helps you explain how databases handle large data efficiently. This skill shows you know how data retrieval scales in real systems.
"What if we used a Scan operation instead of GetItem? How would the time complexity change?"
Practice
Solution
Step 1: Understand key-value store basics
A key-value store uses a unique key to store and retrieve data quickly without complex relations.Step 2: Compare options with key-value features
Options A, C, and D describe relational or fixed schema models, not key-value stores.Final Answer:
It uses a unique key to quickly find data. -> Option AQuick Check:
Key-value store = unique key fast access [OK]
- Confusing key-value with relational databases
- Thinking key-value needs fixed columns
- Assuming key-value supports joins
Solution
Step 1: Recall DynamoDB primary key syntax
DynamoDB primary key can be simple (partition key) or composite (partition + sort key) defined as an object with keys PartitionKey and SortKey.Step 2: Check each option's syntax
PrimaryKey: { PartitionKey: 'UserId', SortKey: 'Timestamp' } correctly shows both PartitionKey and SortKey in an object. PrimaryKey: { PartitionKey: 'UserId' } misses SortKey, B uses wrong assignment syntax, D uses ForeignKey which is invalid in DynamoDB.Final Answer:
PrimaryKey: { PartitionKey: 'UserId', SortKey: 'Timestamp' } -> Option AQuick Check:
Primary key = PartitionKey + SortKey object [OK]
- Using equal sign instead of colon in definitions
- Confusing foreign key with sort key
- Omitting sort key when needed
Solution
Step 1: Understand query by primary key
Querying by UserId '123' returns the item with that key if it exists.Step 2: Match UserId '123' in given items
The item {UserId: '123', Name: 'Alice'} matches the query.Final Answer:
[{UserId: '123', Name: 'Alice'}] -> Option DQuick Check:
Query by key returns matching item [OK]
- Expecting multiple items for a unique key
- Confusing query result with scan result
- Assuming error if item not found instead of empty
Table.query(KeyConditionExpression='UserId = :uid', ExpressionAttributeValues={':uid': '789'}). What is the likely problem?Solution
Step 1: Check key name correctness
If the key name 'UserId' does not exist or is misspelled in the table schema, the query returns no results.Step 2: Validate syntax and parameters
Using ':' in ExpressionAttributeValues keys is correct. '=' is valid in KeyConditionExpression. ScanIndexForward is optional for sorting, not required for results.Final Answer:
The key name 'UserId' is incorrect or missing in the table. -> Option CQuick Check:
Correct key name needed for query success [OK]
- Removing colons from ExpressionAttributeValues keys
- Using '==' instead of '=' in expressions
- Adding unnecessary parameters
Solution
Step 1: Identify data flexibility needs
Nested and flexible data like addresses and preferences require a document model that supports JSON-like structures.Step 2: Match model to DynamoDB capabilities
DynamoDB supports document store model allowing nested objects. Relational and graph models are not native to DynamoDB. Simple key-value stores do not support nested data well.Final Answer:
Use a document store model to save JSON-like nested objects. -> Option BQuick Check:
Nested data = document store model [OK]
- Choosing relational model for flexible nested data
- Assuming key-value stores handle nested objects well
- Confusing graph databases with DynamoDB
