Node.js Architecture
Node.js is a runtime environment which execute javascript code outside of the browser. It is an event driven, Non blocking I/O architecture
It does not have multiple thread to multiple task. The threads are higher in cost and make the system expensive. It is design to handle concurrent request using single threaded event loop.
It is the combination of :-
V8 engine :- It compile the javaScript code into machine code.
libuv :- It is a c library which mainly handle async operation.
Node.js API :- fs(file system), http, crypto
Operating system service :-
event driven architecture
V8 engine :-
V8 engine is develop by google
It is responsible for :-
Execute javascript code
convert js code to machine code.
memory management and garbage collection
Internal component of V8 engine.
1. Parser :-
The parser parse the js code
It read the code
check for the correctness of the code
parse and convert the code into Abstract Syntax Tree(AST).
2. Ignition compiler : -
The Ignition compiler is known as the V8's interpreter.
It interpret the code and convert the code into bytecode.
The code become the Bytecode instruction.
3. TurboFan :-
The TurboFan is an optimized compiler. It compile the bytecode into the machine code and execute the program.
4. Garbage collector :-
It collect and free the unused memory.
const user = {
name : "XYZ",
address : "ABC",
pincode : 203310,
}
user = null
Now the user get null value and is not further in use so the garbage collector free the memory of the user.
Feature of V8 engine :-
JIT compiler : - Just In Time compiler, it compile code in machine code for faster execution of the code.
Garbage collector :- JS automatically free the memory that are not used for long time or not reachable at the time.
Run time optimization :- It detect the frequently used function and optimize it.
ES6+ support :- The modern support of classes, promises help to make it more effective.
Importance of V8 Engine :-
High Performance: - V8 's JIT compilation and optimization enable the node.js to work with the concurrent process efficiently
Asynchronous Operation : - V8 engine work with non-blocking async operation.
Efficient Memory management :- V8 engine free the memory which are not used for long time and are unreachable. This make it memory friendly and prevent from the memory leak.
Cross platform consistency :- V8 engine can work both on Node.js and browser.
LIBUV :-
LIBUC is the cross platform library of the C language used by node.js to that provide event loop, async I/O operation, thread pool to perform non-blocking async operation.
It provide common interface to handle following task : -
file system operation.
DNS Lookup
Networking
Child process
signal handle
Why it is necessary : -
Imagin, a process start executing : -
Read a file
query a database
call a API
The following thing happen
libuv solve this problem using non blocking event driven architecture.
Event driven model :-
libuv work on event driven model :-
let assume a program call a database. The CPU at that time
Request database --------> wait -------> wait --------> complete
At this point of time cpu wait and do nothing. This is the wastage of cpu resources. Here event driven model use the resource efficiently.
Request a database ------> perform other task -------> database respond ------> callback execution.
Main feature of libuv :-
Event Loop :-
It is the heart of the Node.js.
It perform asynchronous operation.
It execute the callback operation when all execution completed.
It use single-threaded async I/O model
It check :
Is event queue empty
If event queue is empty execute call back operation.
Thread Pool :-
Although Js is a single threaded model, but to work on async blocking model or that can not be handled asynchronously. it has 4 worker thread(default).
The worker thread perform multiple task such as :-
Image processing
Video processing
AI computation
large computation
It can be increased using UV_THREADPOLL_SIZE environment variable.
Cross Platform Access :-
It provide common API for different Operating system, such as
Window
macOs
Linux
other unix like system.
Asynchronous Operation :-
It perform asynchronous operation like :-
File system operation
Networking
DNS lookup
child process
Timer
How it perform :-
Advantage of libuv :-
Single threaded system.
Non blocking architecture.
The blocking piece of code are handled by thread pool
Cross platform access.
high concurrency.
Callbacks :-
When all async operation completes,
setTimeOut(() =>{
console.log("hello");
});
It came into the callback queues, and start executing.
