0
0
DynamoDBquery~5 mins

Why DynamoDB pairs with Lambda - Performance Analysis

Choose your learning style9 modes available
Time Complexity: Why DynamoDB pairs with Lambda
O(n)
Understanding Time Complexity

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.

Scenario Under Consideration

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.

Identify Repeating Operations

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.
How Execution Grows With Input

As the number of matching items grows, the query takes longer.

Input Size (n)Approx. Operations
1010 item reads
100100 item reads
10001000 item reads

Pattern observation: The time grows roughly in direct proportion to the number of items returned.

Final Time Complexity

Time Complexity: O(n)

This means the time to complete the query grows linearly with the number of matching items.

Common Mistake

[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.

Interview Connect

Understanding how Lambda and DynamoDB work together helps you design efficient serverless apps that scale well.

Self-Check

"What if the Lambda function used a DynamoDB scan instead of a query? How would the time complexity change?"