If we consider any typical web server like Apache, it requires separate threads for each process until the request is satisfied. The disadvantage in using multi-thread is they are not memory intensive and doesn’t scale very well. Also, we have to ensure that each process must be thread safe and deadlock should not appear.
But Node does things differently. On starting a Node application, it creates only a single thread of execution. When the Node receives a request, it assigns the thread to that process and no other request can be processed until it has finished processing the code for the current request. Therefore, Node handles multiple requests at the same time by using event loop and callback functions. An Event Loop is a type of functionality which basically polls for specific events and invokes event handlers when required. A Callback Function is this event handler in Node.
In Node applications, the Node initiates the request but does not wait around the request to get the response. Instead of that, it attaches a callback function to the request. When the request has been completed or the response has been received by the request, the callback function emits an event to do something with either the results of the requested action or the resource requested.
If multiple people access a Node application at the same time, and the application needs to access a resource from a file, Node attaches a callback function with each request. As soon as the resource becomes available to that particular request, a callback function is called to each person’s request. The Node can handle other requests in the meantime.
The serving of the parallel requests in the Node application depends upon how busy the application is and how it is designed?
Asynchronously opening and writing the contents of a file
- Node.js | Event Loop
- How to write asynchronous function for Node.js ?
- Asynchronous Patterns in Node.js
- Asynchronous file transfer in AJAX
- How to make asynchronous HTTP requests in PHP ?
- Difference between regular functions and arrow functions
- Node.js REPL (READ, EVAL, PRINT, LOOP)
- Node.js | stream.Writable close Event
- Node.js | Writable Stream pipe Event
- Node.js | Writable Stream finish Event
- Node.js | Readable Stream readable Event
- Node.js | Readable Stream error Event
- Node.js | Readable Stream end Event
- Node.js | Readable Stream data Event
- Node.js | Readable Stream close Event
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.