paint-brush
NodeNinja Fundamentals 1.0 — The Basicsby@vijaykumar.csenitj
435 reads
435 reads

NodeNinja Fundamentals 1.0 — The Basics

by Vijay Kumar AttriJune 30th, 2017
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

The whole world is gaga over NodeJs. And when we talk about Node, we hear a lot of terms like its asynchronous,callbacks and promise. Coming from the world of C++ ,where the flows are linear and function calls and return styles very simple, that might seem a bit strange. You hear a lot of metamorphosed words like callbacks and promises.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail

Coin Mentioned

Mention Thumbnail
featured image - NodeNinja Fundamentals 1.0 — The Basics
Vijay Kumar Attri HackerNoon profile picture

The whole world is gaga over NodeJs. And when we talk about Node, we hear a lot of terms like its asynchronous,callbacks and promise. Coming from the world of C++ ,where the flows are linear and function calls and return styles very simple, that might seem a bit strange. You hear a lot of metamorphosed words like callbacks and promises.

So lets get started with these terms one at a time.

Asynchronicity and Synchronicity :

We will take a look at two scenarios to get this concept better.

Scenario 1.

Suppose you go to a bar. And it is Friday evening. You go to the waiter on the bar, ask for a drink, Lets. call the waiter Clay. But as it turns out, Clay is a pretty busy guy, and coming straight from high school, he is a bit slow on his end of things. So you see him filling your glass, utterly slow. You are literally yawning. Meanwhile, a beautiful lady walks from your side and sits at the other end of the bar. You are confused now. She seems like a perfect date. You want to talk to her.But this waiter is dead slow. So what you do.

You are smart. you tell the waiter to call you when your drink is ready and you dash straight for the dame. You are chit chatting with this lady, discussing life. You hear the call from Clay. So , you come back to Clay, thank him and rejoin your beautiful company, and drink to the happy moments..

Scenario 2. :

Suppose, Clay is slow. He is a bit rude also. When you are pleading for him to fill your glass, he directs you to stay. Or somebody else is going to gulp your 60. You have no options. You see Mr. Python go and take the place beside the lady. You are screwed.Your time is the most important thing in the world. And guess what, Clay blocked you and ruined your Friday.

Now, to put things in perspective, suppose Clay is an expensive DB call, an io operation, or a file read. You never know the exact time in which they will complete. These things take variable time. You are the process that is executing these instructions. And the lady is another request on the server.

In scenario 1, what you do is you do not waste your time. Meanwhile, you are waiting for the time consuming call to complete, you put a reminder for its execution and go to proceed with the second request. In this, you get a response from the DB that your operation has completed, so you put the second request in stack and go to execute the pending first request.(Event mechanism)

This is asynchronicity. This is how node seems fast. with just one thread of execution.Actually Javascript is not asynchronous, NodeJS just provides asynchronous APIs. It needs such operations to be non blocking.

Coming to synchronicity.

In scenario 2, Clay blocked you. During the time the second request came, you could do nothing . Mr Python stole the show. You took your drink, drank it and came home.So consider Mr. Python to be another thread.

You were servicing the first request, It blocked and so … . Meanwhile the second request came. So what did the server process do, it spawned a new thread and went about the second request.This is how things work in PHP.

Well this is a generalisation. A thread at anytime has its stack, It executes the functions in its address space. So Node seems a bit of lightweight champion, with its single thread of execution- called the event loop. It runs continuously and the time for one event loop to execute is called is tick.But Node handles this context switch very well and you see, it is what has made it so popular.

Lets consider the implications of this in NodeJS.

So NodeJs has two set of things. Blocking and Non Blocking. (Sync and a-Sync). You have file reading functions such as fileReadSync and fileRead. Consider a server process in which you have 10 APIs, 1 of which is to read the contents of a file which large.

Alice and Bob are newbies and are requesting the APis.

Using fileRead — ASync method

Case 1.

Alice and Bob request the GET /my_favourite_seasons from the server. Server being a node process, sets about executing the calls,

A’s request hits the server first, the event loop picks up the call, starts about executing it. It goes to the file read function. But the file read is Async, so NodeJs puts a note, attaches a kind of event for some notification as and when the read is complete and starts listening again. B’s request comes and well it starts serving it. Fast forward few milliseconds, A and B are happy to receive their list of favourite seasons from the server.

Case 2.

Lets suppose the file read in this scenario is blocking. So the node process sets about executing A’s call. it gets to the file read function, But it gets blocked here. It is not free now. B’s request comes, but guess what, there is no one to service this call. NodeJs is a one man army and that man is busy dumping the contents of the file into memory. So B will be stuck — its request will be delayed. as the event loop was busy.Or simply the server won’t take the connections.

This is what happens when you use too many blocking calls in your APIs. Your server can crash if you miss such considerations.

Try adding an infinite loop in you process and see how the server behaves.

What you should have done is do things the first way. If the file size was very large, you should have streamed the file in the response.

So coming to streams and buffers.

Streams and Buffers :

We hear these terms a lot. My youtube video is buffering, stream from spotify..and what not. You hear these everyday.

what exactly are they?

Buffer : You are reading a file from the disk. What is generally done is the entire contents of the file are copied from the disk to the RAM. The file is stored in a buffer. And when it comes to big files, using buffers is a strict no no(for obvious reasons :D ). What you should do is our next topic of discussion. SO follow on.

Streams :Stream is flowing data. Yeah just like waterfalls. The data flows. Your process calls a plumber. it fixes a pipe , straight from the file tank to your ram, where your node process also lays, and from where the CPU picks it up when its turn to execute comes.

Fetch Decode Execute in short. So what happens is you turn the tap on. Droplets of data are coming in your bucket. When it fills, a data event is generated and the node process reads the data. The entire contents of the file are read in chunks. Only the bucket size is allocated in RAM , and everything goes according to plan. These are streams. And in a nutshell, this is how streaming works.

For some really good references, you can refer :


Why is node.js asynchronous?_Nobody has actually asked this (from all the 'suggestions' I'm getting and also from searching before I asked here). So…_stackoverflow.com


Is an infinite loop impossible in nodejs?_Join the Stack Overflow Community Stack Overflow is a community of 7.3 million programmers, just like you, helping each…_stackoverflow.com

Additional Demo :

IMPLICATIONS OF USING INFINITE LOOPS

for Creating the server and testing , just use express:

npm install express -g

npm install express-generator -g




#make sure you have node installed in your systemnpm install express -gnpm install express-generator -gexpress your-process-name

Then go to the routes/index.js and replace the file with the one below


var express = require('express');var router = express.Router();



/* GET home page. */router.get('/', function(req, res, next) {console.log('hello');

while(1){




}// we will never reach hereres.send();});





router.get('/async', function(req, res, next) {setTimeout(function(){console.log('it should get printed after 10 seconds');res.send();},10000);

});

module.exports = router;

Do a “npm start”. It will set the server listening on port 3000.

So , now try calling the async method first, it will print “it should get printed after 10 seconds” after 10 seconds and send the response(empty).

curl -X GET http://localhost:3000/async

So now call the /async endpoint and then try the below mentioned curl request. You will never get a response from the server.

curl -X GET http://localhost:3000

What really happens is that the callback is deferred indefinitely as the event loop is blocked in the “/” api. So this happens when you use infinite loops.

It has a lot of implications on a server with high traffic.

So guys, this is where Fundamentals 1.0 ends. If you have any doubts or feel something is wrong, please do correct me in the comments.

Peace !!