Lambda execution model in AWS - Time & Space Complexity
When using AWS Lambda, it's important to understand how the time to handle requests grows as more requests come in.
We want to know how the number of Lambda executions changes when the number of incoming events increases.
Analyze the time complexity of invoking Lambda functions for multiple events.
// Example: Processing multiple events with Lambda
for (const event of events) {
const response = await lambda.invoke({
FunctionName: 'MyFunction',
Payload: JSON.stringify(event)
}).promise();
// process response
}
This code invokes a Lambda function once for each event in a list, processing them one by one.
Look at what repeats as the input grows.
- Primary operation: Lambda function invocation API call
- How many times: Once per event in the input list
Each new event causes one Lambda invocation, so the total calls grow directly with the number of events.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 10 Lambda invocations |
| 100 | 100 Lambda invocations |
| 1000 | 1000 Lambda invocations |
Pattern observation: The number of Lambda calls grows in a straight line with the number of events.
Time Complexity: O(n)
This means the total Lambda invocations increase directly in proportion to the number of events.
[X] Wrong: "Lambda can handle all events with just one invocation regardless of input size."
[OK] Correct: Each event usually triggers a separate Lambda invocation, so more events mean more calls, not just one.
Understanding how Lambda scales with input helps you design systems that handle growing workloads smoothly and predict costs better.
"What if we batch multiple events into a single Lambda invocation? How would the time complexity change?"
Practice
Solution
Step 1: Understand Lambda's event-driven model
A Lambda function runs only when triggered by an event, such as an API call or file upload.Step 2: Consider billing and execution behavior
If no event triggers the function, it does not execute and you are not charged.Final Answer:
The function does not run and no charges occur. -> Option AQuick Check:
Lambda runs only on events = C [OK]
- Thinking Lambda runs continuously without events
- Assuming Lambda runs on a schedule by default
- Believing manual start is required for all executions
Solution
Step 1: Recall Python Lambda handler signature
In Python, the handler must accept two parameters: event and context.Step 2: Check syntax correctness
def handler(event, context): return 'Hello World' correctly defines a Python function with event and context and returns a string.Final Answer:
def handler(event, context): return 'Hello World' -> Option DQuick Check:
Python handler(event, context) = A [OK]
- Using JavaScript syntax in Python handler
- Omitting context parameter
- Defining handler without parameters
def handler(event, context):
return event['value'] * 2What is the output if the event is
{"value": 5}?Solution
Step 1: Understand the handler logic
The handler multiplies the 'value' from event by 2.Step 2: Calculate output for event {'value': 5}
5 * 2 = 10, so the function returns 10.Final Answer:
10 -> Option CQuick Check:
5 times 2 equals 10 [OK]
- Confusing event keys or missing them
- Assuming function returns input unchanged
- Expecting error when key exists
def handler(event, context):
return event['data'] * 3What is the likely cause?
Solution
Step 1: Identify cause of error in code
The function tries to access event['data'], which may not exist.Step 2: Understand error from missing key
If 'data' key is missing in event, a KeyError occurs causing the function to fail.Final Answer:
The event dictionary does not have the 'data' key. -> Option BQuick Check:
Missing event key causes error = B [OK]
- Assuming context parameter missing causes error
- Thinking return syntax is wrong
- Believing Lambda cannot do arithmetic
Solution
Step 1: Understand Lambda concurrency
Lambda automatically scales by running multiple instances to handle events concurrently.Step 2: Eliminate incorrect options
Manual start and single-threaded models do not support concurrency; scheduled triggers queue events but don't scale automatically.Final Answer:
Automatic scaling with multiple concurrent executions -> Option AQuick Check:
Lambda auto-scales for concurrency = D [OK]
- Thinking manual start is needed for concurrency
- Confusing scheduling with scaling
- Assuming Lambda runs single-threaded only
Ready to prove it?
Pass the test and this pattern moves unknown
