Saturday, January 18, 2014

Basic Description About Node.js

Basic description about what node.js is.

Breaking the borders

No doubt, there has been a huge hype around Javascript for years now. It seems like Javascript is everywhere. You can create awesome mobile apps with it. Some crazy guys even created a project where C code will be translated into Javascript( emscripten ) See an awesome example of what’s possible with Javascript here.

If you can create singlepage-apps that works like a native application… Why shouldn’t you use Javascript on the server as well? That’s where node.js comes into play. I won’t talk about whether it’s a good idea to use an untyped language on the server or not. This decision belongs to you.. But… choose wisely;))

Node is written in C++ (as well as Google’s V8 Engine), runs on Googles V8 and is part of the Server Side JavaScript environnement. It extends the JavaScript API to offer usual server side functionalities. Node has been created by Ryan Dahl back in 2009. The project is run by Joyent (the company employing Ryan Dahl). It’s an opensource project..

Node’s internal architecture



The goal

“Node.js is a platform built on Chrome’s JavaScript runtime for easily building fast, scalable network applications.” That’s what the website says. It should be easy to use, and also very flexible.
There are 3 major feature in node’s architecture, to archive this goal.

Single threaded
Unlike the most (web)server which spawns a thread for every incomming request, node only uses one thread. This avoids context switching. You may think, 1 thread sounds like a bottle neck? Well consider reading about the C10k problem and go on reading ;)..

Non blocking I/O
Javascript is by design a non blocking language, so node uses it as one of it’s main features to avoid waiting for a response of long running processes. (e.g. database calls)
This allows node to handle a huge amount of traffic because the process will be handed over and the main thread is free to do new stuff..

But wait.. didn’t I just say it’s single threaded… Well in the one hand it is.. In the other it’s not.. Let’s have a closer look.

node_threading_model

Actually, the event loop is indeed single threaded. To be able to answere to new incomming requests as soon as possible, long running jobs will be handed over to internal worker threads.

Event Loop
The (single threaded) event loop uses Marc Lehmann’s libev. As mentioned above, long running jobs will be handed over to worker threads to be ready for the next job. You can think of it as it’s a queue of things to do. Things which takes a little longer will be done by others so that you have more time doing new stuff ;)

Summary

I’ve explained how node works internally and what kind of technic is used to achieve the goals mentioned above. Node actually is a Javascript runtime without a DOM, but with a lot of API’s, you would wish to have in a browser. One of the main features in my opinion is that you can do nearly everything, but you don’t have to. E.g you can create a little webserver that’s really just printing out “Hello World”. This webserver plus the actual “logic” would contain no more that 8 lines of code..