Node.js Architecture
Overview
Introduction to Node
• Server Side.
• Single Threaded.
• Build on Chrome V8 Runtime Environment.
• Uses an event driven, non blocking I/O Model.
Architecture
V8
• JIT
• Written in C++
• Compiles JS directly into assembly code.
• Call Stack, Heap.
• Garbage Collector
• New space
• Old space.
Libuv
• Event Loop.
• Asynchronous I/O.
• Thread pool.
• Networking.
• Asynchronous TCP & UDP sockets.
• File system read/write.
Other C/C++ Components.
• Provide important functionality
• Compressing
• Encryption
• Asynchronous DNS requests
• Etc.
Application/Node.js API
• Application Code.
• Modules.
Node.js Bindings
• Glue code
• Code in different languages can interact with each other.
• Expose core C++ libraries to JS.
• Motivation
• Reusability
• Performance
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.
• 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.
How async callback works
Call Stack Node Core Libraries
(Libuv)
Event Loop
Event Queue
• 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.
• 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.
• 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.
The End
• Thanks for listening
• Questions would be appreciated.

Node Architecture.pptx

  • 1.
  • 2.
    Introduction to Node •Server Side. • Single Threaded. • Build on Chrome V8 Runtime Environment. • Uses an event driven, non blocking I/O Model.
  • 3.
  • 4.
    V8 • JIT • Writtenin C++ • Compiles JS directly into assembly code. • Call Stack, Heap. • Garbage Collector • New space • Old space.
  • 5.
    Libuv • Event Loop. •Asynchronous I/O. • Thread pool. • Networking. • Asynchronous TCP & UDP sockets. • File system read/write.
  • 6.
    Other C/C++ Components. •Provide important functionality • Compressing • Encryption • Asynchronous DNS requests • Etc.
  • 7.
  • 8.
    Node.js Bindings • Gluecode • Code in different languages can interact with each other. • Expose core C++ libraries to JS. • Motivation • Reusability • Performance
  • 9.
    C/C++ Addons • OwnGlue 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 compiledby 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 callbackworks Call Stack Node Core Libraries (Libuv) Event Loop Event Queue
  • 12.
    • Call Stackis 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 operationon 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 awareof 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 • Thanksfor listening • Questions would be appreciated.