What is Lambda Functions and how it works?
In continuation with the previous post, now we're going to see in a deeper way what there is about and behind the Lambda function.
We will see the following points:
- Use cases
- What is a Lambda function and how they work
- Deployment of a Lambda function
The Lambda functions are one of several tools that exists within the concept of Serverless Architecture. Don´t require a server for its operation, but then... How they can execute my code? The key is that it does not require a server managed by you, there is no need to build a server, install libraries, configure firewall or proxies. Lambda or Amazon provides containers with everything necessary to execute our code.
We just write, upload to Lambda and AWS takes of the rest.
The provisioning and scalability of resources is also controlled by Amazon, all this on demand (more or less containers depending the requirements).
What is the cost?
AWS is based on the amount of memory used during the execution time of each function + a value for the number of times the function was executed.
More info: aws.amazon.com/es/lambda/pricing
One case, can be integrations. Integrations with third party services or API's. Services like:
- Sending email for purchase confirmations, send SMS, etc.
- File processing (image reduction, content validation, video transcoding).
- Real-time data streaming (transaction processing, social network click stream analysis, telemetric data collection from IoT devices).
From this point we can see and understand a Lambda function as a specific action of the business or functional logic.
For example, the sending of emails, SMS and image processing are tasks delimited in time, so each invocation will have the same execution time. You must take this into account to get an idea of your monthly billing because a Lambda function can't run forever (by restriction, AWS allows a maximum runtime of 15 minutes).
A use case could be that you have as a requirement to implement a reporting system using Lambdas, at first impression it could seem that the generation of a report could always take less than 15 minutes.
But what would happen if you now ask for consolidate data by week, month or year. Process this amount of data could take more than 15 minutes and our architecture with Lambda couldn´t support such a load and we would have to think about another tool.
This doesn't mean that a system of this type cannot be implemented using Lambdas, but maybe a redesign of the solution has to be considered taking into account these limitations. For example, process the report in batches dividing the workload in several functions.
You must try that a Lambda function is the basic unit of your logic and restrict to a specific task.
Now let's see what is behing and how Amazon orchestrates the scaling and the drive.
How it works?
Amazon counts with environments like Java, Node, Python, .NET, Ruby, Go; but if we need a different language than the ones mentioned above like C++ we need to use the AWS API to create custom runtime environments. And with the right configuration we can execute Lambda functions with almost any programming language.
Once the function is written, the code is uploaded to AWS and the activation event is defined. It can be a POST requirement (HTTP), CRON expressions (to activate the function from time to time) or any other service in AWS.
Events can also deliver input data to Lambda functions, for example. When the function receives an event, Amazon creates a container with the configured environment and the function code.
What is a container?
A container is an isolated software package with everything needed to run our code independently of the host operating system.
Once the container is ready and the code is executed (either successfully or with error), the container is discarted.
Once this life cycle of a Lambda is established and understood, we can go deeper into some other features of these functions:
a) Concept of container reuse:
It's produced an event that triggers a function, a container is created, the code is loaded, it executes and finish. If nothing happens after a time the container is descarted. And when a new event arrives at AWS, the whole process is repeated. On the other hand, if not much time has passed and a new event arrives, it's possible for AWS to recycle that container.
This brings benefits in terms of performance since AWS avoids creating a container and loading the code again.
b) What happens if it receives a new event while a container is alive and executing a function?
Lambda simply initializes another container and runs both events concurrently. As more events come in, the architecture routes them to available containers reusing the containers that have already finished their work, or in any case creating new containers as needed.
This escalation is automatically controlled by AWS.
Amazon recommends that all Lambda function meet the criterion of idempotence, that it if your function is executed 2 or 3 times this will not have any impact on your logic. This is the responsibility of the developer who implements the functions, since Amazon fulfills the criterion of executing the function at least once. This doesn´t guarantee that we will have one and only one execution in our function.
For example, if an app has to execute a POST requirement that activates Lambda to send an email. The function executes normally but an error occurs and Lambda can invoke a second time to ensure the criterion at least once, this second invocation finally ends and the email is sent.
But, what happens with the first invocation? We could have that it really failed and email was never sent or that the function ended successfully and in this case the end user receives two emails.
To avoid these cases we must ensure that the Lambda function is idempotent, this is achieved in a simple way. We can save a unique attribute of the call and verify that attribute in an external service, if the function already exists it returns finishing the execution.
This property is very relevant in case we're implementing sensitive business logic, such as banking transactions, sales, etc.
Surely it has happened to you that you receive duplicate charges and it's not very nice.