Philosophy behind NodeJS


In this section I will try to present philosophical aspects behind NodeJS covering design principles and programming techniques inspired its development.

  1. Hollywood Principle: “Don’t call us we will call you”. To understand this here’s a small story – a struggling actor goes to some director and ask for role, currently the director don’t have any suitable assignment for him, and he don’t want to be bothered again and again, so he asked actor to leave his contact detail to his assistant and says – “don’t call me I will call you if some assignment is there”. This principle initiated the callback mechanism, whereby one task depending on other task for some processing, simply submit request along with a callback method/function to be called by other task when result is available. This way tasks do not block important resources like ‘thread’ in waiting.
  2. Asynchronous Call: To understand this first understand that ‘asynchronous’ is the state of being ‘not synchronised’; synchronous process generally wait for final result to be calculated whether success or failure. In asynchronous call you submit your request along with callback handler that must be triggered when result is prepared and returned, your process doesn’t wait rather continue with its flow. To achieve this ofcourse threads are required but that will be handled by nodejs for developer its single threaded programming model.
    Example: executeQuery("select empid from employee", resultHandler(data))
    Here your resultHandler is a callback handler.

    Please see http://blog.mixu.net/2011/02/01/understanding-the-node-js-event-loop/ for more detail on single thread model.

  3. Non-Blocking IO: NodeJS is suitable for IO intensive application rather computation intensive application (more on this can be found in google). Non-Blocking IO means your program execution flow is not blocked for any IO operation and this is also achieved through callback. Javascript support’s callback implementation but didn’t have any IO API, for node non-blocking IO implementation was added, which is available for developer out of the box.
    Example: executeQuery("select empid from employee", resultHandler(data));
    displayProcessingWindow();
    Here you can see displaying process running info to user is called after db call is made; and this will display process window because your execution flow is not blocked by the call of ‘executeQuery’, also you are not doing anything special to achieve this it will be handled out of the box.
  4. Multiple Request Single Thread: Javascript follows single thread programming model, means there is no way you can spawn new thread in your code/implementation, and this is done to achieve simple programming model. Parallel execution is handled out of the box. Node assume that any call other than IO will be completed immediately, that is why it’s not recommended for computation intensive tasks; as IO operations are non-blocking it will not halt your execution flow. (IO operations are handled parallel in the background using thread)

Conclusion: Hollywood principle is the key design principle behind non-blocking implementation, and callback and event loop is the technique to achieve asynchronous/non-blocking behaviour.

Purpose

  1. Node is designed to be used for applications that are heavy on input/output, but light on computation (computation intensive task should be delegated out of node).
  2. To leverage single thread programming model (for developers) to achieve simplicity, while handling complex multithreading in the background out of the box.
  3. To reduce memory overhead that is inherent in request per thread model.

References

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s