Hi, everyone, and welcome back.
Now in today's video, we are going to discuss about the basics of lambda concurrency.
Now, the first important part that we need to remember is the concept of concurrency.
So at a high level overview, concurrency is the number of in-flight requests that your lambda function
is handling.
At the same time.
Now, at this stage, one point to note is that for each concurrent request, Lambda provisions are
separate instance of the execution environment.
Now, this can be better understood with the diagram where you will see that there are two execution
environments for two requests.
So request one.
You have an execution environment number one.
And at this stage there is also a request two that has come up.
And for this request, two to be processed, Lambda creates one more execution environment.
Now, at this stage, one important learning is that as your function receives more requests, Lambda
automatically takes care of scaling.
So it automatically adds the number of execution environments so that the request can be proceed.
However, there are certain limits to the number of execution environments that you can launch, and
there are certain level of account limits that are available, which you can also raise based on the
requirements.
So let's also go ahead and look into it in more detail.
So let's assume that you have a request that has come to Lambda.
So to handle the request, Lambda must first initialize an execution environment.
This is something that we have seen and this is an important step before using it to invoke your function.
So you see you have the first request that has come, so the initialization of environment has taken
place and then the invocation process starts.
At this stage, during the time when the request has been processed, the execution environment becomes
busy and it does not take any more requests.
So the second point, your states that when the lambda function finishes the processing of the first
request, the execution environment can then process additional requests for the same function.
So your important point to note is that lambda can reuse the same execution environment to handle the
second request.
So your after the first request is processed.
As we know, when the first request comes, the Lambda initializes the execution environment.
So your the execution environment is initialized and the request number one is processed.
And when the second request comes, the second request can use the existing environment and no initialization
is required.
So at this stage, when we say that you have a single instance of execution environment, that means
that you have the concurrency of one.
Now, when we look from a real world perspective, Lambda may need to provision multiple set of execution
environments in parallel to handle all of the incoming requests.
So let's assume that when your function receives a new request, one of the two things can happen.
First one is that if there is a pre initialized execution environment instance that is available, Lambda
will use it to process the request or Lambda can go ahead and create a new execution environment instance
to process this specific request.
This is something that we have already seen.
Now here, if we look into the sample workflow of ten requests, what we can see is that the request
number one, two, three, four, five and nine in the new execution environments are initialized.
However, the request number six, seven, eight and ten, they are reusing the existing set of environments
that are already available but after the earlier request have been processed.
Now, from production point of view, it is important to note that the mode, the execution environment
gets initialized for the request, the more delay it would make.
So there will be certain amount of delay because this execution environment needs to be initialized
and there are certain ways in which you can minimize this.
However, at this stage we are not really discussing it.
We are just looking into it from the basic understanding perspective of post, which will be looking
into how we can optimize our lambda environment from production point of view.
So I hope with this you understood the basics of concurrency and with this will conclude today's video.