0

I was learning node js when I found this issue. I made a route in express app and when the thread hits the route its call a sleep function and wakes after 10 sec. During this time I was not able to access other routes in the system. I tried the same in django and everything works fine. How do I overcome this. Is this can be a issue when a user is uploading some images or files ?

Shed some light on this. I heard something called clustering but didn't understand what it is.

ilovecse
  • 321
  • 2
  • 10

2 Answers2

0

Clustering is basically spawning multiple instances of the same process, so that you can have process based parallelism.

Here's what it is: Say you spawn 4 instances of your app and send 1000 requests to it.

First instance will take the first request and process it.
Second instance will take the second request and process it.
Third instance will take the third request and process it.
Fourth instance will take fourth request and process it.
The instance that first completes processing its request will process the 5th request 

and so on.

What's wrong with process based parallelism is this:

Let's say you get 4 requests and you know that they will take 5 minutes, 40 seconds, 30 seconds, 45 seconds respectively to process. They all arrive at time t_0.

You distribute each request to an instance.

At t_0 + 30 seconds, first instance will finish processing its respective request.

At t_0 + 40 seconds, the second instance will finish processing its respective request.

At t_0 + 45 seconds, the third instance will finish processing its respective request.

Starting from t_0 + 45 seconds, ending at t_0 + 300 seconds, all instances but one will wait idly and do a whole lot of nothing while the first instance is still processing that 300-second-long request.

ardilgulez
  • 1,856
  • 18
  • 19
  • I admit I don't quite get the example. In particular, is there any other approach that would perform better in this specific case of 4 requests? – Wiktor Zychla May 22 '17 at 12:28
0

NodeJs is designed to be asynchronous, handling all requests from a single thread.
So any time consuming operations (blocking) on the main thread would put other requests on hold, Similar to what you faced with the sleep (though there is no build in support for sleep in JS).

In order to perform blocking operations like I/O. NodeJs provides callbacks or promises, enabling the process to not wait for the operations to complete (This is basically the asynchronous behavior).

To answer Is this can be a issue when a user is uploading some images or files ? The answer is No. You should use callbacks or promises.

Using callbacks and the build in fs module to read a file asynchronously.

const fs = require('fs');
function doSomething(err, data){
  //do some operations
}
function testAsync(){

fs.readFile('path/to/someFile',doSomething)
console.log('I log first');
}

Here node process will not wait for the readFile to finish and would continue ahead to log I log first. Function doSomething only be called post readFile is complete.

Also you don't need Clustering to solve this. Clustering is used to spawn multiple NodeJs processes, essentially to balance loads on a multi-core system.

I would advise you going through a introductory tutorials before starting up with express, there are couple of great tutorials available, One of them is art of node.

Shivam
  • 3,462
  • 1
  • 15
  • 20