Why DynamoDB pairs with Lambda - Performance Analysis
When using DynamoDB with Lambda, it's important to understand how the time to complete tasks changes as data grows.
We want to know how the number of operations grows when Lambda reads or writes data in DynamoDB.
Analyze the time complexity of this Lambda function querying DynamoDB.
const AWS = require('aws-sdk');
const dynamodb = new AWS.DynamoDB.DocumentClient();
exports.handler = async (event) => {
const params = {
TableName: 'Users',
KeyConditionExpression: 'userId = :id',
ExpressionAttributeValues: { ':id': event.userId }
};
return await dynamodb.query(params).promise();
};
This Lambda function queries the DynamoDB table for items matching a user ID.
Look for repeated work inside the query process.
- Primary operation: DynamoDB scans or queries items matching the key.
- How many times: Depends on number of matching items; Lambda runs once per event.
As the number of matching items grows, the query takes longer.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 item reads |
| 100 | 100 item reads |
| 1000 | 1000 item reads |
Pattern observation: The time grows roughly in direct proportion to the number of items returned.
Time Complexity: O(n)
This means the time to complete the query grows linearly with the number of matching items.
[X] Wrong: "Lambda always runs instantly regardless of data size."
[OK] Correct: Lambda's runtime depends on how much data DynamoDB returns; more data means more time.
Understanding how Lambda and DynamoDB work together helps you design efficient serverless apps that scale well.
"What if the Lambda function used a DynamoDB scan instead of a query? How would the time complexity change?"
Practice
Solution
Step 1: Understand DynamoDB's role
DynamoDB stores data quickly and scales automatically without needing servers.Step 2: Understand Lambda's role
Lambda runs code automatically in response to events, such as changes in DynamoDB data.Final Answer:
Because Lambda can automatically run code when DynamoDB data changes -> Option AQuick Check:
Event-driven code execution = C [OK]
- Thinking DynamoDB needs Lambda to store data
- Confusing Lambda as a database
- Believing both are relational databases
Solution
Step 1: Identify how DynamoDB triggers Lambda
DynamoDB Streams capture table changes and can be linked to Lambda as event sources.Step 2: Evaluate options
Only setting up a DynamoDB Stream and attaching it to Lambda is correct; others are invalid or impossible.Final Answer:
Set up a DynamoDB Stream and attach it as an event source to the Lambda function -> Option BQuick Check:
DynamoDB Stream triggers Lambda = A [OK]
- Assuming Lambda can be called directly by DynamoDB without streams
- Thinking Lambda polls DynamoDB instead of event-driven
- Believing DynamoDB runs Lambda code internally
exports.handler = async (event) => {
for (const record of event.Records) {
if (record.eventName === 'INSERT') {
console.log('New item:', record.dynamodb.NewImage);
}
}
};Solution
Step 1: Understand DynamoDB Stream event structure
INSERT events include NewImage with the new item's attributes and their DynamoDB types.Step 2: Analyze the Lambda code
The code logs the NewImage object for INSERT events, so it outputs the full item with types.Final Answer:
Logs the new item data with keys and DynamoDB types -> Option DQuick Check:
INSERT event logs NewImage = B [OK]
- Assuming NewImage is undefined for INSERT
- Expecting only raw values without DynamoDB types
- Thinking INSERT events are ignored
exports.handler = async (event) => {
console.log('Event received');
};
Options:Solution
Step 1: Check event source configuration
Lambda triggers on DynamoDB Streams only if the stream is enabled and linked as an event source.Step 2: Evaluate code and invocation
Code with async and console.log is valid; missing return or sync invocation does not stop triggering.Final Answer:
The DynamoDB Stream is not enabled or not attached as an event source -> Option AQuick Check:
Missing stream or event source = D [OK]
- Thinking async keyword blocks Lambda triggers
- Believing console.log stops execution
- Assuming return statement is required for triggers
Solution
Step 1: Identify event-driven update method
Using DynamoDB Streams to trigger Lambda on data changes is efficient and real-time.Step 2: Evaluate alternatives
Polling wastes resources, embedding code in DynamoDB is not possible, and DynamoDB triggers require Lambda.Final Answer:
Enable DynamoDB Streams, trigger Lambda on changes, and update the search index inside Lambda -> Option CQuick Check:
Event-driven updates with Streams + Lambda = A [OK]
- Choosing polling instead of event triggers
- Thinking DynamoDB can run code internally
- Ignoring the need for Lambda in triggers
Ready to prove it?
Pass the test and this pattern moves unknown
