• Node.js Video Tutorials

Node.js - Scaling Application



Scalability of a Node.js application refers to its ability to handle increasing workload on the server with a greater number of requests received from clients. A Node.js application can utilize child processes to address this issue. A child process is a separate instance of the Node.js runtime that can be spawned and managed by the main process. Such a spawned child process can perform specific tasks parallel, thereby it improves the overall performance and scalability of the application.

The child_process module is one of the core modules of Node.js runtime. This module provides various methods for creating, managing, and communicating with child processes, especially on multi-core CPU based systems.

A child process always has three streams: child.stdin, child.stdout, and child.stderr. These streams may be shared with the streams of the parent process.

The child_process module which has the following three major ways to create a child process.

  • exec − child_process.exec method runs a command in a shell/console and buffers the output.

  • spawn − child_process.spawn launches a new process with a given command.

  • fork − The child_process.fork method is a special case of the spawn() to create child processes.

The exec() method

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 − cwd- the current working directory of the child process, and env - Environment key-value pairs

    • encoding − encoding string (Default: 'utf8')

    • shell − Shell to execute the command with (Default: '/bin/sh' on UNIX, 'cmd.exe' on Windows, The shell should understand the -c switch on UNIX or /s /c on Windows. On Windows, command line parsing should be compatible with cmd.exe.)

    • timeout − Default: 0

    • maxBuffer − Default: 200*1024

    • killSignal − Default: 'SIGTERM'

    • uid − Sets the user identity of the process.

    • gid − Sets the group identity of the process.

    • 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 and tries to return all the buffered data at once.

Example

Let us 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 fs = require('fs');
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);
   });
}

Now run the main.js to see the result −

Child process exited with exit code 0
stdout: Starting Child Process-0
Child Process-0 executed.

stderr:
Child process exited with exit code 0
stdout: Starting Child Process-1
Child Process-1 executed.

stderr:
Child process exited with exit code 0
stdout: Starting Child Process-2
Child Process-2 executed.

stderr:

The spawn() Method

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

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

The first argument is the command to be run by the child process. One or more arguments may be passed to it. The options arguments may have one or more of the following properties −

  • cwd (String) Current working directory of the child process.

  • env (Object) Environment key-value pairs.

  • stdio (Array) String Child's stdio configuration.

  • customFds (Array) Deprecated File descriptors for the child to use for stdio.

  • detached (Boolean) The child will be a process group leader.

  • uid (Number) Sets the user identity of the process.

  • gid (Number) Sets the group identity of the process.

The spawn() method returns streams (stdout &stderr) and it should be used when the process returns a volume amount of data. spawn() starts receiving the response as soon as the process starts executing.

Example

As in the previous example, create child.js and main.js files.

child.js

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

main.js

const fs = require('fs');
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);
   });
}

Output

stdout: Starting Child Process-0

stdout: Child Process-0 executed.

Child process exited with exit code 0
stdout: Starting Child Process-1

stdout: Child Process-1 executed.

stdout: Starting Child Process-2

stdout: Child Process-2 executed.

Child process exited with exit code 0
Child process exited with exit code 0

The fork() Method

child_process.fork method is a special case of spawn() to create Node processes. It has the following syntax −

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

Parameters

The first parameter is the module to be tun by the child. You may pass additional arguments to the module. The options are the same as in the spawn() method.

Example

As in the previous example, create child.js and main.js files.

child.js

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

main.js

const fs = require('fs');
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);
   });
}

Output

Starting Child Process-0
Child Process-0 executed.
Starting Child Process-2
Child Process-2 executed.
Starting Child Process-1
Child Process-1 executed.
child process exited with code 0
child process exited with code 0
child process exited with code 0

execFile() method

The child_process.execFile() function is similar to the exec() method, except that it does not spawn a shell by default. It is slightly more efficient than the exec() as the specified executable file is spawned directly as a new process.

Syntax

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

Parameters

  • command − Accepts a string that specifies the name or path of the command to run.

  • args − List of string arguments.

  • options − Some of the options available are cwd, env, encoding, shell, timeout etc

  • callback − The callback function is called when the process terminates. The arguments to this function are error, stdout, and stderr respectively.

Advertisements