Node.js architecture is all about managing the incoming requests and providing an appropriate response to each of those requests. This article dives deeper into the working of Node.js and the workflow of the whole server environment.
8. Node.js Bindings
• Glue code
• Code in different languages can interact with each other.
• Expose core C++ libraries to JS.
• Motivation
• Reusability
• Performance
9. C/C++ Addons
• Own Glue Code to include third party or own C/C++ library.
Think of Addons and Bindings as bridges between Node.js C/C++
and Your JS Code.
10. • Code compiled by V8.
• Communicate with low level node.js components.
• All events registered with node.js.
• Event triggered enqueued in event queue.
• Event loop dequeue events in the queue and putting them onto
the call stack.
11. How async callback works
Call Stack Node Core Libraries
(Libuv)
Event Loop
Event Queue
12. • Call Stack is JS Runtime (Single threaded).
• Executes the tasks.
• C++ libraries like Libuv make async programming possible in Node.
• Event Loop (Also part of libuv)
• Loops around JS Runtime.
• If JS runtime is free it pulls tasks from event queue to call stack so that JS
Runtime can execute them.
13. • I/O operation on call stack.
• Delegates tasks to libuv for processing.
• Libuv maintains thread pool for I/O operations And DNS related.
• Thread interact with Node’s low-level libraries.
• Enqueue event back to event queue.
• Libuv user multiple strategies to achieve asynchronicity depends upon
nature of function.
14. • Be aware of blocking code.
• Avoid long running functions that require long computations.
• Use ES6 generators in that kind of functions that will make code async. By
explicitly pause them and signal them to resume.
• Delegate CPU bound tasks to child processes.
• explicit multi-processing/multi-threading
• Clusters.
• Webworker-threads.
15. The End
• Thanks for listening
• Questions would be appreciated.