3

Two concurrent requests R1 and R2 come to node.js server. Node.js is running with single thread T1. T1 takes 5 seconds to process the request R1. Assume this time spent in some actual processing of big function and there is no waiting/IO call.

My question is - will request R2 will be taken up by after 5 seconds(once R1 completes) or both(R1 and R2) will be run in round robin fashion?

If answer is sequential(i.e R2 will be taken up after 5seconds), My followup question is say i have got 5k http concurrent requests and each request takes 2 ms, then last request will be served after 5k*2ms= 10 sec. Is it not bad ? Do i need to go for clustering here ?

6
  • Node runs an event loop system, not threads. Each request fires the event loop so R1 and R2 run parallel. Commented Apr 23, 2017 at 0:32
  • Then what is single thread model here ? do you mean two request will fire the two events, event loop will execute the corresponding event handlers through single thread in round robin fashion and once done will send the response back ? Commented Apr 23, 2017 at 0:46
  • Again, it is an even loop system, not a threaded one, don't think of threads, that's handled on a lower level you don't have access to, thankfully. Both requests will run at the same time. Commented Apr 23, 2017 at 0:48
  • @MarcoScabbiolo Please experiment before you comment. A simple google search will tell you that Node.js is single-threaded. The two requests would not be run in parallel if the first one is blocking. Commented Apr 23, 2017 at 1:34
  • @TimWong Node runs an event loop, it is a different type of system, the implementation doesn't affect how the event loop works, and you don't need to worry about threads, you just have to understand the event loop. Of course a block in the event loop will block, thats how it works! View youtube.com/watch?v=QgwSUtYSUqA if you want to learn more. Commented Apr 23, 2017 at 17:32

2 Answers 2

2

will request R2 will be taken up by after 5 seconds(once R1 completes) or both(R1 and R2) will be run in round robin fashion?

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

In short, you can google nodejs event loop. There are a lot of great articles explaining how Node.js uses an event loop to handle requests.

The event loop is what allows Node.js to perform non-blocking I/O operations — despite the fact that JavaScript is single-threaded — by offloading operations to the system kernel whenever 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 should either breakdown R1 into smaller pieces to be processed asynchronously, or you could use a child_process to offload the operation to another thread.

If answer is sequential(i.e R2 will be taken up after 5seconds), My followup question is say i have got 5k http concurrent requests and each request takes 2 ms, then last request will be served after 5k*2ms= 10 sec. Is it not bad ? Do i need to go for clustering here?

It depends. 2ms is actually a long time for the computer to process a lot of things. Before you go for clustering, you should refactor your code to minimize the blocking code in the request handler as mentioned above. And before you buy more servers for clustering, you could fully utilize your CPU cores by cloning your application to other threads using the cluster library. A well-designed Node.js application should be capable to handle thousands of requests without issues. Otherwise, you might reconsider if Node.js is the best fit for your application.

Bonus: Let's listen to the inventor of Node.js - why he created Node.js

Sign up to request clarification or add additional context in comments.

6 Comments

what do you mean by if R1 is synchronous. ? these are the http request coming to node server from browser. So what is synchronous here ?
I mean the logic in your request handler. For example, a simple while-loop is synchronous because the application cannot do anything else before the while-loop completes. A fs.readFile operation is asynchronous because the file I/O is handled in the background and the application is capable to handle other requests at the same time.
Thanks got it. One question on fully utilize your CPU cores by cloning your application to other threads . Say my node.js(single thread) is running on 8080 port.This is utilizing one of the cores. To utilize another core through cluster library , will it start another node process and listening on same port 8080 ?
Although I am not very sure how it is achieved, the example in the official documentation states that the workers can share any TCP connection (i.e. 8080 port in your case). Please refer to the documentation for more explanation. Please also be aware that your application should be stateless when you cluster it, or things will become unpredictable.
Last followup question You said ... or you could use a child_process to offload the operation to another thread I am not sure what do you mean by another thread ? Can we have more than one thread in node.js(as node works in single thread model and keep on processing the events from event loop) ?
|
1

If your function is CPU-bound, you'll tie up the event loop. You have two options.

  1. Shift to a different model of communication with other processes, perhaps running their own event loop: pre-fork or worker MPM. You can spawn those processes from within Node if you'd like using child_process
  2. Limit CPU processing, and issue a .nextTick() to allow other things in the event loop to complete and resume later.

3 Comments

if i don't do both the steps you mentioned when R2 request will be taken up for processsing? Is it after 5 seconds(i.e sequential) ?
@user3198603 yes, so long as there hasn't been a client-side timeout which is typical. Not to many clients are going to wait five seconds for your server to answer and start processing (though web browsers will).
I did not get when you say "Not to many clients are going to wait five seconds for your server to answer and start processing (though web browsers will). " ? browser/client for R2 will wait for seconds. Right ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.