Node.js is an open-source, cross-platform JavaScript run-time environment (Google Chrome's V8 Engine) that executes JavaScript code outside of a browser. Node.js lets developers use JavaScript to write command-line tools and for server-side scripting—running scripts server-side to produce dynamic web page content before the page is sent to the user's web browser.
Official definition: Node.js is a platform built on Chrome's JavaScript runtime to easily build fast and scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.
Node.js is quite often used to build back end services like API, which power different client applications like the web app and mobile app running in mobile. Node.js helps to create highly scale scalable, data-intensive and real-time apps.
Why should we use Node.js as there are a lot of other tools and framework for building back-end services like Asp.net, rails, Django etc.?
Node.js is great in Prototyping and agile developments, building super fast and highly scalable services. Some of the companies that use node.js heavily are Netflix, Walmart, PayPal to name few. In fact, PayPal has rebuilt one of its application based on java and spring into node.js. And they ended up building it twice faster with fewer people, 33% fewer lines of code, 40% fewer files, 2x request/sec and 35% faster response time.
Given that many companies use it, let’s understand in detail what makes it useful.
Any typical web application making a service call which needs to fetch/update data in a database in the back end works like this:
User does an action -> application starts processing action -> Makes a DB call -> WILL WAIT TILL REQUEST IS DONE -> Request complete -> Send back result to user.
In case of a multi-thread application, the same scenario is repeated for the multi-thread
As we can see that whenever the thread is waiting for a response from DB, it spends 0% CPU. Ideally, we would like to run some other code when this happens. And in case of multi-threads, we have to allocate the memory for each thread.
However, Node.js being a single-threaded application can still work with 10K concurrent requests. This is because of the event loop mechanism that it has [will learn more about it in later topics]. If we take the same example of multiple requests in node.js case, this is how it works.
If we compare multithreading with node.js, both take roughly the same latency as most of the processing is on the DB side. But the advantage in the case of node.js is that we need not spawn threads, plus the cost of context switching between threads and do lots of mallocs which slows down.
This may sound surprising but node.js being single-threaded is able to match with multithreading because of the event loop and node.js capability to leverage the multi-threading of database. Some of the cases where multithreading fails and node.js wins is when we need to allocate a lot of RAM per thread. And allocating lots of objects which is common in modern web frameworks, will further slowdown.
This does not mean that we can use node.js in all places. Node.js is not good for intense computations/processing, which does a lot of CPU calculations. And another case is when we are having multi-core servers, we don’t use servers to full capacity as node.js is single-threaded, using only one core. Node.js is not meant for blocking operations and most of the cases of node misuses are because of this.
Some application where node.js suits better are CHAT, BROKERAGE – STOCK TRADER’S DASHBOARD, MONITORING DASHBOARD (and SYSTEM), DATA STREAMING, QUEUED INPUTS, API on Object DBs (MongoDB). Will touch based on this kind of applications with examples.
Node.js is programmed in Javascript, so Javascript is used for both front and back end services. It is cleaner and a more consistent codebase. More importantly, it has the largest ecosystem of open source libs.
All browsers have a JS engine which converts javascript code to machine code. For example, different browsers have different engines like IE uses Chakra, firefox uses SpiderMonkey, chrome uses a V8 engine.
In 2009, Ryan Dahl had taken V8 engine out of the browser and embedded it in a C++ program and called it as Node.js. Similar to a browser, node.js provides an environment to run JS engine but it can’t work with document object like a browser for accessing elements in DOM. Node.js has other capabilities like working with a file system, networking by creating a server etc., which opens a lot of other capabilities.
Node.js is not a programming language like C# or a framework like asp.net. It is a run-time environment, which executes javascript code outside the browser.
Going further on this topic, it requires some javascript knowledge for which refer to Javascript primer. [Other Document]
JavaScript is a dynamic computer programming language for the web. Jav...
Introduction: Angular (What is Angular?)Angular was formerly introdu...
Leave a Reply
Your email address will not be published. Required fields are marked *