How to Scale a Node.js Application for High Performance

How to Scale a Node.js Application for High Performance

The capacity of a Node.js application to manage growing server burden with an increase in client requests is known as scalability. To solve this problem, a Node.js application can make use of child processes. A distinct instance of the Node.js runtime that may be launched and controlled by the main process is called a child process. A child process that has been generated can carry out particular tasks concurrently, which enhances the application’s overall performance and scalability.

One of the essential components of the Node.js runtime is the child_process module. Especially on systems with multi-core CPUs, this module offers a variety of ways to create, manage, and interact with child processes.

Child.stdin, child.stdout, and child.stderr are the three streams that are always present in a child process. The streams of the parent process might share these streams.

There are three main ways to generate a child process using the child_process module:

  • exec: Executes a command in a console or shell while buffering the results.
  • spawn: Uses a specified command to start a new process.

To create child processes, use the fork function as a special instance of spawn().

The exec() Method

The child_process.exec method runs a command in a shell and buffers the output. It has the following signature:

child_process.exec(command[, options], callback)

Parameters:

  • command − The command to run, with space-separated arguments.
  • options − Options like cwd (current working directory), env (environment key-value pairs), encoding (default: ‘utf8’), shell, timeout, maxBuffer, killSignal, uid, and gid.
  • callback − A function with three arguments: error, stdout, and stderr, which are called with the output when the process terminates.

The exec() method returns a buffer with a max size and waits for the process to end before returning all buffered data at once.

Example:

Create two JS files named child.js and main.js:

File: child.js

console.log("Starting Child Process-" + process.argv[2]);
console.log("Child Process-" + process.argv[2] + " executed.");

File: main.js

const child_process = require('child_process');

for (var i = 0; i < 3; i++) {
   var childprocess = child_process.exec('node child.js ' + i, function(error, stdout, stderr) {
      if (error) {
         console.log(error.stack);
         console.log('Error code: ' + error.code);
         console.log('Signal received: ' + error.signal);
      }
      console.log('stdout: ' + stdout);
      console.log('stderr: ' + stderr);
   });

   childprocess.on('exit', function(code) {
      console.log('Child process exited with exit code ' + code);
   });
}

The spawn() Method

The child_process.spawn method launches a new process with a given command.

child_process.spawn(command[, args][, options])

Example:

File: main.js

const child_process = require('child_process');

for (var i = 0; i < 3; i++) {
   var childprocess = child_process.spawn('node', ['child.js', i]);

   childprocess.stdout.on('data', function(data) {
      console.log('stdout: ' + data);
   });

   childprocess.on('exit', function(code) {
      console.log('Child process exited with exit code ' + code);
   });
}

The fork() Method

The child_process.fork method is a special case of spawn() to create Node processes.

child_process.fork(modulePath[, args][, options])

Example:

File: main.js

const child_process = require('child_process');

for (var i = 0; i < 3; i++) {
   var childprocess = child_process.fork("child.js", [i]);

   childprocess.on('close', function(code) {
      console.log('Child process exited with code ' + code);
   });
}

execFile() Method

Similar to exec(), the child_process.execFile() function does not by default start a shell. Because it spawns the supplied executable file as a new process, it is marginally more efficient than exec().

child_process.execFile(command [, args][, options][, callback])

Parameters:

  • command − Name or path of the command to run.
  • args − List of string arguments.
  • options − Options like cwd, env, encoding, shell, timeout, etc.
  • callback − Function called when the process terminates with arguments error, stdout, and stderr.

With this method, developers may use Node.js child processes to grow and manage their applications effectively.

It might be helpful:

A Guide to Node.js Command Line Options

Node.js Event Handling Explained with Examples

How to Ensure Data Privacy in SAP Test Environments

admin
admin
https://www.thefullstack.co.in

Leave a Reply