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 🙋

 

NodeJS, Single-threaded? Not Single-threaded

I still heard workmate saying that NodeJs is mono-thread. Well, it’s not quite true…but not quite wrong either.

img-211161171f3

 

 

 

 

Node’s heart is its javascript engine, which is mono-threaded. But other parts of node are in C++ and run in a thread pool.

Let’s imagine a basic Rest API code which receives a request, gets data from a database and returns those data in json. The exchange with the DB is excuted by a thread of the pool so that the javascript thread is able to process another request in parallel.

The thread is often named as the loop event thread and it’s genuine role is to process events. A schema, that can be find in multiple places on Internet, offers a good overview of threads interaction:

nodejs20for20dotnet

Have you notice the use of callbacks ? Whenever event loop calls an asynchronous function has the result, it calls the callback with the result. Behind an asynchronous function, you can find a C++ pool thread code.

Here is another Internet schema to clarify this:

threads-in-node-ja_

Node manages all the plumbing between javascript and C++: all this is unseen by the developer. Yet, Node developer must have this in mind to write effective code. My next posts will explain why and how to write scalable code.

See you and have a nice code. 🙋