views:

163

answers:

3

I have been looking into Node.JS and all the documentation and blogs talk about how it uses an event-loop rather than a per-request model.

I am having some confusion understanding the difference. I feel like I am 80% there understanding it but not fully getting it yet.

A: 

Think of incoming requests or callbacks as events, that are enqueued and processed.

That is exactly the same what is done in most of the GUI systems. The system can't know when a user will click a button or do some interaction. But when he does, the event will propagated to the event loop, which is basically a loop that checks for new events in the queue and process them.

The advantage is, that you don't have to wait for results for yourself. Instead, you register callback functions that are executed when the event is triggered. This allows the framework to handle I/O stuff and you can easily rely on it's internal efficiency when dealing with long-taking actions instead of blocking processes by yourself.

In short, everythings runs in parallel but your code. There will never be two fragments of callback functions running concurrently – the event loop is a single thread. The processes that execute stuff externally and finally propagate events however can be distributed in multiple threads/processes.

PartlyCloudy
+3  A: 

A threaded model will spawn a new thread for every request. This means that you get quite some overhead in terms of computation and memory. An event loop runs in a single thread, which means you don't get the overhead.

The result of this is that you must change your programming model. Because all these different things are happening in the same thread, you cannot block. This means you cannot wait for something to happen because that would block the whole thread. Instead you define a callback that is called once the action is complete. This is usually referred to as non-blocking I/O.

Pseudo example for blocking I/O:

row = db_query('SELECT * FROM some_table');
print(row);

Pseudo example for non-blocking I/O:

db_query('SELECT * FROM some_table', function (row) {
  print(row);
});

This example uses lambdas (anonymous functions) like they are used in JavaScript all the time. JS makes heavy use of events, and that's exactly what callbacks are about. Once the action is complete, an event is fired which triggers the callback. This is why it is often referred to as an evented model or also asynchronous model.

The implementation of this model uses a loop that processes and fires these events. That's why it is called an event queue or event loop.

Prominent examples of event queue frameworks include:

igorw
I think it is important to outline that an event queue does not imply the absence of thread pools. For instance, node.js heavily uses thread pools for blocking operations internally. What the event queue model cahnges is the way how to interact with such long-taking actions.
PartlyCloudy
A: 

An evented loop allows you to handle the time it takes to talk to the hard drive or network. take this list of time:

Source | CPU Cycles
L1     | 3 Cycles
L2     | 14 Cycles
RAM    | 250  Cycles
Disk   | 41,000,000 Cycles
Network| 240,000,000 Cycles

That time you're running curl in PHP is just wasting CPU.

Rixius