简体   繁体   中英

How work when a lot of requests arrive at the web server at the same time? NODEJS

IN NODEJS: IF we can only run one function at the same time if node is not using multiple threads. How can this work when a lot of requests arrive at the web server at the same time?

Can to clear the panorama about thread and process??

Theoretically if a large number of requests happened in the same second - or if each request has to do something that takes a while, like hard math - your server could get bogged down. Either by not responding to people at the "end" of the line (late by milliseconds), or never finishing all of the things Node needs to do to serve those requests at the "front" of the line.

In general the strategy Node takes is that if you're going to perform a long operation - like querying the database - the execution of the program should not sit around waiting, but should "call back" to some other function when the database query is eventually done.

I talk more about this in another SO answer . You could Google "node.js is cancer" for other examples of just what you are talking about.

But the prevalence of this strategy is one of the major differences between Node and other languages/frameworks: that's just how Javascript deals.

Now, in practice, several things actually happen.

First, any production Node app really should be running with Cluster or some kind of solution that provides load balancing. Because you'd be having multiple processes of your app working, your solution can do more than one thing at once.

Secondly, in general Node.js stays up really well, because the idea of not waiting around for everything. It keeps your server busy, instead of cooling it's jets waiting for something to be done.

Thirdly, yes you do have to be careful about what you do in the server. If something's going to take too long (modiying all the records in the database), probably wise to do it in the background via some kind of worker queue system: "Hey, I need to update this person's username in all of (these) records in the database" probably should happen by yet another Node.js process being the worker.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM