Node.js – What is it?

Node.js is simply server based JavaScript.

Some, including Intellij, refer to it as a framework.  Yet others want to be clear that Node.js on it’s own is not a framework.  There is a web framework called Express.js.  Express.js provides the MVC architecture required to build web apps in Node.js.

Why should we care about Node.js?

I hated JavaScript.  I hated JavaScript with a passion. I can not stand looking at JavaScript.  But then I met a Node.js evangelist.  He began telling me all sorts of cool features and aspects of Node.js.  I thought, “why not use Rails, or Grails, or Java with Spring?”  He told me the most crazy stories… he claimed Node.js had amazing performance due to it’s non blocking single threaded design.

But wait, single thread?  Doesn’t that mean we aren’t utilizing multi-threaded processors?  It does… but then web apps typically are not processor heavy applications.

I started digging around, and I saw more people all jazzed about Node.js.  Part of me wondered, “is it because it’s new?”  Then I happened across a fellow who claimed Node.js Rest services he created performed better than those he created in Java.  His blog post was very controversial and brought a lot of heat from the Java guru’s… who went after him saying his Java Rest service wasn’t utilizing various optimizations, etc.

Real World Example: Paypal

I happened across a real world example… Paypal.  It appears that Paypal moved off Java/Spring to be solely a Node.js backend.  That’s pretty crazy.  We have a high profile company that’s very profitable, takes on high traffic and must be accurate as it’s handling money… and they moved off Java to go with Node.js?

The full article is here: https://www.paypal-engineering.com/2013/11/22/node-js-at-paypal/

The highlights of the story are:

  • Rebuilding Paypal in Node.js was twice as fast to code
  • It utilized less people to build it
  • Lines of code shrank 33%
  • Requests per sec doubled, compared with the previous Java app
  • 35% decrease in the average response time (200ms faster)
  • They could run this on a single processor server (rather than 5 CPU servers with Java)

It sounds like a Fantasy.  How can a scripting language outperform a compiled language?  How can a JavaScript based language be so effective in performance?  Less code I understand, but this performance is insane.

The main reason people harp on Java as the language of choice over Rails, Grails or Django, is that Java (with Spring) is a much faster performer.  This is a constant theme of Java guru’s and elitists. I’ve been slammed for using Ruby for testing Web Apps by Java nutters because it’s not uber fast like C or Java (even though it didn’t apply because the testing launches a browser and that becomes the bottleneck.)

Now you have a simple scripting language, while looking ugly too me (compared to Python, Groovy or Ruby) is able to offer such great performance that Paypal now replaces Java with it.

Real World Example: Netflix

Another real world example can be found here: http://techblog.netflix.com/2014/11/nodejs-in-flames.html

This particular article is more of a performance tuning article on Node.js.  It doesn’t expose any view on how Node.js is a better performer than their previous language choice (which isn’t mentioned.)  I list it here for reference that Netflix is also utilizing Node.js to some capacity.

How Does Node.js Work?

An article at toptal.com gives a high level concept of what is good and not good about Node.js.

The author mentions that Node.js uses non-blocking, event driven I/O that is lightweight and a good performer for data driven real-time apps. Chat apps, streaming media, web applications… These are all good examples of how Node.js can thrive.

The author at toptal.com warns against using Node.js in all circumstances.  Any app that is CPU heavy will be better served with a multi-threaded approach, in order to make it more scalable.

Node.js uses a single thread.  Since each thread that spawns in a regular web app consumes memory, a single thread consumes less RAM.  How Node.js can handle issues of concurrency (where you have tens of thousands of simultaneous calls), is by making the calls non blocking.

These concurrent connections are managed in an event loop.  That way the concurrency is resolved through a single thread. In the example at http://www.toptal.com/nodejs/why-the-hell-would-i-use-node-js he writes out some calculations, that with 8GB of RAM in a multithreaded environment (where each thread consumed 2MB), 4000 concurrent connections could be created.  Yet with the model that Google employed for Node.js, they can handle levels over 1 Million concurrent connections – all on a single thread.

For a deeper dive in how the Node.js Model works, check out the accepted answer at StackOverflow: http://stackoverflow.com/questions/14795145/how-the-single-threaded-non-blocking-io-model-works-in-node-js

Async Event Looping (Akka and Node.js)

When I first read the details on how Node.js worked, I thought: Hey wait, doesn’t Akka do that?  Yeah, and there’s an article comparing the difference between Akka and Node.js: http://www.quora.com/How-does-Node-js-compare-to-Scala-and-Akka There are some great comments on that question at quora.

Again, the theme of CPU intensive tasks require a different solution than Node.js.  However, if the task is not CPU intensive, but more of a data handling/socket streaming task, than Node.js performance is on par with Akka.

Negative Aspects of Node.js

Again the reoccurring theme warns that CPU intensive applications could be the death of a Node.js server.

Node.js is a pain to use with relational databases.   If you’re going to use a relational Database, it is often suggested to use another approach such as Rails, Django and I would add Grails and Java with Spring.

 

Leave a Reply

Your email address will not be published. Required fields are marked *