Node.js

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Prelates, Moderators General

Node.js

Postby naschilling » Tue Sep 06, 2011 9:48 pm UTC

I've been a long time user of Apache (since 1.3) and I have in depth knowledge of the modular design and processing mechanisms. However, lately I've been hearing more about less traditional servers, such as Node.js.

It's JavaScript based and uses the Chrome V8 engine, which is no surprise, which gives it good JavaScript performance. The more interesting component to me is that it completely ignores the traditional 1 thread/process to 1 request model that has been the basis of servers for as long as I can remember. Supposedly, the model scales well, and I've seen some preliminary data that supports those claims.

The whole notion has broken my brain. In short, my question is "how does it work?" I've read all their documentation and tells in depth about what the Apache model does wrong, but it doesn't describe how it does it right. I understand that a signal is raised upon data arriving on a socket, and once the entire request arrives, the callback is launched, but how does that mechanism handle multiple requests at the same time? How does that scale well? The IO is praised as being non-blocking, but how does it read a file using non-blocking IO while processing a request?
If you don't have walls, why would you need Windows?
User avatar
naschilling
 
Posts: 142
Joined: Wed Apr 06, 2011 2:52 pm UTC

Re: Node.js

Postby Xanthir » Tue Sep 06, 2011 10:02 pm UTC

It all works in the same way (essentially) as current JS engines in browsers work, with events and callbacks. Whenever an event is done running (the script stack "hits zero"), you return to the event loop and process the next event in the queue.

Slow things like file io are wrapped in an async api, so you have to pass a callback to them to continue your program execution. This means that the rest of your program happens at some indeterminate time later, when Node's event loop reaches your callback. In the meantime, any other requests that came in between the time your code started running and the time the file io returns get serviced ahead of you.

This sort of model works great when you have lots of short, easy things to do, and only an occasional big expensive thing. The big thing gets broken up into pieces and spread out, so the little things can run in between those pieces. This means that the big expensive thing ends up taking a little bit longer, but it's already going to take a while, so the difference is fairly negligible, while the small things aren't blocked by your expensive thing and get to run much quicker than otherwise.

The whole thing runs as a single process, continually receiving requests, pausing code, and reviving it as its callback reaches the top of the event queue.
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))
User avatar
Xanthir
My HERO!!!
 
Posts: 4305
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex

Re: Node.js

Postby Anonymously Famous » Tue Sep 06, 2011 11:46 pm UTC

I've never heard of Node.js. I'll have to look it up later. Thanks for the information!
Anonymously Famous
 
Posts: 240
Joined: Thu Nov 18, 2010 4:01 am UTC

Re: Node.js

Postby naschilling » Wed Sep 07, 2011 1:07 am UTC

The longer I think about it, the better it sounds. I'm paralleling it to the cloud-based services where the processing is handled separate from file storage or database interaction. Once that abstraction is made, I can more easily see how each request takes a minimal amount of processor time.

It still seems a bit elaborate to scale this to a multiple processor system, but not impossible.

I still feel like this isn't what JavaScript was made for, but it is a revolution in the processing model of web requests.
If you don't have walls, why would you need Windows?
User avatar
naschilling
 
Posts: 142
Joined: Wed Apr 06, 2011 2:52 pm UTC

Re: Node.js

Postby KnightExemplar » Wed Sep 07, 2011 4:52 am UTC

naschilling wrote:I still feel like this isn't what JavaScript was made for, but it is a revolution in the processing model of web requests.

Hate to rain on your parade, but this isn't really revolutionary at all.

http://en.wikipedia.org/wiki/C10k_problem

Lighttpd is over 8 years old, and was able to break the 10k barrier when it was originally written due to the event-driven nature of the server. Note: 10,000 concurrent requests means less than 500k memory per request on a 32-bit computer with 4 GB of RAM, so accomplishing this on standard computers 8 years ago was quite the feat.

There are plenty of relatively mainstream servers that take this approach. Lighttpd is still popular, as are Ngix and Cherokee. I personally use Cherokee for my VPS, but thats cause its user interface looks pretty awesome. Using Cherokee + Postgres + PHP only uses ~200 MB of ram under stress ("Stress" is ab -c 100 -n1000 on the blog's homepage), although I'm not running much on my server yet.

Long story short... I don't really see how this is much different from the leaner web servers like Ngix, Lighttpd, and Cherokee. Except those web servers are written in C and have been optimized the hell out of for nearly a decade, while Node.js is Javascript-based and probably hasn't had nearly as much optimization time as those lighter HTTP servers.

Nonetheless, it is impressive to put Node.js on the list of servers that can accomplish c10k, but it doesn't seem to have much benefits as a general web server. Can it run Ruby? Can it run PHP? Or is it only Javascript? Does it do URL-Rewriting? Etc. etc. Server-side Javascript might be useful in maximizing code reuse on client side (validation of forms comes to mind...), but without framework libraries or other APIs, I don't expect it to displace any of the Giants of the Web World (.Net, Java, PHP, Python, RoR).

And still, Apache has its place as the 800lb Gorilla of the Web World. Every web package seems to target Apache still (Lets see, phpbb, wordpress, and so forth have .htaccess files, which is an Apache-specific feature).

------------------

As far as web-scalability goes, I'm interested in database replication (its always the database / disk that is slowest), NoSQL, especially for caching, and Gearman. Node.js is an interesting application of Javascript... but I think other web servers have already solved the HTTP scalability problem. Plus, Google is going one step further, and is trying to displace HTTP with the new SPDY protocol.
First Strike +1/+1 and Indestructible.
KnightExemplar
 
Posts: 2404
Joined: Sun Dec 26, 2010 1:58 pm UTC

Re: Node.js

Postby Maelstrom. » Wed Sep 21, 2011 7:12 am UTC

KnightExemplar wrote:
Nonetheless, it is impressive to put Node.js on the list of servers that can accomplish c10k, but it doesn't seem to have much benefits as a general web server. Can it run Ruby? Can it run PHP? Or is it only Javascript? Does it do URL-Rewriting? Etc. etc. Server-side Javascript might be useful in maximizing code reuse on client side (validation of forms comes to mind...), but without framework libraries or other APIs, I don't expect it to displace any of the Giants of the Web World (.Net, Java, PHP, Python, RoR).


Node doesn't run PHP or Ruby, but that really isnt a fair question. its like asking if Ruby can run PHP, or if Python can run Ruby. To create a node server, you set up an event listener on a port, and wait for incoming connections, much the same way that you can run Python/Django or RoR (ignoring mod_passenger et al). You run your node application either directly on port 80 for a single application server, or run it off a different port and proxy pass to it from Apache/nginx/Cherokee/Lighhtp/etc. The node process is always running, unlike PHP applications, which are usually started and stopped for every page load.

Node isn't a 'web server' in the sense of Apache, it merely contains a module that can listen on a network port and understand HTTP requests.
Maelstrom.
 
Posts: 71
Joined: Tue Oct 21, 2008 12:18 pm UTC

Re: Node.js

Postby KnightExemplar » Wed Sep 21, 2011 1:41 pm UTC

Node doesn't run PHP or Ruby, but that really isnt a fair question. its like asking if Ruby can run PHP, or if Python can run Ruby. To create a node server, you set up an event listener on a port, and wait for incoming connections, much the same way that you can run Python/Django or RoR (ignoring mod_passenger et al). You run your node application either directly on port 80 for a single application server, or run it off a different port and proxy pass to it from Apache/nginx/Cherokee/Lighhtp/etc. The node process is always running, unlike PHP applications, which are usually started and stopped for every page load.

Node isn't a 'web server' in the sense of Apache, it merely contains a module that can listen on a network port and understand HTTP requests.


I understand it isn't a 'fair' question, but I'm trying to understand its use case. And for right now, I see only a rather limited one. Clearly, it isn't a general-purpose web server. So that means it was designed for some niche purpose. But what purpose was that?

If its designed for some sort of middleware layer, we've got Gearman and RabbitMQ for efficient backend computer-to-computer communication. If its for the public web-facing stuff, we've already got efficient HTTP servers that support the FastCGI protocol. True, options are always nice. So the good thing about Node.js is all about programming Javascript on the backend. (which is a more difficult process before Node.js. You'd probably have to do some sort of Java Servlet+Rhino thing). The efficiency thing is more of a bonus on top of that, but not the real story.
First Strike +1/+1 and Indestructible.
KnightExemplar
 
Posts: 2404
Joined: Sun Dec 26, 2010 1:58 pm UTC

Re: Node.js

Postby b.i.o » Thu Sep 22, 2011 2:58 am UTC

KnightExemplar wrote:I understand it isn't a 'fair' question, but I'm trying to understand its use case. And for right now, I see only a rather limited one. Clearly, it isn't a general-purpose web server. So that means it was designed for some niche purpose. But what purpose was that?

The purpose is running JavaScript on the server. What it excels at are single-page web applications. (And also small, easy-to-write, specialized web servers.) All of the innovation in real-time server-browser communication is happening in and coming from the Node world.

Node's competitors are not generally things like Apache. People don't use Node to serve web applications not written in Node. It's a much closer competitor to frameworks like Rails or Django, except with an integrated server.
User avatar
b.i.o
Green is the loneliest number
 
Posts: 2520
Joined: Fri Jul 27, 2007 4:38 pm UTC
Location: Hong Kong

Re: Node.js

Postby thedufer » Tue Oct 04, 2011 9:44 pm UTC

I used Node.js at my internship this summer, and its perfect for single-page webapps. Since you serve very little HTML, you do most of your work building JSON objects, which is easy when you are sending data between client and server javascript. You only have to model your data once - client and server can reuse the code so they're always working with the same model. As far as the speed gains from the callback-oriented async stuff goes, javascript is basically built for that use-case, so its way easier to code in it than any other language I've come across.

It also helps that MongoDB, which is probably the leading NoSQL these days, uses javascript in its shell and deals in JSON, so you really only have to deal with one language between three layers (client, server, DB).

Finally, web browsers are basically the most important applications these days, so optimizing V8 is a huge priority for a large number of very smart people. No other language is going to get that kind of attention towards optimization.
User avatar
thedufer
 
Posts: 263
Joined: Mon Aug 06, 2007 2:11 am UTC
Location: Northern VA (not to be confused with VA)


Return to Coding

Who is online

Users browsing this forum: No registered users and 4 guests