[Node.js] Intro to Node.js

1) What is Node.js?

  • JavaScript without browser (eg. server-side scripting, scripting); represents "Javascript Everywhere"
  • Unifying web application development around a single programming language (rather than different languages for server and client side)
  • Event-driven, non-blocking I/O model - lightweight & efficient.
  • Package ecosystem, npm, is the largest ecosystem of open-source libraries in the world.
  • Open source (free)
  • Built on Chrome's V8 Javascript engine
  • Is "fast" because it uses small amount of threads to handle many clients via asynchronous calls. However, it is only effective if each work/task is small.

2) Javascript background

Generally considered an Interpreted language, but modern Javascript engines compiles it as well (since 2009). Javascript internally compiled by V8 with just-in-time (JIT) compilation to speed up execution. While it might take a bit more to have Javascript ready, once done, it's more performant than purely interpreted code.

3) Confusion - Single or Multi Threaded?

Javascript is single or multi-threaded? This is often confusing.

Firstly, putting aside multi-threading/processing (via worker threads and clustering), the NodeJS stack/environment only have 1 execution of javascript and that runs on 1 thread; which is the single-threaded event loop. But, there are other threads (managed by libuv) in NodeJS that handles asynchronous operations, most famously the I/O operations. This is what makes NodeJS multi-threaded. A more in-depth comment is these asynchronous operations happen only when we call a library that utilizes them; more will be explained in another post.

In short, there is only 1 execution of javascript, but there are other threads running in the NodeJS stack/environment to make it more asynchronous.

Secondly, multi-threading and processing via worker threads and clustering makes it more 'multi-threaded'. Note that the only way to make > 1 instances of javascript running is to do multi-processing.

3.1) Libuv

Libuv is a C++ library that provides support for asynchronous I/O based on event loops. It is capable of multi-threading. Node.js uses libuv to operate single-threaded event loop and worker threads.

Without libuv, NodeJS is just a synchronous Javascript execution.

3.2) Single-threaded event loop

While the event loop is single-threaded, I/O operations are actually delegated to libuv from the event loop. When I/O operation are completed in libuv, it runs the callback waiting on this event and pushes it back to the main thread.

Under the hood, libuv by default creates a thread pool with 4 threads to offload asynchronous work to. Today's OS already provide asynchronous interfaces for many I/O tasks (including access to third-party systems, like database). Whenever possible, libuv will use those async interfaces, avoiding the usage of thread pool.

3.3) Worker threads

Separate threads used to compute CPU-intensive tasks, because if used in event loop, it will block the entire event loop.

Comments

Popular posts from this blog

[Redis] Redis Cluster vs Redis Sentinel

[Unit Testing] Test Doubles (Stubs, Mocks....etc)

[Node.js] Pending HTTP requests lead to unresponsive nodeJS