Node.js 101: #1 Introducing Node.js: Blocking vs Asynchronous Non-Blocking

Node.js is one of programming language which become more popular today, especially in the era of Cloud Computing where all of your stuff are saved in a cloud and people can easily reaching (or accessing) the cloud to take or put something in it. It still gaining more programmer who are attracted with its simplicity and capability for scalable based network programming.

But of course, you can actually program network application with programming language you know, you can create many kind of web application with Java, PHP, Python, or even Ruby. But what sort of thing that make Node.js very special? Here is some point from what I know that make Node.js is special :

  • It based on Javascript (V8 Google Chrome Javascript engine to be exact). Today internet web interface is coded mostly in Javascript, and you can just master single language to create a full stack dynamic web application by using Node.js.
  • Because it uses Javascript, It would be asynchronous and non-blocking by nature. You don’t event need to specify thread creator literally in your code because Node.js use function callback mechanism by default.
  • Same like many other sophisticated programming language, it came with dozen of library via centralized package manager called npm (You can browse all the package via npmjs).

The first bad news that I would explain here about Node.js is that asynchronous non-blocking mechanism could frustating newcomer in first place because most of programming language they know use blocking sequential mechanism by default. So to be familiar with it, here I would explain to you about blocking sequential synchronous vs non-blocking asynchronous programming.

Blocking Sequential Programming

Here some example to make it clear.


In sequential programming, the call stack of this program would be : call a(), wait until a() done, then call b(), wait until b() done and call c() then wait until c() done. You see, all function we called is actually pausing the process of main program and redirecting it into the function program. Then when the function program exit (or returning value), It goes back into the next line of main program. This is the nature of most programming you might know.

What if I want to run a() in separate processing and don’t need to wait it to run next statement b()? Yes you can do that by using the Thread. Most of programming language has different mechanism to deal with thread management, and mostly all of your programming language that you know has a capability of it.

Threading creates a very complex problem called synchronization problem. To be clear, here I slighty modified the code (coded in pseudo-Python :D) :

valueForB = thread.start(a)  # Called in different thread

The problem is that because function a() is invoked in different thread, they might not returning valueForB yet the b function need it. You might encounter “variable undefined” or some error like that because that make sense if valueOfB haven’t setted yet. The solution is to tell the program to wait for a() function to returned the value before b() function is called to ensure that valueForB is setted correctly. But you know what? This problem is more complex in a complex program and you will deal with that kind of synchronization one-by-one.

Introducing (if you haven’t know it yet) a callback

For those who have been experiencing in programming in sophisticated language, you might recognize a term called callback. It actually the function (callback function) that passed as argument of function call so that the callback function can be called at some point from the main function.

This callback mechanism is the other solution of synchronization problem. Rather than waiting a() to be done before passing its return value to an argument of b(), why not we just put function b as an argument of a() so that a() could called it later at some point inside function a() definition.

# Main program
thread.start(a, (b)) # b is function that passed as argument of function a

# Inside function a(callback_fn)

def a(callback_fn):
  valueForB = 41

This could lead to non-blocking code and make the code clearer. The good news is, all high level programming language has capability of it. But the bad news is, you might don’t even know how to do this in the first place because the programming language you know may don’t use this mechanism significanly, except one very popular but mostly used in client side interface programming language called Javascript.

If you are experiencing with Javascript and some of its famous library such as jQuery, you will dealing with callback function more often with a bonus that you don’t need to explicitly define a thread creator. Here is a snippet code you might recognized :

$.post("/submission", function(data) {
    alert("I get the data");

alert("This called first");

The first function ($.post) is by the rule of top-bottom code strucure would be called first. It does an AJAX POST mechanism to a controller “/submission” then registering a callback function (which the function body defined directly inside the function call). The callback function won’t be called until the AJAX POST is successfully invoked and get its response, so the first alert message will be displayed is obviously inside the 4th line because the first function won’t blocking the second function to be called.

How it works actually is that the first function is automatically creating a thread (or something like that if that is actually not an OS or kernel level thread). You don’t even mention “thread” word in it and it just work like that, what a magic.

So far, you might only know this kind of magic inside client-side javascript programming. Why don’t we just create this thing to the next level of server-side programming? Yes, Node.js does make this magic come true to backend’er.

Introducing Node.js

Here I introduce to you a Node.js, claimed to be “software platform for scalable server-side networking application”. It has many library to deal with today web application such as :

  • Web Application Framework (express, geedy).
  • Web based Socket (
  • Web templating engine (ejs, jade, nunjuck).
  • Testing Framework (mocha, expresso)
  • Middleware Framework (connect)
  • Database Access (mysql, mongoose)
  • Object Relational Mapper (sequelize)
  • etc.

You can choose and combine some of that library by yourself easily and more clearly. From what I know, there is no kind of so-called full stack framework that can integrate some of you need seamlessly (like Ruby on Rails Web Application Framework) in Node.js. This is actually a good point because full stack framework has a lot of overhead when it come to performance. You may use a library that you don’t actually use.

Who uses Node.js? You can see some testimony at their website that Node.js has been used in large scale industry such as Paypal, Yahoo, Dow Jones, eBay, Linkedin and even Microsoft embraced it in their cloud platform. The power of Node.js comes from its simplicity, single language of Javascript, scalability, and its performance.

So why don’t you spent your time to learn it? It would very exciting. You can get started from this which has a basic tutorial to get started. And here in my blog, I will love to write an article about it regularly :).

To be continued

Leave a Reply

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


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Current ye@r *