Sunday, August 23, 2015

About NodeJS

NodeJS is a Javascript core interpreter which is executed at the server side. The core of NodeJS is the V8 Google Chrome's open source Javascript engine (which is written in C++).  


Why would we need to execute Javascript at the server side? Well, Javascript is just the programming language of the scripts to interpret, so let's see which are the benefits of NodeJS.

NodeJS is: 

  • Non-blocking: handles incoming requests and waits (sleeping) for new events to manage (despite the response has not been already sent). 
  • Asynchronous: it does not block resources, just performs actions and waits for new events to execute a callback function.
  • Event-driven: when an event is fired, NodeJS handles it, and waits for more events. 
  • Single-Threaded: A NodeJS instance is a process with just a single thread (despite you could spawn child processes to take advantage of multi-core CPUs). 
These features are what make NodeJS so great in performance (it can handle thousands of concurrent connections). 

For all the things exposed previously, NodeJS may be suitable for: 
  • Build fast, scalable and real time web applications (social networks, chat services, PaaS monitoring and management, etc).
  • Develop non CPU intensive processing services (CRUD microservices, etc). 
  • Provide an entry point (reverse proxy, smart load balancing) for cloud applications.
On the other hand, NodeJS should be avoided for CPU intensive applications (the throughput would decrease) and applications with a lot of business logic with multiple database queries (because of the complexity associated with managing this scenario in an asynchronous way).

The NodeJS ecosystem is composed by a lot of tools and packages to ease development of high-availability solutions: 
Now we have Javascript at presentation layer (navigator context, through jQuery, AngularJS, etc) and at server-side processing (NodeJS with Express, etc). The last area to find Javascript is at database level, and here comes MongoDB

MongoDB is a NoSQL database management system, which provides horizontal scalability for the storage system, online querying of big data, data replication and high availability for the persistence layer. 

With Javascript at all application layers (presentation, processing and storage) we have a full Javascript stack, which is known as MEAN (MongoDB, Expres, AngularJS, NodeJS), and uses JSON as the data interchange format. 

To conclude, NodeJS can be used to develop CRUD microservices with few business logic, a smart routing / reverse proxy layer, or bidirectional web applications with push capabilities (chat services, platforms monitoring, etc). 

The low resources consumption of NodeJS, makes it a great player for internet of things (IoT) devices management and monitoring, as well as robots or drones communications implementation. 

We should avoid using NodeJS for web applications with a lot of business logic processing or CPU intensive computing (performance will be bad under these conditions and programming could be quite difficult because of the event-oriented nature of NodeJS). 

Another key aspect to take care of when programming with NodeJS is that we must take care of exception handling, because the main process can stop if we don't manage correctly thrown errors.