Signup/Sign In

Node.js Process Model

    Node applications are highly scalable this is because of non-blocking and asynchronous nature of Node. In this post we will try to understand the Process Model of the NodeJS framework which works on a Single main thread running the show.

    Refer the example from the What is Node.js section, which is as follows:

    Think of Node.js application as McDonald's restaurant.

    Customers come to the counter and give the order to a waiter(the main thread).

    When a large number of customers visit at the same time, they wait in a line (enqueued in the event queue) for their turn.

    Once the customer is served by the waiter, the waiter passes the order to the manager(libuv), who assigns the order to the other chef(worker threads).

    The chefs will use different ingredients and machines(low-level C/C++ components) depending upon the order.

    Once the waiter passes the order to the manager, he doesn't wait for the order to be prepared instead, the waiter takes the order of the next customer in the line(the dequeued event loop and pushed onto the call stack).

    The current event is the customer that is being served at the counter. When the order is ready the waiter calls the name of the customer and the customer gets his order.

    The waiter in the above example can be treated as a single thread which is allocated to handle a request. Similar is the case with NodeJS, in which a single thread handles all the requests.

    Node.js Process Model

    Node.js uses a single thread to handle the request, but it is non-blocking and asynchronous. For example, if we need to query the database, the thread doesn't have to wait for the database to return the data i.e. it works asynchronously and while the database is executing the query that thread will serve another client.

    When the database is ready with the result it puts the message in what we call as an event queue. Node is continuously watching this event queue which is running in the background. When it finds a new event it takes it out from the ready queue and processes it.

    Node.js Process Model

    Since Node.js uses a single thread only it can't be used for CPU-intensive applications.

    What do you mean by non- blocking or asynchronous?

    Imagine the situation of a restaurant. The waiter takes order from table1 and gives the order in the kitchen, now the waiter can serve another table let's say table2 and get their order. This is asynchronous system, as the chef is preparing the food, meanwhile a single waiter is serving different tables(taking orders and serving food). The waiter doesn't have to wait for the chef to cook the meal before he has to go to another table. This is what we call a non-blocking or asynchronous architecture.


    Considering the similar example of the restaurant for PHP, ASP etc. The waiter will take order from table1 give it to the kitchen and will wait until the chef prepares the food. This is called blocking or synchronous nature.

    In the case of synchronous, each time a new request comes a single thread is allocated for the new request. But what if there are a large number of requests to the server and we are left with no more threads then this will lead to the waiting of certain processes. Node.js, therefore, has the advantage by being asynchronous in nature.

    Node.js uses an event-based programming model and non-blocking I/O operations at its heart. Node.js serves the request using a single thread using an Event Loop. When Node receives a request from the client it will put the request into an event queue. The event loop runs indefinitely to receive a request from the event queue.

    There are two cases:

    • If there is no blocking process, node will prepare the response immediately and send it back to the client.

    • If the request requires blocking I/O operations, the node will dedicate it to the worker thread pool. The blocking I/O operation will be associated with a callback so that when the blocking I/O operation is complete the worker thread can send the response back to the event loop which then runs the callback function and sends the response to the client. Therefore, the main code is not blocked by the I/O operation. So node keeps a single-threaded event loop for our code and everything runs in parallel.

    NodeJs Process Model

    The Node.js process model increases the performance and scalability of the CPU. It can't be used for CPU-intensive operations like image processing or any application which requires heavy computation work.

    You may also like:

    Working as an Application Developer. I love to learn and discover things JavaScript, HTML, and CSS.