Skip to content

Instantly share code, notes, and snippets.

@PrakadAlpha
Created March 30, 2020 08:18
Show Gist options
  • Save PrakadAlpha/000aef27830d4a320e277263879deb64 to your computer and use it in GitHub Desktop.
Save PrakadAlpha/000aef27830d4a320e277263879deb64 to your computer and use it in GitHub Desktop.
Node Architecture and Performance Analysis
Threads:
It consists of intructions to be ran on a process in the cpu..It is a part of thread
An OS schedular tells which threads should be processed
Process:
An instance of a computer program
A process can have multiple threads
Cores(cpu):
A Cpu can execute more than one thread a time, this is called multi-threading or hyper-threading
More the cpu cores number of threads can be executed increases
I/O Intesive Tasks
- Refers to using file system and network connections
- Async FileSytems,DNS are handled by worker pool in libuv
CPU Intensive Tasks
- Zlib, Crypto, Stringify, parse are handled by worker pool in libuv
NODE IS SINGLE THREADED::
- Node uses only one thread to process the request given to it..
- Node consist of EVENT LOOP
- Node.js has two types of threads: one Event Loop and k(logical cores) Workers.
=> The Event Loop is responsible for JavaScript callbacks and non-blocking I/O, and
=> Worker executes C++code(libuv)that completes an asynchronous request,blocking I/O and CPU-intensive work
EventLoop::
- Event Loop is a single thread
- Every loop is a tick (can be explicitly called by process.tick() or setImmediate() that can run in next tick)
- Order of executions::
- Timers: this phase executes callbacks scheduled by setTimeout() and setInterval().
- Pending callbacks or OS tasks: executes I/O callbacks deferred to the next loop iteration (Like listening to a port,if a req comes in it is called back). => THIS IS WERE THE WORKER POOL STUFFS ARE CALLED AND ACCESSED
- Idle, prepare: only used internally. (wait for pending tasks to be completed or new ones to come in)
- Poll: retrieve new I/O events; execute I/O related callbacks (almost all with the exception of close callbacks, the ones scheduled by timers, and setImmediate()); node will block here when appropriate.
- Check: setImmediate() callbacks are invoked here.
- Close callbacks: some close callbacks, e.g. socket.on('close', ...)
Worker Pool::
- It handles all the io and cpu intensive tasks of node js from the event loop
- It has default 4 threads(default)
- process.env.UV_THREADPOOL_SIZE = 1024 max
Increase the Capability of node app::
1) ulimit -n 999999 => increase number of open sockets
2) –nouse-idle-notification => remove gc()
3) –expose-gc => specify gc()
4) –max-old-space-size=8192 => increase heap size
Increase the Potential Of Node::
Use Cluster module => cluster.fork() for as many logical cores available
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment