0
0
AWScloud~5 mins

Lambda concurrency and throttling in AWS - Commands & Configuration

Choose your learning style9 modes available
Introduction
When many requests try to run your AWS Lambda function at the same time, it can get overwhelmed. Lambda concurrency controls how many instances run simultaneously. Throttling happens when requests exceed this limit, causing some to wait or fail.
When you want to limit how many Lambda functions run at once to control costs.
When your backend service can only handle a certain number of requests at a time.
When you want to reserve capacity for critical Lambda functions to avoid delays.
When you want to prevent your Lambda from being overwhelmed by sudden traffic spikes.
When you want to monitor and handle throttled Lambda requests gracefully.
Commands
This command sets a limit of 5 concurrent executions for the Lambda function named 'my-lambda-function'. It helps control how many instances run at the same time.
Terminal
aws lambda put-function-concurrency --function-name my-lambda-function --reserved-concurrent-executions 5
Expected OutputExpected
{"ReservedConcurrentExecutions":5}
--function-name - Specifies the exact Lambda function to configure.
--reserved-concurrent-executions - Sets the maximum number of concurrent executions allowed.
This command checks the current concurrency limit set for the Lambda function to verify the configuration.
Terminal
aws lambda get-function-concurrency --function-name my-lambda-function
Expected OutputExpected
{"ReservedConcurrentExecutions":5}
--function-name - Specifies the Lambda function to query.
This command retrieves the number of throttled requests for the Lambda function in the last hour to monitor if throttling is happening.
Terminal
aws cloudwatch get-metric-statistics --namespace AWS/Lambda --metric-name Throttles --dimensions Name=FunctionName,Value=my-lambda-function --start-time 2024-06-01T00:00:00Z --end-time 2024-06-01T01:00:00Z --period 60 --statistics Sum
Expected OutputExpected
{"Label":"Throttles","Datapoints":[{"Timestamp":"2024-06-01T00:05:00Z","Sum":2}],"ResponseMetadata":{"RequestId":"1234abcd-5678-efgh-ijkl-9012mnopqrst"}}
--namespace - Specifies the AWS service namespace.
--metric-name - Specifies the metric to retrieve.
--dimensions - Filters the metric for the specific Lambda function.
Key Concept

If you remember nothing else from this pattern, remember: setting concurrency limits controls how many Lambda instances run at once, and throttling happens when requests exceed that limit.

Common Mistakes
Not setting any concurrency limit and expecting unlimited Lambda executions.
This can cause unexpected high costs or overwhelm downstream services.
Set a reserved concurrency limit to control usage and protect resources.
Setting concurrency too low without monitoring throttling metrics.
This causes many requests to be throttled and fail silently.
Monitor throttling metrics in CloudWatch and adjust concurrency accordingly.
Confusing reserved concurrency with unreserved concurrency pool.
Reserved concurrency guarantees capacity but reduces the shared pool for other functions.
Understand reserved concurrency reserves capacity and plan limits for all functions.
Summary
Use 'aws lambda put-function-concurrency' to set how many Lambda instances can run at the same time.
Check your concurrency settings with 'aws lambda get-function-concurrency'.
Monitor throttling with CloudWatch metrics to ensure your Lambda is not overwhelmed.