Introduction
We will start off by describing what concurrency in AWS Lambda is, and then what are the default standards that AWS has described. Further, we shall talk about types of concurrencies in AWS Lambda and how one affects the other. Lastly, we talk about how the concurrency can be used to save your systems from excessive load that are involved with lambda.
What is concurrency in AWS Lambda?
Concurrency is the number of requests that your function is serving at any given time. When your function is invoked, AWS Lambda allocates an instance of it to process the event. When an instance of a lambda function completes its execution, it can handle another request. If the function is invoked again while a request is still being processed, some other instance is allocated, which increases the function’s concurrency. Simply put, concurrency means multiple instances of a lambda function running simultaneously when invoked from multiple trigger points. For example, if a lambda function is attached to a DynamoDB stream, then on every insert operation the lambda is triggered. Let’s say lambda function takes 1 min to complete its execution. At timestamp :
- T – an insert in DynamoDB occurs and it triggers the lambda function. Hence one instance of lambda function say L1 starts executing.
- T + 15 sec – another insert in DynamoDB occurs and it again triggers the lambda function but the instance L1 is still running and it is expected to complete its execution by T + 60 sec. At this point, AWS Lambda will assign a new instance L2 and start the execution for this new entry.
Now 2 instances of lambda function are concurrently running. - T + 60 sec – L1 completes execution.
- T + 75 sec – L2 completes execution
The AWS Lambda has a concurrency limit – which is described as ‘How many number of concurrent invocations of a lambda function is allowed.’ Incase this limit exceeds, lambda starts throttling the remaining requests.
Reserved Concurrency Limit vs Unreserved Concurrency Limit
There are two types of Lambda concurrency limits – Reserved and Unreserved.
The unreserved concurrency limit is the number of invocations that the AWS offers to all those lambda functions who have not specified their reserved concurrency limit. Such lambdas share the common pool of Unreserved Concurrency Limit. By default the limit is set to 1000 for one AWS region. This means if there are n number of functions – 1000 concurrent invocations will be shared among them. Note that, the invocations are shared, not divided, which means there are no reservations yet for any lambda. From the unreserved concurrency limit pool, an instance is picked and used as and when the unreserved lambdas require.
The reserved concurrency limit is exactly what it sounds. It means the number of invocations that are reserved and will be guaranteed to the lambda function. When a function has reserved concurrency, no other function can use that concurrency. Fortunately, AWS has now allowed to set reserved concurrency limit at function level as well. Hence, we can set different reserved concurrency limits to different functions.
We will now get to know some of the important points about the concurrency of the AWS Lambda.
Important Points
1. The concurrency limit is defined region-wise in an AWS Account which means, each region in your AWS account has a Lambda concurrency limit. The limit applies to all functions in the same region and is set to 1000 by default.
2. We can set a limit of reserved concurrent executions (invocations) on a function level. This means for each function, we can set a limit through console and also via CDK.
3. The limit (say X) that we set will be reserved concurrent executions for that particular lambda. The remaining (1000 –X) will still be in pool and will be shared among rest of the unreserved functions.
4. The lambda having a reserved concurrent executions limit, cannot access from unreserved pool of executions, which means if the number of executions exceeds the limit, it would throttle remaining requests.
5. You must only set the reserved concurrent executions limit if you know how many concurrent executions the lambda would require.
6. Lambda requires at least 100 unreserved concurrent executions per account.
How is concurrency useful to save systems that lambda would operate?
Having reserved concurrency limit set, can be used very effectively to preserve the systems that lambda interacts with. Let’ take an example – A lambda function performs queries on redshift cluster, and a number of instances of this lambda will query the redshift simultaneously. However, what if the number of concurrent lambdas querying the redshift increases to a high number? From the redshift’s point of view it will have larger amount of load to serve the queries to these concurrent lambdas. As a result, the redshift might go down (or the performance gets incredibly slow). Hence in order to preserve the redshift, we can ‘reserve’ the concurrency limit to certain number, and if it exceeds, the lambda would throttle any more new invocation requests. This would save the load on redshift.
Similar use-cases can be explored and systems can be preserved by setting reserved concurrency limit.
References
https://docs.aws.amazon.com/lambda/latest/dg/configuration-concurrency.html
https://www.bluematador.com/blog/why-aws-lambda-throttles-functions