Node.js architecture - overview

As per Wiki :

Node.js is an open-source, cross-platform JavaScript run-time environment for executing JavaScript code server-side. Historically, JavaScript was used primarily for client-side scripting, in which scripts written in JavaScript are embedded in a webpage’s HTML, to be run client-side by a JavaScript engine in the user’s web browser.

Introduction

Node.js is a JavaScript runtime built on Chrome’s V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient.

It’s an asynchronous event driven JavaScript runtime, which is designed to build scalable network applications. It can handle many concurrent connections at a time, where when connection request are made concurrently for each connection a callback is fired. If there is no task to be performed Node will go to sleep.

Node.js connection handling mechanism is super efficient compared to our existing classical thread based model. Thread-based networking is relatively inefficient and very difficult to use. Furthermore, users of Node.ja are free from worries of dead-locking the process, since there are no locks. Almost no function in Node directly performs I/O, so the process never blocks. Because nothing blocks, scalable systems are very reasonable to develop in Node.js

Architecture

node.js architecture

Node.js follows Single Threaded with Event Loop Model. Node.js Processing model mainly based on JavaScript event based model with JavaScript callback mechanism.

  • V8 Engine

It’s an open source JIT(Just In Time) compiler written in C++ which has outperformed PHP, Ruby and Python performance wise.

  • Libuv

This C++ library that handles Node’s asynchronous I/O operation and main event loop. There are thread pool reserve in Libuv which handles the thread allocation to individual I/O operations.

  • Other Low-Level Components c-ares, http parser, OpenSSL, zlib, and etc, mostly written in C/C++.

  • Application Here is your code, modules, and Node.js' built in modules, written in JavaScript (or compiled to JS through TypeScript, CoffeeScript, etc.)

  • Binding Binding basically is a wrapper around a library written in one language and expose the library to codes written in another language so that codes written in different languages can communicate.

NPM

NPM, node package manager is an official open source package manager for Node.js written purely in Javascript.

Pros

Following are some prominent highlights that make Node.js the first choice of software developers specially backend.

  1. It is asynchronous and event driven All APIs of Node.js library are asynchronous, that is, non-blocking. It essentially means a Node.js based server never waits for an API to return data. The server moves to the next API after calling it and a notification mechanism of Events of Node.js helps the server to get a response from the previous API call.

  2. Super Fast Being built on Google Chrome’s V8 JavaScript Engine, Node.js is super efficient and quick in code execution.

  3. Single Threaded but Highly Scalable Node.js uses a single threaded model with event looping. Event mechanism helps the server to respond in a non-blocking way and makes the server highly scalable as opposed to traditional servers like Apache which create limited threads to handle requests.

  4. No Buffering Node.js applications never buffer any data. These applications simply output the data in chunks.

When to use Node.js?

Node.js as a platform fits and is typically used when the requirements include

  • I/O bound Applications
  • Data Streaming Applications
  • Data Intensive Real-time Applications (DIRT)
  • JSON APIs based Applications
  • Single Page Applications

Worth noting is that Node.js also represent a "JavaScript" everywhere paradigm, and there is a lot of software developers that are familiar with JavaScript.

References


Published: 2018-01-18
Author: Henrik Grönvall
Henrik Grönvall
Copyright © 2022 Henrik Grönvall Consulting AB