Why Node.js



Why Node .js?
This is basically focused on performance. Node performs the best for client’s response for servers. Say we have 100 concurrent clients and 1 megabyte of response. According that the node will have 833 Req/sec. This is comparably very fast response rather than ngins and thin. For this kind of thing we have some option call Sync and un-Sync options. We can see that later in this progress Document.
Code Comparison between Node and PHP.
For a Request,
Query in PHP.
result =query('select * from T')
The above single line of query may blocks the entire process somewhere or implies multiple execution stacks Until it get the response of the DB. Because the query is accessing the disk for an information. But when is come to the Ram and L1, L2 caches those are non-blocking processes. So we can increase our performance when we keep the application request in that level.
So the solutions for the above blocking issue are,
Option 1: Multitasking
Option 2: Other threads of execution can run while waiting
Through the event looping we can process the query efficiently.
query('select..',function(result){
        //use result
});
The above line allows the program to return to the event loop immediately using the callback function. Here No machinery required
The beauty of this event looping and callback is we can use this according to our needs (block or un-block),
For example:
We need to stop the process until we read any specific config file or specific name for the process. So for that we have to do some waiting event.
>In cultural Base
puts("Enter your name ");
var name = gets();
puts("Name : " + name);
The above sample code is asking for a user name. Until it receives the using name the other lines will not process. But again we have another problem if we have 1000 users online. And those 1000 users waiting to enter their names like this then think about the server load as that time. So here the callback plays the role to execute other line until is wait for some specific functions.
Using callback function,
puts ('Enter your name');
gets(function (name){
puts("Name :" + name);
});
But again the rejection is so complicated. Then why everyone using event loops? Because single threaded event loops require I/O to be non-blocking. We can access the disk without blocking. ruby python are event based programming languages. But the according to the user increases we need some expert knowledge of event loops. The JavaScript designed specifically to be used with an event loop and amazing thing
-Anonymous function, closures
-Only one callback at a time
-I/O through DOM event callbacks
The node.js project is to provide a purely evented non-blocking infrastructure to script highly concurrent programs.
Node JS Design goals
No function should directly perform I/O
To receive info from disk, network, or another process there must be a callbacks.
The Node is process based on Stack
ev_loop()->socket_readable(1)->http_parse(1)->load("index.html") So node sends request to the thread pool to load "index.html" and the request is send to disk In the mean time it will call the socket_readable(2) until the socket_readable(1) response. If the socket_readable(1)response then it will return file_loaded() in stack as bellow ev_loop -> file_loaded() ->http_respond(1) it means is respond to first request
Reference:
Nodes Conference 2012 - Click
Ryan Dahl (Creator of Node.js) Introduction speech - Click
Bruno Terkaly of Microsoft workshop - Click

1 comment:

  1. Thanks for sharing this post. Its really useful information about Nodejs. Nodejs Training in Bangalore

    ReplyDelete