Skip to content
Related Articles

Related Articles

Improve Article

How to run many parallel HTTP requests using Node.js ?

  • Last Updated : 02 Aug, 2021

We know NodeJS application is single-threaded. Say, if processing involves request A that takes 10 seconds, it does not mean that a request which comes after this request needs to wait 10 seconds to start processing because NodeJS event loops are only single-threaded. The entire NodeJS architecture is not single-threaded. 

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. EventLoop is the listener for the EventQueue. 

If NodeJS can process the request without I/O blocking then the event loop would itself process the request and sends the response back to the client by itself. But, it is possible to process multiple requests parallelly using the NodeJS cluster module or worker_threads module.

How to scale your NodeJS application with Cluster Module? 



A single instance of Node.js runs in a single thread. If you have a multi-core system then you can utilize every core. Sometimes developer wants to launch a cluster of NodeJS process to take advantage of the multi-core system.

The cluster module allows easy creation of child processes that all share the same server ports.

Step 1: Create a NodeJS application and install the required Express.js module.

mkdir Project && cd Project
npm init -y 
npm i express

Step 2: Create an index.js file on your root directory with the following code. 

Javascript




const express = require('express');
const cluster = require('cluster');
 
// Check the number of available CPU.
const numCPUs = require('os').cpus().length;
 
const app = express();
const PORT = 3000;
 
// For Master process
if (cluster.isMaster) {
  console.log(`Master ${process.pid} is running`);
 
  // Fork workers.
  for (let i = 0; i < numCPUs; i++) {
    cluster.fork();
  }
 
  // This event is firs when worker died
  cluster.on('exit', (worker, code, signal) => {
    console.log(`worker ${worker.process.pid} died`);
  });
}
 
// For Worker
else{
 
  // Workers can share any TCP connection
  // In this case it is an HTTP server
  app.listen(PORT, err =>{
    err ?
    console.log("Error in server setup") :
    console.log(`Worker ${process.pid} started`);
  });
}

 
 

Explanation: If your system has 8 CPU then 8 NodeJS instances will be created and every instance has its own independent event loop. Now NodeJS can process all request parallelly. 

They are all share same port (PORT 3000) but not state. The master process listens on a port, accepts new connections and distributes them across the workers in a round-robin fashion, with some built-in smarts to avoid overloading a worker process.



Step 3: Run the index.js file using the following command.

node index.js

Output:

Using worker_threads Module: The best solution for CPU performance is Worker Thread. This module is used in Node.js because it is useful for performing heavy JavaScript tasks.

Instead of having:

  • One process
  • One thread
  • One event loop
  • One JS Engine Instance
  • One Node.js Instance
  • Worker threads have:

One process

  • Multiple threads
  • One event loop per thread
  • One JS Engine Instance per thread
  • One Node.js Instance per thread

Example: Create an index.js file with the following code.

index.js

Javascript




const {Worker} = require('worker_threads');
 
const worker = new Worker(__filename);
worker.on('message', message => console.log(message));
 
worker.postMessage('GeeksforGeeks');
worker.emit(true)

Run the server with the following command:

node --experimental-worker index.js

Note: We have to use the –experimental-worker because Workers Thread modules are still experimental.

Output:

{ name: ‘GeeksforGeeks’ }

Pros of Worker_Threads:

  • Passing native handles around (http/https request)
  • Deadlock detection.
  • More isolation, so if one process is affected, it won’t affect others.

Cons of Worker_Threads:

  • Not good for I/O operations.
  • Spawning Workers is not cheap.



My Personal Notes arrow_drop_up
Recommended Articles
Page :