Updated April 12, 2023
Introduction to Node.js
Node.js is an open-source runtime environment. it uses javascript on the server-side. it is used for building web applications. Based on google chrome’s v8 engine; it runs on various platforms such as windows, linux, mac, etc. node.js uses asynchronous programming. The popular frameworks built for node.js are express, sails, meteor, nest, koa, loopback, derby, and many more. node.js is primarily used for event-driven servers, due to its single-threaded nature. NPM is a package manager for the node javascript runtime environment. it is a recommended feature for node.js installer. It can manage packages that are local dependencies of a particular project and is the world’s largest online repository.
Now, let us have a look at how node.js handles a particular request:
- The client sends a request to the server.
- Now, Node.js is ready to handle the next request.
- Once the request is processed, the server returns the content to the client.
Node.js does not keep waiting but continues with the next request.
Advantages of Node.js
Some of the advantages are:
- It is scalable and lightweight.
- Provides high performance with fewer lines of code.
- As it uses asynchronous programming, the non-blocking input output module solves performance issues.
- Node.js modules which are similar to a set of javascript files make it easier to use. e) it is highly efficient as it allows handling multiple requests.
Working of Node.js
As mentioned earlier, node.js applications are highly-scalable due to its asynchronous i.e non-blocking nature. Let us consider a real-life application:
Imagine a restaurant; a waiter is allocated to serve the tables. a waiter comes and takes the order from table 1 and informs the kitchen; now the chef is busy preparing the order. Meanwhile, the waiter is now available to serve table 2 and table 3.
The above scenario is called as asynchronous or non-blocking.
Node.JS applications are asynchronous by default. as the above figure depicts, a single thread is allocated to handle a request; where this single thread is used to handle multiple requests. When a request arrives, a single thread is allocated to handle that request. If we have to query the database, our thread doesn’t have to wait for the database to return the data; while the database is executing our query, that thread will be used to serve another client. When the database prepares the result, it returns the content to the client. this kind of architecture makes node.js ideal for building data-intensive applications.
Architecture of Node.js
Node.JS is based on single threaded event loop model. Its processing is mainly based on the javascript event loop model.
Let us first understand the single threaded event loop model:
Client sends request to Server.
- Node.js web server internally maintains a thread pool to provide services to the client.
- the requests received at the node.js web server are placed into the queue, which is also called an event queue.
- Event loop operates on a single thread only.
- When a request is placed in the Event Queue, the Node.JS web server starts processing that particular client request.
- If processing a particular client request does not involve any blocking input-output operations, then that request is processed; the response is prepared and sent back to the client.
- If processing a particular client request does involve blocking input output operations like interacting to an external service or querying a database, a different approach is followed.
- Thread availability check is performed at Internal Thread Pool.
- It then picks up a thread an assigns this client request to that
- That thread is then responsible for taking that request ahead, processing it, performing blocking operations, preparing the response, and sending it back to the Event loop.
- The event loop in turn, sends the response back to the client.
- The following diagram will help us understand the architecture of Node.JS: a) In the above diagram, there are “n” numbers of the client sending requests to our Node.JS server.
- Suppose our clients are Client-1, Client-2 …..Client-n.
- Our Node.js web server has an internal thread pool consisting of “n” number of threads.
- On receiving multiple client requests, the web server places them into the Event Queue.
- Now, the Event loop operates on all requests one by one.
At first, the event loop picks up the client-1 request-1
- Performs check if the client-1 request-1 requires any blocking input output operations or interaction with the database.
- As this particular request is simple and does not involve any complex operations, it does not require any separate thread to process it.
Event loop then processes the client-1 request-1 and prepares the response-1
- Event loop sends the response-1 to client-1
Event loop picks up the client-2 request-2
- Checks whether if the client-2 request-2 requires any blocking input output operations or an interaction with database.
- As this particular request is not simple and requires blocking input output operations or more complex operations, the event loop assigns a thread-1 to this request.
Thread T-1 now processes the request-2, performs all complex operations and prepares response-2.
- Thread T-1 then sends this response to the event loop.
- The event loop in turn, sends the response-2 to client-2.
Advantages of using single threaded event loop:
- You can easily handle concurrent client’s request.
- Node.JS uses fewer threads so that it can use fewer resources.
When a Node.JS web server receives concurrent requests, it need not create multiple threads because of event loop.
Examples of Node.js
There are several companies using node.js to build their applications. For example, paypal(online payment platform), netflix(biggest streaming media), uber (a platform that connects cab drivers to customers), ebay(multinational e-commerce function), walmart(world’s largest retail chain), linkedin(biggest social-networking platform dedicated to business and employment) and many more.
Conclusion
Thus, Node.js is a javascript runtime environment that runs on various platforms. it can be used for building highly scalable, data-intensive, and real-time based web applications as it uses asynchronous programming which is memory efficient.
Recommended Articles
This is a guide to What is Node.js?. Here we also discuss the introduction and working of node.js along with architecture and examples. You may also have a look at the following articles to learn more –