How to query a node.js model process in parallel with one thread?

Two parallel requests R1 and R2 come to node.js

server. Node.js

work with the same thread T1. T1 takes 5 seconds to process R1's request. Suppose this time spent doing some actual processing of a large function, and there is no wait / I / O call.

My question is - will request R2 be considered in 5 seconds (after R1 completes) or will both (R1 and R2) be executed in a circular manner?

If the response is consistent (i.e. R2 will be considered after 5 seconds), my follow-up question says that I have 5k http concurrent requests and each request takes 2ms, then the last request will be served after 5k * 2ms = 10 sec. Isn't that bad? Do I need to go for clustering here?

+3


source to share


2 answers


will the request R2

be considered in 5 seconds (as soon as it R1

completes), or will both ( R1

and R2

) be executed in a circular manner?

Yes, R2 will only be considered after R1 completes if R1 is synchronous.

In short, you can google nodejs event loop

. There are many great articles out there explaining how Node.js uses the event loop to handle requests.

The event loop is what allows Node.js to perform non-blocking I / O operations - even though JavaScript is single-threaded - by offloading operations to the core as much as possible. (Source: https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/ )



You are correct that Node.js is single threaded. It cannot process anything else if it is blocked by a long running task. In your situation, you have to either break down R1

into smaller parts that need to be processed asynchronously, or you can use child_process

to offload operations with another thread.

If the response is consistent (i.e. R2

will be considered after 5 seconds) my follow-up question will say that I have 5k http concurrent requests and each request takes 2ms, then the last request will be served after 5k * 2ms = 10 seconds. Isn't that bad? Do I need to go for clustering here?

It depends. 2ms is actually a long time for a computer to process many things. Before moving on to clustering, you should refactor your code to minimize blocking code in your request handler, as stated above. And before buying more servers for clustering, you can make full use of your processor cores by cloning your application to other threads using the library cluster

. A well-designed Node.js application should be able to handle thousands of requests without issue. Otherwise, you may want to reconsider if Node.js is the best fit for your application.

Bonus: let me listen to the inventor of Node.js - why he created Node.js

+2


source


If your function is processor bound, you bind the event loop. You have two options.



  • Change to a different communication model with other processes, perhaps by running your own event loop: pre-fork or worker MPM. You can create these processes from Node if you want to usechild_process

  • Limit CPU processing and release .nextTick()

    so that other things in the event loop are completed and resumed later.
+1


source







All Articles