Node.js, its awesome!
Tags: node, microservices
Published: 2020-05-01 06:43:51

_checkout what makes Nodejs so popular_

### What is Node.js?
Node.js is an open-source and cross-platform JavaScript runtime environment. It runs the V8 JavaScript engine, the core of Google Chrome, outside of the browser.
Node.js uses event-driven non-blocking I/O design, which makes it very performant while reducing CPU and memory usage. A Node.js app runs on a single process, and it doesn't create new thread for every request like other server side languages. There is only one OS process running the event-loop, the core of Node.js application. This event-loop runs your code, and registers callbacks for asynchronous non-blocking I/O. This makes it a suitable candidate for highly scalable containerized applications and microservices.

### What is V8 Javascript Engine
V8 Javascript engine is a javascript runtime developed in C++ for compiling and executing javascript code in Google Chrome. This is what runs the javascript code from the webpage you're viewing with Chrome. V8 is always evolving, just like the other JavaScript engines around, to speed up the Web and the Node.js ecosystem.

### Why is non-blocking I/O important?
There is one key understanding here. _The speed of an application mostly depends on the speed of the I/O devices_. Read and writes from the main memory(RAM) is the fastest(leave aside CPU registers and caches for simplicity), followed by Disk reads and writes(reading from a file or local database server), followed by Network level I/O(like a HTTP request, database server requests). This is why applications are loaded into main memory, rather than keeping it in disk before executing. Extensive graphical applications loads the data into the GPU memory for faster access by the GPU, as GPU memory(VRAM) provides more than 10X speed in sequential access than RAM.

But a web application needs to access Network I/O even though its extremely slower than RAM I/O. So in a threaded model like PHP, Python the thread __waits for the I/O method to complete__. That is why they need multiple threads to handle concurrent requests, where one thread is allocated for every request. But with increase in number of threads comes three main issues
* Increase in memory usage(RAM)
* Possibility of deadlocks
* Possible issues in thread concurrency
The second and third one is prevented using careful designing and deadlock detection and breaking methodologies, while the first one needs machines with more RAM, and/ or clustering.

### Specialties of Node.js
Because of its event-driven non-blocking I/O nature, __it doesn't wait for the I/O to finish but registers a callback__ to happen when the I/O completes. The event-loop then continues performing other tasks. Node.js internally uses thread pool to manage callbacks and event notification. It will be notified by the callback with an event when I/O method finishes, and it can continue from there. Due to this single threaded nature it provides the ability to
* Handle thousands of concurrent requests without increasing burden on the main memory
* No possibility of a deadlock
* No issues with thread concurrency

Because of its Javascript(one of the most popular language for the web) based nature, it provides the following advantages
* Large collection of user libraries
* One language in both server and client side

### Threads with Node.js?
Although Node.js is designed with single threading in mind, _you can also leverage the power of multiple cores when you want_. You can create child processes, manage clusters with simple interfaces. For the extreme cases(like machine learning, complex computations), you can use multi-threaded libraries in C++(the fastest language) as an addon library. The C++ addon libraries provide interface for Node.js applications to use them like regular Node.js libraries. Much like how Tensorflow(written in C++) exposes interface for Python to use it like a python library.


### When to use Node.js?
Because of its simple asynchronous nature and its event-driven model it has preffered choice for applications of different types and scale.
* Highly scalable network applications, microservices
* Reducing number of servers while maintaining network throughput
* Increasing the ability to handle large number of concurrent requests

From this post you can see why Node.js is becoming the first choice for large scale network applications and services. Many companies have ported over their codebase to Node.js from other languages and have significantly reduced the number of servers, and increased ability to handle concurrent requests while doing so.

### Wondering if your application idea is suitable for Node.js?
I am a software developer and a skilled problem solver wih good experience in web technologies. I build web applications, scalable APIs, business management systems. Let's discuss your project and we can understand specific needs of your system. Before crafting your idea into application code, I can also help you get a good overview of your system.

Regards,
Rituraj Borpujari
[https://in.linkedin.com/in/riturajborpujari](LinkedIn profile)