Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nodejs - Why Node.js can handle large number of simulteneous persistent connections?

I know Node.js is good at keeping large number of simultaneous persistent connections, for example, a chat room for many many chatters.

I am wondering how it achieves this. I mean anyway it is using TCP/IP which is encapsulated by the underlying OS, why it can handle persistent connections so well that others cannot?

What is the magic thing does it have?

like image 922
Jackson Tale Avatar asked May 12 '12 11:05

Jackson Tale


People also ask

How many simultaneous connections can NodeJS handle?

To address these issues, Node. JS uses a single thread with an event-loop. In this way, Node can handle 1000s of concurrent connections without any of the traditional detriments associated with threads.

How does NodeJS handle multiple requests at the same time?

How NodeJS handle multiple client requests? NodeJS receives multiple client requests and places them into EventQueue. NodeJS is built with the concept of event-driven architecture. NodeJS has its own EventLoop which is an infinite loop that receives requests and processes them.

Is NodeJS good with concurrency?

The reason why node js is popular despite being single-threaded is the asynchronous nature that makes it possible to handle concurrency and perform multiple I/O operations at the same time. Node js uses an event loop to maintain concurrency and perform non-blocking I/O operations.

Why NodeJS is highly scalable?

Where Node. js really shines is in building fast, scalable network applications, as it's capable of handling a huge number of simultaneous connections with high throughput, which equates to high scalability.


1 Answers

Node.js makes all I/O asynchronous. It only runs in a single thread, but will do other requests or operations while waiting on I/O.

In contrast, classical web servers will not serve another request until the previous one is fully done. For this reason, Apache runs several processes at the same time; let's say there's 10 httpd processes, that normally means 10 requests can be served at any one time (*). If the processes take more time to complete, you will serve less requests - or will have to spawn more processes, even if the process is doing nothing - like waiting for the database to chew up and return data.

A node.js process, faced with a request that will go to the database, leaves the database to work while it goes to serve another request.

*) MPM makes this not quite true, but true enough for all intents and purposes.

like image 187
Amadan Avatar answered Oct 04 '22 11:10

Amadan