“Node.js goal is to provide an easy way to build scalable network programs” → Ryan Dahl, creator of Node.js.
Understanding the Node environment is crucial to building scalable systems with it. The first principle to remember is that the Node.js engine chooses clarity and simplicity over complexity and unpredictable codes.
Therefore, here’s a simple summary of the Node.js environment.
There is one thread bound to an event loop.
Deferred tasks are encapsulated, entering and exiting the execution context via callbacks.
I/O operations generate event data streams that are piped through a single stack.
Concurrency is managed by the system, abstracting away thread pools and simplifying memory management.
Dependencies and libraries are introduced through a package management system, neatly encapsulated and easy to distribute, install and invoke.
What problems are Node.js Solving?
How to serve many thousands of simultaneous clients efficiently
Scaling networked applications beyond a single server.
Preventing I/O operations from becoming bottlenecks.
Eliminating single points of failure, thereby ensuring reliability.
Achieving parallelism safely and predictably.
N/B: Managing concurrency in connection handling and application design is the key to building scalable web architectures.
Reasons why JavaScript is Node.js language
When Ryan Dahl designed Node.js, JavaScript was not the language of choice. But after exploring languages, JavaScript was a good modern language without opinions on streams, the filesystem, handling binary objects, processes, networking, and other capabilities expected from a system’s programming language.
JavaScript was only limited to the browser. Therefore, those features were not implemented, making it easy for Node.js to implement and extend JavaScript.
Node Rigid Principles
Here are some of the rigid principles that guided Ryan Dahl in choosing JavaScript
A Node Program/process runs on a single thread, ordering execution through an event loop.
Web applications are I/o intensive, so the focus should be on making I/O fast.
Programs flow is always directed through asynchronous callbacks.
Expensive operations should be assembled from simpler programs.
The general principle of Node.js is:
Operation Must Never Block.
Node.js is built for speed (high concurrency) and efficiency (minimal resource usage) by reducing waste, and a waiting process is a wasteful process, especially when waiting for an I/O operation. Therefore, JavaScript's asynchronous, event-driven design fits neatly into this model (Node principle).