By Rambabu Posa
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
Today we will look into Node JS Architecture and Single Threaded Event Loop model. In our previous posts, we have discussed about Node JS Basics, Node JS Components and Node JS installation.
Before starting some Node JS programming examples, it’s important to have an idea about Node JS architecture. We will discuss about “How Node JS works under-the-hood, what type of processing model it is following, How Node JS handles concurrent request with Single-Threaded model” etc. in this post.
As we have already discussed, Node JS applications uses “Single Threaded Event Loop Model” architecture to handle multiple concurrent clients. There are many web application technologies like JSP, Spring MVC, ASP.NET, HTML, Ajax, jQuery etc. But all these technologies follow “Multi-Threaded Request-Response” architecture to handle multiple concurrent clients. We are already familiar with “Multi-Threaded Request-Response” architecture because it’s used by most of the web application frameworks. But why Node JS Platform has chosen different architecture to develop web applications. What is the major differences between multithreaded and single threaded event loop architecture. Any web developer can learn Node JS and develop applications very easily. However without understanding Node JS Internals, we cannot design and develop Node JS Applications very well. So before starting developing Node JS Applications, first we will learn Node JS Platform internals.
Node JS Platform uses “Single Threaded Event Loop” architecture to handle multiple concurrent clients. Then how it really handles concurrent client requests without using multiple threads. What is Event Loop model? We will discuss these concepts one by one. Before discussing “Single Threaded Event Loop” architecture, first we will go through famous “Multi-Threaded Request-Response” architecture.
Any Web Application developed without Node JS, typically follows “Multi-Threaded Request-Response” model. Simply we can call this model as Request/Response Model. Client sends request to the server, then server do some processing based on clients request, prepare response and send it back to the client. This model uses HTTP protocol. As HTTP is a Stateless Protocol, this Request/Response model is also Stateless Model. So we can call this as Request/Response Stateless Model. However, this model uses Multiple Threads to handle concurrent client requests. Before discussing this model internals, first go through the diagram below. Request/Response Model Processing Steps:
Server waits in Infinite loop and performs all sub-steps as mentioned above for all n clients. That means this model creates one Thread per Client request. If more clients requests require Blocking IO Operations, then almost all threads are busy in preparing their responses. Then remaining clients Requests should wait for longer time. Diagram Description:
Here “n” number of Clients Send request to Web Server. Let us assume they are accessing our Web Application concurrently.
Let us assume, our Clients are Client-1, Client-2… and Client-n.
Web Server internally maintains a Limited Thread pool. Let us assume “m” number of Threads in Thread pool.
Web Server receives those requests one by one.
Web Server pickup Client-1 Request-1, Pickup one Thread T-1 from Thread pool and assign this request to Thread T-1
Web Server pickup another Client-2 Request-2, Pickup one Thread T-2 from Thread pool and assign this request to Thread T-2
Web Server pickup another Client-n Request-n, Pickup one Thread T-n from Thread pool and assign this request to Thread T-n
Once Threads are free in Thread Pool and available for next tasks, Server pickup those threads and assign them to remaining Client Requests.
Each Thread utilizes many resources like memory etc. So before going those Threads from busy state to waiting state, they should release all acquired resources.
Drawbacks of Request/Response Stateless Model:
Here Client Request is a call to one or more Java Script Functions. Java Script Functions may call other functions or may utilize its Callback functions nature. So Each Client Request looks like as shown below: For Example:
function1(function2,callback1); function2(function3,callback2); function3(input-params);
As I’m a Java Developer, I will try to explain “How Event Loop works” in Java terminology. It is not in pure Java code, I guess everyone can understand this. If you face any issues in understanding this, please drop me a comment.
That’s all for Node JS Architecture and Node JS single threaded event loop.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in our Questions & Answers section, find tutorials and tools that will help you grow as a developer and scale your project or business, and subscribe to topics of interest.Sign up now
Please explain that what if an another request arises to event loop with blocking I/O operation and thread t1 is busy in executing earlier blocking I/O request. The in the case what will event loop decide to perform? Thank You
This is one of the best articles on Node.js single-thread architecture. So helpful specially for a newbie like me.
That means the only deference between multi thread request response and single thread (node.js) is before handling request node js check if the request is blocking operation or not if blocking then it will apply thread otherwise not Am I right? And another thing is though node js is single thread it internally use multi thread to handle request right?
- Shuvo Khalid
In the native model, if there is no free thread pool then request should be wait right as per ur blog. But u didnt emntion this case for the nodejs single threaded.
- Rabeesh V
Great Explanation. I have a question i.e When Blocking code/file processing is taking care of Thread, who is taking care of simple code/ task which involves calculations/ etc . I mean processer/threads ?
thank you very much, it is the best Node.js architecture explanation I have ever read
- abdullatif jamous
Very well explained. This my first time in Node JS and read many articles on the architecture, but your article made the concept so clear. Thanks.
- Sonal Agarwal
Good article. I should have read earlier. There is a clear clarification on the high level Thanks.
Hi All, I am confused about how event loop decide which request is blocking or nonblocking? Jatin Raghv
beautiful explanation. everyone can understand this. so clear. thank you so much.