In this blog, we will know why Node.js should be used when we have many other programming languages and frameworks. Before this let us first understand what Node.js is and how it works.
What is Node.js ?
Node.js is ideal for building highly scalable, data-intensive and real time back end services that empower a client application. There are other frameworks also for the same then what so special about Node.js is.
What’s So Special About Node.js?
- Node.js is easy to get started and and can be used for prototyping and agile development.
- It can also be used for building superfast and highly scalable super services. It’s used in production by large companies such as Paypal, Uber, Netflix, Walmart and so on. In fact in Paypal, they rebuilt one of their Java and Spring based application using Node.js and found that with node it was built :
- twice as fast with fewer ppl
- within 33% fewer lines of code
- within 40% fewer files
- with 2x request/sec
- with 35% faster response time
So node is an excellent choice for building highly scalable services.
- It has larger ecosystem of open source libraries.
Architecture of Node.js
How Node Works?
Node.js is asynchronous in nature. Let us first understand the difference between synchronous and asynchronous architectures.
Synchronous Architecture: If a thread is allocated to a client request, it won’t handle new client request until the current client request it is handling, gets completed. If the request is like fetching a query from database, it will wait until the result appears. So for new request, we need to serve using a new thread. Imagine what would happen if we have a large number of concurrent clients? At some point, we are going to run out of threads as all threads get busy. So new clients have to wait untill free threads are available or we need more hardware as represented in Figure 1. So with this kind of architecture , you are not utilizing your resources efficiently. This is the problem with blocking or synchronous architecture. ASP.NET is such framework which has synchronous architecture. It can be converted into asynchronous architecture as well but we need to do some extra coding to achieve the same.
Asynchronous Architecture: Node applications are asynchronous by default. In Node.js, we have single thread to handle all the requests. When a request arrive, that single thread is used to handle the request. If the request is to create a database, the thread doesn’t wait for database to return the data. While the database is executing our query, that thread will be used to serve another client as represented in Figure 2.
When database prepare the results, it puts a message in an Event Queue (see Figure 3). Node is continuously monitoring this queue in the background. When it finds an event in this queue, it will take it out and processes.
The asynchronous architecture makes node ideal for building apps that use a lot of disk and network access. We can serve more clients without the need of more number of hardware and that’s why node is highly scalable. And therefore, Node.js is not a good idea where we have to handle more CPU requests and arithmetic calculations because then the single thread will be busy until it finishes all the CPU based calculations from a current client and the new client will have to wait.