Lambda function with DynamoDB - Time & Space Complexity
When using a Lambda function to interact with DynamoDB, it's important to understand how the time it takes to run changes as the data grows.
We want to know how the number of operations grows when the database or input size increases.
Analyze the time complexity of the following code snippet.
const AWS = require('aws-sdk');
const dynamodb = new AWS.DynamoDB.DocumentClient();
exports.handler = async (event) => {
const params = {
TableName: 'Users',
Key: { userId: event.userId }
};
const data = await dynamodb.get(params).promise();
return data.Item;
};
This Lambda function fetches a single user record from DynamoDB by its userId key.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: A single DynamoDB GetItem request to fetch one record.
- How many times: Exactly once per Lambda invocation.
Since the function fetches one item by key, the time does not increase with the total number of items in the table.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 Get request |
| 100 | 1 Get request |
| 1000 | 1 Get request |
Pattern observation: The number of operations stays the same no matter how many records are in the table.
Time Complexity: O(1)
This means the time to get one item stays constant, no matter how big the database grows.
[X] Wrong: "Getting an item from DynamoDB takes longer as the table gets bigger."
[OK] Correct: DynamoDB uses keys to directly find items, so fetching one item by key is fast and does not slow down with more data.
Understanding how database calls scale helps you write efficient serverless functions and shows you know how to handle data growth gracefully.
"What if the Lambda function scanned the entire table instead of getting by key? How would the time complexity change?"
Practice
Solution
Step 1: Understand Lambda triggers
Lambda functions can be triggered by events, such as changes in DynamoDB tables.Step 2: Identify the purpose of automation
This allows automatic code execution when data changes, without manual intervention.Final Answer:
To run code automatically when DynamoDB data changes -> Option AQuick Check:
Lambda triggers = automatic code run [OK]
- Thinking Lambda manually updates tables
- Confusing Lambda with backup tools
- Assuming Lambda creates tables automatically
Solution
Step 1: Recall correct DocumentClient syntax
The AWS SDK requires using 'new' keyword to create a DocumentClient instance.Step 2: Match correct class name and usage
The correct class is AWS.DynamoDB.DocumentClient, not DynamoDBClient or Document.Final Answer:
const docClient = new AWS.DynamoDB.DocumentClient(); -> Option BQuick Check:
Use 'new' with DocumentClient class [OK]
- Omitting 'new' keyword
- Using wrong class name like DynamoDBClient
- Calling Document() instead of DocumentClient
const AWS = require('aws-sdk');
const docClient = new AWS.DynamoDB.DocumentClient();
exports.handler = async (event) => {
const params = {
TableName: 'Users',
Key: { id: '123' }
};
const data = await docClient.get(params).promise();
return data.Item;
};Solution
Step 1: Understand docClient.get usage
The get method retrieves a single item by key from the specified table.Step 2: Analyze the return value
If the item with id '123' exists, data.Item will contain that item object.Final Answer:
The item object with id '123' from the Users table -> Option DQuick Check:
docClient.get returns item if found [OK]
- Expecting a list instead of single item
- Thinking get method is invalid
- Assuming empty object if item exists
const AWS = require('aws-sdk');
const docClient = new AWS.DynamoDB.DocumentClient();
exports.handler = async (event) => {
const params = {
TableName: 'Products',
Item: {
productId: '001',
name: 'Book'
}
};
await docClient.putItem(params).promise();
return 'Success';
};Solution
Step 1: Check DocumentClient method names
DocumentClient uses 'put' method, not 'putItem'.Step 2: Confirm correct method usage
Using 'putItem' causes a runtime error because it does not exist on DocumentClient.Final Answer:
Using 'putItem' instead of 'put' method on DocumentClient -> Option AQuick Check:
Use 'put' to write items with DocumentClient [OK]
- Confusing low-level DynamoDB client with DocumentClient
- Assuming 'putItem' exists in DocumentClient
- Ignoring method naming conventions
Solution
Step 1: Understand conditional update in DynamoDB
To update only if an item exists, use ConditionExpression with 'attribute_exists' on the key.Step 2: Apply ConditionExpression in update method
docClient.update supports ConditionExpression to prevent overwriting non-existing items.Step 3: Evaluate other options
Using put without condition overwrites or creates new items; delete before update is inefficient; get then put is not atomic.Final Answer:
Use docClient.update with ConditionExpression 'attribute_exists(id)' -> Option CQuick Check:
Conditional update = update + attribute_exists [OK]
- Using put which overwrites without condition
- Deleting item before update unnecessarily
- Not using atomic conditional update
Ready to prove it?
Pass the test and this pattern moves unknown
