
AWS Lambda offers two primary concurrency controlsโreserved concurrency and provisioned concurrencyโeach serving distinct purposes. Here’s a detailed comparison to help you understand their differences and use cases:(AWS Documentation)
๐น Reserved Concurrency
Purpose: Guarantees a specific number of concurrent executions for a function, ensuring it has dedicated capacity.
Key Characteristics:
- Fixed Allocation: Allocates a set number of concurrent executions exclusively to the function.
- No Additional Cost: Configuring reserved concurrency incurs no extra charges.
- Impact on Account Limit: The reserved concurrency reduces the total available concurrency for other functions in your account.
- No Auto-Scaling: Does not automatically adjust based on traffic patterns.(AWS Documentation)
Use Cases:
- Ensuring critical functions always have the necessary capacity.
- Preventing other functions from consuming all available concurrency.(AWS Documentation, AWS Documentation)
Example:
aws lambda put-function-concurrency \
--function-name MyFunction \
--reserved-concurrent-executions 100
๐น Provisioned Concurrency
Purpose: Prepares a specified number of execution environments in advance to reduce cold start latency.
Key Characteristics:
- Pre-Initialization: Allocates and initializes execution environments before they are needed.
- Additional Cost: Involves extra charges for the pre-initialized environments.
- Cold Start Mitigation: Reduces or eliminates cold start times by having environments ready to handle requests.
- Auto-Scaling Support: Can be managed using Application Auto Scaling to adjust based on demand.(AWS Documentation, AWS Documentation)
Use Cases:
- Reducing latency for performance-sensitive applications.
- Handling predictable traffic patterns with minimal cold starts.(Amazon Web Services, Inc.)
Example:
aws application-autoscaling register-scalable-target \
--service-namespace lambda \
--scalable-dimension lambda:function:ProvisionedConcurrency \
--resource-id function:MyFunction:prod \
--min-capacity 10 \
--max-capacity 100
๐ Comparison Overview
Feature | Reserved Concurrency | Provisioned Concurrency |
---|---|---|
Purpose | Guarantee capacity for critical functions | Reduce cold start latency |
Cost | No additional charge | Additional charges apply |
Scaling | Manual adjustment only | Can be auto-scaled with Application Auto Scaling |
Cold Start | Possible | Reduced or eliminated |
Impact on Account | Reduces total available concurrency | Does not impact account concurrency limit |
โ Recommendations
- Use Reserved Concurrency: When you need to ensure that a function has dedicated capacity, preventing other functions from consuming all available concurrency.
- Use Provisioned Concurrency: When you require consistent low-latency responses and can manage the associated costs.
- Combine Both: For critical functions, you can set reserved concurrency to guarantee capacity and provisioned concurrency to reduce cold starts.
Configure provisioned concurrency
To configure auto scaling for AWS Lambda functions using Application Auto Scaling, you’ll need to set up provisioned concurrency and register your Lambda function as a scalable target. Here’s a step-by-step guide:AWS Documentation+2Amazon Web Services, Inc.+2AWS Documentation+2
๐ ๏ธ Step 1: Create a Lambda Alias
First, create an alias for your Lambda function to specify which version you want to scale.Amazon Web Services, Inc.
bashCopyEditaws lambda create-alias \
--function-name MyFunction \
--name prod \
--function-version 1 \
--description "Production alias"
๐ Step 2: Register the Scalable Target
Next, register your Lambda function alias as a scalable target with Application Auto Scaling.AWS Documentation+4AWS Documentation+4Amazon Web Services, Inc.+4
bashCopyEditaws application-autoscaling register-scalable-target \
--service-namespace lambda \
--scalable-dimension lambda:function:ProvisionedConcurrency \
--resource-id function:MyFunction:prod \
--min-capacity 0 \
--max-capacity 100
This command sets the minimum and maximum provisioned concurrency for your function alias. Amazon Web Services, Inc.+2AWS Documentation+2AWS Documentation+2
๐ Step 3: Schedule Scaling Actions
You can schedule scaling actions using cron expressions. For example, to scale up at 11:45 AM UTC daily:theburningmonk.medium.com
bashCopyEditaws application-autoscaling put-scheduled-action \
--service-namespace lambda \
--scalable-dimension lambda:function:ProvisionedConcurrency \
--resource-id function:MyFunction:prod \
--scheduled-action-name scale-up \
--schedule "cron(45 11 * * ? *)" \
--scalable-target-action MinCapacity=50
This sets the minimum capacity to 50 at the specified time. Amazon Web Services, Inc.+1AWS Documentation+1
๐ Step 4: Monitor and Adjust
Regularly monitor your Lambda function’s performance and adjust the scaling parameters as needed to optimize cost and performance.