Lambda with DynamoDB Streams in AWS - Time & Space Complexity
When using Lambda with DynamoDB Streams, it's important to understand how the number of events affects processing time.
We want to know how the work grows as more data changes in the database.
Analyze the time complexity of the following operation sequence.
// Lambda function triggered by DynamoDB Stream events
exports.handler = async (event) => {
for (const record of event.Records) {
if (record.eventName === 'INSERT') {
// Process new item
await processNewItem(record.dynamodb.NewImage);
}
}
};
async function processNewItem(item) {
// Example processing logic
console.log('Processing item:', item);
}
This Lambda function processes each new item inserted into the DynamoDB table by reading stream records one by one.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Processing each record from the DynamoDB Stream inside the Lambda function.
- How many times: Once for every record in the event batch, which depends on the number of changes in DynamoDB.
As the number of new items inserted grows, the Lambda function processes more records one by one.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 processing calls |
| 100 | 100 processing calls |
| 1000 | 1000 processing calls |
Pattern observation: The number of processing steps grows directly with the number of stream records.
Time Complexity: O(n)
This means the processing time grows linearly with the number of new records in the stream.
[X] Wrong: "The Lambda function processes all records instantly regardless of how many there are."
[OK] Correct: Each record requires individual processing, so more records mean more work and longer total processing time.
Understanding how event-driven functions scale with input size helps you design efficient cloud applications and answer questions about system behavior under load.
"What if the Lambda function processed records in parallel instead of sequentially? How would the time complexity change?"
Practice
Solution
Step 1: Understand DynamoDB Streams role
DynamoDB Streams capture data changes in the table as events.Step 2: Understand Lambda integration
Lambda can be triggered automatically by these stream events to run code.Final Answer:
To automatically run code when data in the DynamoDB table changes -> Option BQuick Check:
Lambda triggers on stream events = A [OK]
- Thinking Lambda triggers manually from console
- Confusing streams with backups
- Assuming streams increase table capacity
Solution
Step 1: Identify how to enable streams
DynamoDB Streams are enabled by setting the stream specification on the table.Step 2: Choose correct stream view type
NEW_AND_OLD_IMAGES captures both before and after images of changes.Final Answer:
Set the stream specification to NEW_AND_OLD_IMAGES in the table settings -> Option CQuick Check:
Stream enabled via table settings = B [OK]
- Trying to enable streams by creating Lambda
- Confusing versioning with streams
- Changing capacity units instead of streams
def lambda_handler(event, context):
for record in event['Records']:
if record['eventName'] == 'INSERT':
print(record['dynamodb']['NewImage']['id']['S'])
What will this code print when a new item with id '123' is added to the DynamoDB table?
Solution
Step 1: Understand event structure for INSERT
For an INSERT event, 'NewImage' contains the new item data including 'id'.Step 2: Analyze code behavior
The code prints the string value of 'id' from 'NewImage' when eventName is 'INSERT'.Final Answer:
'123' printed once for the new item -> Option AQuick Check:
INSERT event prints new id value = D [OK]
- Assuming eventName is different
- Expecting OldImage on INSERT
- Confusing key name with value
Solution
Step 1: Check stream enablement
Lambda triggers on DynamoDB Streams events, so streams must be enabled on the table.Step 2: Consider other causes
While code errors or roles cause failures, the function would still be invoked if streams exist.Final Answer:
The DynamoDB table does not have streams enabled -> Option AQuick Check:
Streams must be enabled for Lambda trigger = C [OK]
- Assuming code errors prevent invocation
- Ignoring missing streams
- Confusing read capacity with triggers
Solution
Step 1: Choose stream view for updated data
NEW_IMAGE provides the new state after update, suitable for processing changes.Step 2: Filter events for updates only
Filtering on eventName 'MODIFY' ensures only update events trigger Lambda.Final Answer:
Enable NEW_IMAGE stream view and filter events where eventName is 'MODIFY' -> Option DQuick Check:
Updates = NEW_IMAGE + 'MODIFY' filter [OK]
- Using KEYS_ONLY which lacks attribute data
- Filtering on INSERT or REMOVE instead of MODIFY
- Choosing OLD_IMAGE which shows old data only
Ready to prove it?
Pass the test and this pattern moves unknown
