To understand node js architecture, we need to understand the working of node js.
Node Js Architecture describes the working of node js, how node js work, are you reading at office or home, fold your sleaves ang get ready for 3 min glimse.
Node js is single threaded language and reactor pattern is heart of asychrononous in Node js.
Asychronous is Non-blocking() ,you and your team will not stop you till plant a tree operation is successful, it's possible that your friend Abhishek has planted a tree and you are still digging the whole, you started parallelly, for example you are picking the customer info from database, the email operation will be start in parallel and it will run together the complex task will take time and easy one
sending one email will be performed early.
Reactor pattern represents the handler or callback to handle i/o operations. The callback will be invoked when an event processed by the event loop.
There are a lot of misconceptions around the event loop of nodejs, and reactor pattern elaborated about the event loop.
1-We have a game developed with node js for example
call of duty, it has names like james, who works on
the army they have thier life too. When game(app) sends a i/o request to the handler and handler is part of event demultiplexer and handler returns the message to your application after affect from bumb, the health decrease message. When the i/o operation completes(In case of call of duty)- Gun Sound, hitting the target,missing the tarket and updating the health, capsule then increasing the health etc.
2-Event demultiplexer has a hander (callback function), and event demultiplexer outsource the events into the
event queue. The Event Loop iterates over((print)) the items of the Event Queue.
When event queue is full, it blocks the event demultiplexer and event loop is performing the operation of event and associated handler from event
loop parallelly when task is complete ,app sends the control back to event loop, app means call of duty game. You will understand more clearly with this diagram.
Libuv (the I/O engine of Node.js)-
It is a pure c library and it's not a thread,and in order to simulate non-blocking behavior, it is necessary to use a separate thread outside the event loop.
We have thread pool which is collection of pre created
threads ,and parallel execution context (called runner) is additional thread, now node js breaks it's principle node js is a single threaded language.
The reason to create libuv is Node.js compatible with all the major operating systems to provide os level async i/o. Libuv portrays the low-level
I/O engine of Node.js and it is most imp component of
node js.
libuv also enacts the reactor pattern, it provides API for creating event loops,mananging the event queue, and running asynchronous I/O operations, and queuing other types of task.
** Event loops**
The responsibility of gathering events from the operating system or monitoring other sources of events is handled by libuv, and the user can register callbacks to be invoked when an event occurs. The event-loop usually keeps running
forever. In pseudocode: you have seen a while true
loop and loging on console runs always.
Google V8 Engine:
It's javaScript runtime enviromment for executing the
javaScript code from outside browser and inside server. It is orignally developed for google chrome by google team and that is the reason node js is fast.
Who will help the v8 and libuv library to talk with each other, the answer is binding ,the code responsible for this purpose. An example is a binding for database libraries.
V8 has no native understanding/cognition of databases, and database libraries don't care who uses them, so a binding is needed to make sure the two can talk to each other.
V8 engine leverages the Just In Time(JIT) compilation pipeline design pattern to execute Js code that is the reason ,js is fast.



Top comments (0)