Asynchronous Non-Blocking Backends – Node.js is cool

I am beginning to have a liking for the new stuff everyone is buzzing about. The server side V8 based javascript framework called Node.js.

In the start when i heard about it at a hacker’s get together, i thought its another one of those, cool buzz words framework which makes you cool and hip if you use it. However after playing around with it for a bit, it seems pretty nice.

The idea is simple. if you are in the business of writing High Performance Web-Servers or backends, you know you have to be God to get the concurrency right in lets say Java. Concurrency is one of the hardest things to do right in large systems. Its easy to do it wrong. You have to write specific multi-threaded code to be able to do it, or you have to use frameworks like Scala + Akka or Java + Akka

What if you can write a backend, which does all the concurrency by it self. Your code never blocks, and everything is based on Event-Driven callbacks ?

I am sure if you are in the business of writing an HTTP or a TCP server you’d love to have that, and it sounds very familiar right? i mean if you ever programmed with Ajax or jQuery, it’s the same concept. The only difference is , it is done on the server side, and it is done using “javascript” and it runs on the Google’s phenomenally fast javascript engine called V8.

Now i know some of you will say javascript on the server side is a terrible choice. Maybe you are right, but what if you are a web-programmer who codes in Javascript for a living, for you to learn C++ or Java and then try to write a high performance backend using some weird looking multi-threaded APIs would be a nightmare.Node.js allows you to write a high performance backend, lightning fast, no need to worry about concurrency everything is taken care of.

Lets say in Java your code will go something like …

List data = dataService.getAllData(pending);

data.doSomething();

// Something else happens

Node.js equivalent will look like

dataService.getAllData(pending, function(data) {

data.doSomething() ;

}

// Something else happens

In the Java example the “Something Else” will always execute *after* the data.doSomething(). So your thread blocks. In Node, the execution continues and “Something Else” will normally be executed before data.doSomething(), and data.doSomething() will only be executed after getting all data is done.

Now lets look at how concurrent this can be. Lets steal a simple example from the Node.js website and put it through Apache Bench to see how concurrent can it be.

Since Node.js comes with a built in HTTP, TCP, DNS and many other servers, we’ll use the built in HTTP to do a basic benchmark.

You can see that even by increasing the load on the server exponentially, it is serving concurrent requests nicely, and does not increase the time exponentially, or effects how many requests per second i can serve with my server, and all of that without having to write any specific code using threads or workers or Actors or whatever the hell you use to write concurrent code.

You can learn more on the website , and the creator of Node.js has an excellent talk on youtube here