Domanda

First of all, I am starter trying to understand what is Node.Js. I have two questions.

First Question
From the article of Felix, it said "there can only be one callback firing at the same time. Until that callback has finished executing, all other callbacks have to wait in line".

Then, consider about the following code (copied from nodejs official website)

var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(8124, "127.0.0.1");

If two client requests are received simultaneously, it means the following workflow:

  1. First http request event received, Second request event received.
  2. As soon as first event received, callback function for first event is executing.
  3. At while, callback function for second event has to be waiting.

Am I right? If I am right, how Node.js control if there are thousands of client request within very short-time duration.

Second Question
The term "Event Loop" is mostly used in Node.js topic. I have understood "Event Loop" as the following from http://www.wisegeek.com/what-is-an-event-loop.htm;

An event loop - or main loop, is a construct within programs that controls and dispatches events following an initial event.

The initial event can be anything, including pushing a button on a keyboard or clicking a button on a program (in Node.js, I think the initial events will be http request, db queries or I/O file access).

This is called a loop, not because the event circles and happens continuously, but because the loop prepares for an event, checks the event, dispatches an event and repeats the process all over again.

I have a conflict about second paragraph especially the phrase"repeats the process all over again". I accepted that the above http.createServer code from above question is absolutely "event loop" because it repeatedly listens the http request events.

But I don't know how to identify the following code as whether event-driven or event loop. It does not repeat anything except the callback function fired after db query is finished.

database.query("SELECT * FROM table", function(rows) {
  var result = rows;
});

Please, let me hear your opinions and answers.

È stato utile?

Soluzione

Answer one, your logic is correct: second event will wait. And will execute as far as its queued callbacks time comes.

As well, remember that there is no such thing as "simultaneously" in technical world. Everything have very specific place and time.

The way node.js manages thousands of connections is that there is no need to hold thread idling while there is some database call blocking the logic, or another IO operation is processing (like streams for example). It can "serve" first request, maybe creating more callbacks, and proceed to others.
Because there is no way to block the execution (except nonsense while(true) and similar), it becomes extremely efficient in spreading actual resources all over application logic.

Threads - are expensive, and server capacity of threads is directly related to available Memory. So most of classic web applications would suffer just because RAM is used on threads that are simply idling while there is database query block going on or similar. In node that's not a case.

Still, it allows to create multiple threads (as child_process) through cluster, that expands even more possibilities.

Answer Two. There is no such thing as "loop" that you might thinking about. There will be no loop behind the scenes that does checks if there is connections or any data received and so on. It is nowadays handled by Async methods as well.

So from application point of view, there is no 'main loop', and everything from developer point of view is event-driven (not event-loop).

In case with http.createServer, you bind callback as response to requests. All socket operations and IO stuff will happen behind the scenes, as well as HTTP handshaking, parsing headers, queries, parameters, and so on. Once it happens behind the scenes and job is done, it will keep data and will push callback to event loop with some data. Once event loop ill be free and will come time it will execute in node.js application context your callback with data from behind the scenes.

With database request - same story. It ill prepare and ask stuff (might do it even async again), and then will callback once database responds and data will be prepared for application context.

To be honest, all you need with node.js is to understand the concept, but not implementation of events. And the best way to do it - experiment.

Altri suggerimenti

1) Yes, you are right.

It works because everything you do with node is primarily I/O bound.

When a new request (event) comes in, it's put into a queue. At initialization time, Node allocates a ThreadPool which is responsible to spawn threads for I/O bound processing, like network/socket calls, database, etc. (this is non-blocking).

Now, your "callbacks" (or event handlers) are extremely fast because most of what you are doing is most likely CRUD and I/O operations, not CPU intensive.

Therefore, these callbacks give the feeling that they are being processed in parallel, but they are actually not, because the actual parallel work is being done via the ThreadPool (with multi-threading), while the callbacks per-se are just receiving the result from these threads so that processing can continue and send a response back to the client.

You can easily verify this: if your callbacks are heavy CPU tasks, you can be sure that you will not be able to process thousands of requests per second and it scales down really bad, comparing to a multi-threaded system.

2) You are right, again.

Unfortunately, due to all these abstractions, you have to dive in order to understand what's going on in background. However, yes, there is a loop.

In particular, Nodejs is implemented with libuv.

Interesting to read.

But I don't know how to identify the following code as whether event-driven or event loop. It does not repeat anything except the callback function fired after db query is finished.

Event-driven is a term you normally use when there is an event-loop, and it means an app that is driven by events such as click-on-button, data-arrived, etc. Normally you associate a callback to such events.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top