- Newest
- Most votes
- Most comments
You should consider provisioned concurrency option for lambda to minimize the latency and cold start. Make sure it has enough memory allocated to it.
Here is the blog post, which is very thorough and have detailed explanation about, how does it work and how it would help you to speed up operation.
To your question, why it takes less time second time, here is the verbiage from above mentioned blog post for your quick reference:
After the execution completes, the execution environment is frozen. To improve resource management and performance, the Lambda service retains the execution environment for a non-deterministic period of time. During this time, if another request arrives for the same function, the service may reuse the environment. This second request typically finishes more quickly, since the execution environment already exists and it’s not necessary to download the code and run the initialization code. This is called a “warm start”.
For java applications, you can consider snapstart option, here is the blog post for same.
References:
Configuring Provisioned Concurrency
Hope this helps, feel free to comment here if there are additional questions.
Abhishek
What you are experiencing is called Cold Start. This happens every time we create a new execution environment to run your function. After the invocation finishes, we keep it around, and if a new invocation happens, we reuse the execution environment, and this is a warm start.
There are different things that can be done to reduce the cold start duration. One of them is to reduce the Lambda package size (may not be possible in your case). You can find more optimization options here.
Note, cold start are most often not an issue. For instance, if your function is invoked asynchronously, cold start time usually doesn't matter. It mainly matters when you have a user making a synchronous API call. In this case, if really needed, you can use provisioned concurrency to initialize execution environments up front. Note that this will incur additional cost.
Relevant content
- asked a year ago
- asked 10 months ago
- Accepted Answerasked 2 years ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 3 years ago
- AWS OFFICIALUpdated 2 years ago
Do you have any questions further?
I reserved concurrency for my function which sped it up a lot. Thanks.