1) Lambda execution cycle
When lambda is invoked what exactly happens:
a)Download your code b)Start container c)Bootstrap the container d)Start your code
A cold start occurs when an AWS Lambda function is invoked after not being used for an extended period of time resulting in increased invocation latency.
Best practices to optimize the execution time:
1.1) Avoid fat functions.
1.2) Initialize global variables outside the Handler. These variables are initialized when the container is created.
1.3) Only use the bare minimum dependent libraries needed to execute the code. Ex: A common mistake is to use the entire AWS SDK for writing code and ship it with the packages. Instead, identify the required SDK and ship that only.
1.4) Lambda is a good candidate for Transformation and should not be used for Transport.
1.5) Don’t use Lambda for I/O operations or Orchestration.
Note: By saving 1 second of execution time, for 10 million requests you save 100$s.
2) Lambda invocation
Lambda is invoked either by AWS events that happen inside the AWS services or through direct invoke from AWS SDK.
Best practices to follow while invoking lambda and sending the response back from Lambda:
2.1) Compact the payload. JSON is a user friendly format. But it eats up lot of memory. Protocol buffers or binary json are some candidates which can be used while sending data to lambda.
Also Lambda can use any of those protocols to send out data as response.
Use filtering as much as possible and try to read only those data which is required.
2.2) Different types of Lambda invocations:
1)Synchronous 2)Asynchronous 3)Stream based
Why this invocation type matter?
-> Resiliency and Durability
Consider the following scenarios:
q.1) What if the Lambda is down and the event has been triggered by some AWS service to invoke the Lambda?
q.2) Where the events should be stored or captured?
Event store events can be preserved for a future retry.
In Synchronous lambda invocation, the number of Retry is 0.
In Asynchronous, retried 2 times.
In Streams, retried all the time.
3) Lambda concurrency configuration
Earlier, Lambda automatically scales up and down based on the number of requests it receives. It can concurrently respond to requests since it is assumed that Lambda function is Idempotent.
But some clients faced issues when they used Lambda to talk to Relational database which can handle only few requests at a time.
This resulted in Concurrency configuration in Lambda.
Now you can configure how much concurrency you want with your Lambda function by concurrency parameter.
If the value of concurrency is 0,it is called as Kill switch ,that means your Lambda is off and it will not respond to any request.
4) Lambda inside VPC
When will you put a Lambda in a VPC?
There is a guideline for this:
4.1) If your Lambda needs to access a resource which is inside a VPC, then and then only you need to put the Lambda inside that VPC.
Why there is a concern about putting Lambda inside a VPC?
Because the cold time increases. VPC introduce two more steps in the cold start of Lambda. Create VPC end point and Attach VPC to lambda.
5) Monitoring of Lambda
AWS X-Ray is widely mentioned to check the performance of Lambda. There is an open source project as well named Lambda power tuning on GitHub.
6) Code deploy is a good candidate to deploy the Lambda.
7) Canary Lambda release
There is a new parameter in the Lambda configuration called as Weight. When there is a new release of Lambda function, you can shift the traffic from the old to new version of Lambda based on the value of the weight parameter.
8) Lambda AWS UI is now well equipped for with cloud9 for development and testing.