Effective NodeJS, part one

NodeJS, What for ?

nodejs-green

 

 

Node is not suited for CPU eavy applications, but it is OK for Rest server, chat server or web gammig: Node is made for processing lightweight requests.

 

One should consider Node for serving requests issued by single page applications written with Angular or Backbone. This is the case where Node is the best. But you can also use Node to generate HTLM using tools like Handlebar, and Sails lets you create application with MVC architecture. To be complete, streams allow fast processing of huge amount of data making Node a tool of choice for ETL.

 

♻ Effective application architecture

At this time of writing (February 2016), clustering a single Node app is the buzzword.  It consists of forking the Node process, HTTP connections are then spread around the children processes. To find out how this can be done, I invite you to read this post.

 

♻ Code : lesson I’ve learned

At my beginning with Node, I wanted a mean to hash passwords with a salt to store them in a database.  For this I first used a library found on npms.org. It was working well and quickly…. when processing one request at a time.
When I tried multiple requests, response times were terrible 😨.
What was going wrong ❓❓❓
The library I was using was doing all its computation in the event loop, thus preventing Node to process multiple requests in parallel 💡( you can find an introduction on event loop in my previous blog).

Knowing that, I removed the lib and wrote a hashing and salting function using Node’s Crypto module.

  • It’s more work
  • Response time for completing single request was a little bit longer than with the previous lib.
  • … Response time while processing multiples requests was actually better

The reason ? I was using asynchronous functions, which allow to spend less time in event loop, thus lets Node to process requests in parallel (to simplify, event loop was able to treat a request while a worker thread was computing the hash). This lead us to the golden rule when developing a Node app:

As far as possible, don’t overload the event loop

This can be achieved using some rules:

  • Always prefer asynchronous functions to synchronous ones. (I’m not always following this rule, but only when I write application initialization)
  • When comparing modules, prefer  those using asynchronous Node’s API or having their own C++ module
  • If you use to develop javascript on browser side, sorry but forget your favorite libraries. No matter how rapid they are, they are not design to fit event loop.

 

Lastly, if you have a code that keeps event loop busy for a while, you probably  haven’t good reactivity. There’s 3 ways to address this:

  1. Create a specific application that can be launched from the event loop. Here is NodeJS documentation for managing child process..
  2. If you’re courageous enough, you can create your own Node Addon.
  3. You can split your code in chunks using setImmediate or process.nextTick

 

I’ve not used child nor wrote addon yet, so I won’t break them down: my next post will be about what I know: it will dig into event loop and will explain how to use setImmediat and process.nextTick.

 

See you and have a nice code 🙋

 

Advertisements