Fiveable Founding Member Experience

Checking your email frequently is something that many educators and professionals recommend students do, especially in the summer. I, as the self identified planner and organization queen that I am…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Single Threaded Event Loop

Traditional Web Server Model

Before the advent of NodeJS, the web server model consisted of a thread pool where a dedicated thread handled each user request. In this model each time when a new user request comes in, it is assigned to a different thread, and in any event, when a thread is not available to process the request, it needs to wait for the next available thread. A thread is freed only when the request is processed, a response is returned, and the thread is returned back to the thread pool. This makes the traditional web server model synchronous and blocking.

Contrary to the traditional web server model, NodeJS uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. The NodeJS process model can be explained with three architectural features of NodeJS.

NodeJS runs on a single-threaded environment which means each user request processes on a single thread only. This makes it use lesser resources and run smoothly using events and emitters.

Events are a crucial paradigm of the NodeJS process. Events are actions that instruct the runtime what and when something needs to be completed. Event Emitters are response object instances that can be subscribed to and acted upon to perform operations. Event Emitters emit events based on certain predefined events accepting a callback. According to MDN web docs, event loops are responsible for executing the code, collecting and processing events, and executing queued sub-tasks.

Blocking codes or operations are the ones that need to be completed entirely before moving on to another operation. Non-blocking codes are asynchronous and accept callback functions to operate.

As mentioned, every request has a synchronous and asynchronous part. The main thread of NodeJS does not keep waiting for the background thread to complete the asynchronous I/O operations. The main thread keeps switching between other requests to process their synchronous part while the background thread process the asynchronous part.

Once the execution of the background thread is complete, the background thread emits events to notify the main thread. Callback functions are associated with asynchronous processes. If the main thread is not free, the request waits for the main thread to be free and then takes up the callback request for further execution.

Add a comment

Related posts:

Computing Large Factorials using Logarithm

Computing Large Factorials using Logarithm. “Computing Large Factorials using Logarithm” is published by Priyanshichaki.

The Subjunctive

The Subjunctive. You don’t say what you mean, that you don’t mean what you say. You only dream of what shouldn’t be, but not of what could be..

Post Publishing Stress Syndrome

The medical community has finally identified and named a condition writers have been suffering from for decades. PPSS, Post Publishing Stress Syndrome, can become serious and debilitating if left…