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?"