Node JS Architecture
Node JS Architecture
Search
Get unlimited access to the best of Medium for less than $1/week. Become a member
Introduction:
Node.js is an extremely powerful JavaScript-based platform that’s built on Google
Chrome’s JavaScript V8 Engine, used to develop I/O intensive web applications like
video streaming sites, single-page applications, online chat applications, and other
web apps.
Node.js is used by large, established companies and newly-minted startups alike.
Open-source and completely free, the platform is used by thousands of developers
around the world. It brings plenty of advantages to the table, making it a better
choice than other server-side platforms like Java or PHP in many cases.
Node.js has its core part written in C and C++. Node js is based on a single-threaded
event loop architecture which allows Node to handle multiple client requests. Node
js uses the concept of an asynchronous model and non-blocking I/O. We will look at
these terms in detail.
Node.js is one of the best options for other server-side platforms because of the
architectural pattern it follows.
Table of contents:
Basic components of web applications
Node.js Architecture.
1. Client
2. Server
3. Database
Client:
The user interacts with the front-end part of a web application. The front-end is
usually developed using languages like HTML and CSS styles, along with extensive
usage of JavaScript-based frameworks like ReactJS and Angular, which help with
application design.
Server:
The server is responsible for taking the client requests, performing the required
tasks, and sending responses back to the clients. It acts as a middleware between the
front-end and stored data to enable operations on the data by a client. Node.js, PHP,
and Java are the most popular technologies in use to develop and maintain a web
server.
Database:
The database stores the data for a web application. The data can be created,
updated, and deleted whenever the client requests. MySQL and MongoDB are
among the most popular databases used to store data for web applications.
Node.js Architecture:
Node.js uses the “Single Threaded Event Loop” architecture to handle multiple
concurrent clients. Node.js Processing Model is based on the JavaScript event-based
model along with the JavaScript callback mechanism.
Now let’s understand each part of the Node.js architecture and the workflow of a
web server developed using Node.js.
Node.js server: Node.js server is a server-side platform that takes requests from
users, processes those requests, and returns responses to the corresponding
users
Event Queue: Event Queue in a Node.js server stores incoming client requests
and passes those requests one-by-one into the Event Loop
Thread pool: Thread pool consists of all the threads available for carrying out
some tasks that might be required to fulfill client requests
Event loop: Event Loop indefinitely receives requests and processes them, and
then returns the responses to corresponding clients.
External Resources: External resources are required to deal with blocking client
requests. These resources can be for computation, data storage, etc.
A web server developed using Node.js typically has a workflow that is quite similar
to the diagram illustrated below. Let’s explore this flow of operations in detail.
Clients send requests to the webserver to interact with the web application.
Requests can be non-blocking or blocking e.g Querying the data , updating the
data or deleting the data .
Node.js retrieves the incoming requests and adds those requests to the Event
Queue.
The requests are then passed one-by-one through the Event Loop. It checks if
the requests are simple enough to not require any external resources.
A single thread from the Thread Pool is assigned to a single complex request. This
thread is responsible for completing a particular blocking request by accessing the
external resources, such as compute, database, file system, etc.
Once, the task is carried out completely, the response is sent to the Event Loop that
in turn sends that response back to the Client.
Handling multiple concurrent client requests is fast and easy: With the use of
Event Queue and Thread Pool, the Node.js server enables efficient handling of a
large number of incoming requests.
No need for creating multiple threads: Event Loop handles all requests one-by-
one, so there is no need to create multiple threads. Instead, a single thread is
sufficient to handle a blocking incoming request.
All of these advantages contribute to making the servers developed using Node.js
much faster and responsive when compared to those developed using other server
development technologies.
JavaScript
Follow