Overview of Amazon Lambda

Before we start with amazon lambda, what all things do we need to do in the production env to run our service.
1) We setup a machine ( say a VM), with certain configurations like os, ram, space etc.
2) You setup a server there.
3) You deploy your app.
4) You start the server.

Now your application is live and ready to serve requests, but what happens when the number of requests increase from few hundred to thousands per seconds, you server begins to blow up!!!!!.
What do you do next? Yes!!!, Scale!!!!.
You will probably repeat all the above steps again to maybe start a new server. Then configure a load Balancer to route the requests to the servers.
This is really painful.

Amazon Lambda can take care of lot of things which I mentioned above.

So, what is Amazon Lambda?
Amazon Lambda is a service which runs your code that you upload to the AWS Lambda infrastructure. AWS lambda infrastructure takes care of everything, managing servers, OS maintenance, Scaling, logging etc.

When does your code run?
Amazon lambda runs your code in response to
1) events that occur at different event sources, e.g adding or deleting object in S3 or some change in DynamoDB.
2) http requests.

What languages does it support?
Node.js, Java, Python.

So as I mentioned above the lambda function is run in response to events (like adding, deleting updating etc) that occur in various event sources (Amazon s3, Amazon DynamoDB) etc. So the event based execution can be done using the Push or the Pull model.
Pull Model: Amazon lambda (your code) polls the event source(e.g S3) to check for the occurrence of the event. So the association of the lambda function (your code) to the event source is maintained at the Lambda side
Push Model: The event source has the information about the lambda function, and calls the function when the event occurs.

Using https: You can also invoke your lambda code using the https request. When you send an HTTPS request to the API endpoint, the Amazon API Gateway service invokes the corresponding Lambda function. see http://aws.amazon.com/api-gateway/

We will basically talk about writing lambda in Node.js.
Basic Components of Lambda:
Handler function: This function is the starting point of your lambda function. In node you make this function available to the lambda infrastructure using the typical exports keyword.

e.g:
exports.myHandler = function(event, context, callback) {
...
}

Parameters in the handler function:
1) event: using this parameter event data is passed to the handler e.g S3 may pass the object added in bucket for processing.
2) context: using this parameter you get the information about the timeout for your lambda, the log streams associated with your lambda etc.
3) callback: this is optional and used to send information to the caller. It has the typical callback format: callback( <error>, <success object>).

Following is a image showing the triggering of lambda expression on image upload in S3, and then resizing the image and storing it in other S3 bucket:
lambda_image_thumbnail_creation
image describing the workflow is taken from AWS Documentation

This is the basics of Amazon lambda. To get started with your first lambda function visit http://docs.aws.amazon.com/lambda/latest/dg/getting-started.html.

What really is Javascript’s Event Loop

All of us know Javascript is a “single-threaded non-blocking asynchronous concurrent language, It has a call stack, an event loop, a callback queue, some other apis and stuffs” . We have also heard about V8 – Chrome’s Javascript runtime engine.

Many of us have known already about Javascript’s Event loop, but for the newbies this must be a good read.

Putting the big picture,

Squashapps-chrome-runtime

Let’s see what happens when this piece of code is executed,


console.log('hi');
setTimeout(function cb() {
console.log('JSC');
},5000);
console.log('how are you');

The result in the console would be,
hi
how are you
JSC

How this code is manipulated in V8 is,

  • When that piece of code is executed, console.log(‘hi’) is pushed onto the stack and “hi” is printed in the console.

Eventloop

  • The next thing is, the timeout function is triggered. The timeout function is not in the stack so a timer is kicked off in the webapis  which will run for 5 secs.

Eventloop-timeout

  • Since the timer is run by the webapi and also the stack is empty, console.log(‘JSC’) is pushed onto the stack and “JSC” is printed in the console.

Eventloop-2

  • After 5 sec, once the timer is done the function is queued up in the task queue

Event loop

  • Here is where the event loop comes into picture, the job of the event loop is to execute the task queue in a FIFO manner only when the stack is empty.

So, you may have a question “What if some process is taking place in the stack and the timeout is done?”  The answer is the timeout function will be put onto the queue and it will wait until the stack is empty, no matter how many seconds timeout you gave.

What do you think will be the output for this code?


for(var i=0; i<1000000000; i++) {
console.log(i);
if(i===0){
setTimeout(function cd() {
console.log('hi');
},2000);
}
}

I was asked the same question by my CEO one day in my office, the next moment i replied
0,1,2,3 and so on and after 2 secs some where in between
‘hi’ and then all the way to
…. 1000000000

But that’s not right, I totally forgot about the event loop. The exact output would be
0,1,2,3, … ….. …… , 1000000000
hi

You may ask me, what if I give a timeout(function(), 0); , even then the timeout function is going to go to the task queue and it should wait until the event loop is triggered and will be triggered  when the stack is empty 😉

“Go through this wonderful presentation by Philip Roberts which was my inspiration”.

Beware of the Event loop!

Happy coding!