Understanding NodeJS Architecture Works

When Client sending Task/Request to NodeJS application, they will send to Queue and will do process every Queue coming, because NodeJS use Single Thread, and the impact is every Request coming to NodeJS application will be stored in queue before executing.

By default, NodeJS uses single thread that is nature. but if we see in the background there is thread helping it was calling C++ Threadpool. So, if we see the diagram below, we can see Single Thread but if we see overall not only single thread because there is Threadpool too for helping the process execute.

Event-Driven Architecture

Event-Loop

  • Event-loop is a single thread process used for executing code Non-Blocking. NodeJS was recommended if we create code, it must Non-Blocking.
  • Because Event-Loop only uses single thread, we must be careful when creating blocking code, because it can slowly execute the code, they will busy manage code blocking, they didn’t have time to execute another code.
  • Event-Loop the job only received and send code executing to C++ Threadpoll, so that why we must use code Non-Blocking for process blocking will do inside C++ Threadpool.
  • Event-Loop will receive response from C++ threadpool sent via callback.

C++ Threadpool / Worker Threadpool

  • NodeJS using C++ Threadpool for worker code execution.
  • Threadpool inside NodeJS using library Libuv whereby default Libuv using 4 threads inside Threadpool, this makes we can do 4 workers blocking at one time. If we send 4 requests, they can execute parallel together, but if we send 6 requests only 4 requests will be executed, 2 requests will be waiting in Queue.
  • If there is too much blocking worker, we can change the amount of thread in Libuv, if we want uses more thread, inside environment settings variable UV_THREADPOOL_SIZE by default using 4 threads, but we can change it, for example because my code blocking will use for encryption we want upgrade to 40 thread, but consider the impact will uses bigger memory.