Serverless vs container decision in AWS - Performance Comparison
When choosing between serverless and containers, it's important to understand how the time to handle tasks grows as workload increases.
We want to see how the number of operations or API calls changes when we add more tasks.
Analyze the time complexity of deploying and running tasks using serverless functions versus containers.
// Serverless example
for each event in events:
invoke Lambda function(event)
// Container example
for each task in tasks:
schedule task on ECS cluster
This sequence shows invoking a serverless function for each event versus scheduling tasks on a container cluster.
Look at what repeats as workload grows.
- Primary operation: Invoking a Lambda function or scheduling a container task.
- How many times: Once per event or task, so it grows with the number of events/tasks.
As the number of events or tasks increases, the number of function invocations or container schedules increases proportionally.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 Lambda invocations or 10 container task schedules |
| 100 | 100 Lambda invocations or 100 container task schedules |
| 1000 | 1000 Lambda invocations or 1000 container task schedules |
Pattern observation: The number of operations grows directly with the number of tasks or events.
Time Complexity: O(n)
This means the time or number of operations grows linearly as the number of tasks or events increases.
[X] Wrong: "Serverless automatically handles all tasks instantly, so time doesn't grow with more events."
[OK] Correct: Each event still triggers a separate function call, so total operations increase with more events.
Understanding how workload size affects operations helps you explain design choices clearly and shows you grasp cloud scaling basics.
What if we batch multiple events into a single serverless function call? How would the time complexity change?
Practice
Solution
Step 1: Understand serverless concept
Serverless means you don't manage servers; code runs on demand.Step 2: Identify AWS service matching serverless
AWS Lambda runs code without server management and scales automatically.Final Answer:
AWS Lambda -> Option BQuick Check:
Serverless = AWS Lambda [OK]
- Choosing EC2 which requires server management
- Confusing containers with serverless
- Picking Elastic Beanstalk which manages servers but not fully serverless
aws ecs create-cluster --cluster-name myCluster
Solution
Step 1: Analyze the command
The command uses 'aws ecs create-cluster' which is for ECS clusters.Step 2: Match command to service
ECS is AWS service for managing containers in clusters.Final Answer:
Amazon ECS container cluster creation -> Option CQuick Check:
"aws ecs create-cluster" = ECS container cluster [OK]
- Thinking this command deploys Lambda functions
- Confusing S3 bucket commands with ECS
- Assuming CloudFormation syntax
Options:A) AWS Lambda
B) AWS CloudFront
C) Amazon S3
D) Amazon ECS with Fargate
Solution
Step 1: Identify app requirements
The app runs continuously and needs custom OS settings, so it needs control over environment.Step 2: Match service to requirements
AWS Lambda is event-driven and short-lived, not continuous. ECS with Fargate runs containers continuously with environment control.Final Answer:
Amazon ECS with Fargate -> Option DQuick Check:
Continuous app + custom OS = ECS Fargate [OK]
- Choosing Lambda for continuous workloads
- Confusing S3 (storage) or CloudFront (CDN) as compute
- Ignoring environment control needs
Solution
Step 1: Understand Lambda container support
Lambda supports container images but has a max execution time of 15 minutes.Step 2: Identify cause of failure
If container runs longer than 15 minutes, Lambda will fail the execution.Final Answer:
Lambda does not support running containers longer than 15 minutes -> Option AQuick Check:
Lambda container max time = 15 minutes [OK]
- Thinking Lambda cannot run containers at all
- Believing Lambda only supports Python code
- Confusing Dockerfile requirement
Solution
Step 1: Analyze startup needs
Startup wants to minimize costs and avoid managing servers, with unpredictable traffic.Step 2: Match AWS service to needs
AWS Lambda automatically scales with traffic and requires no server management, reducing cost and effort.Step 3: Compare other options
ECS with EC2 or Elastic Beanstalk requires server management or fixed capacity, increasing cost and complexity.Final Answer:
Deploy on AWS Lambda for automatic scaling and no server management -> Option AQuick Check:
Min cost + no servers + spikes = Lambda [OK]
- Choosing ECS or EC2 which need server management
- Assuming Elastic Beanstalk auto-scales without servers
- Ignoring cost and management factors
Ready to prove it?
Pass the test and this pattern moves unknown
