2. SCALING APPLICATION IN NODE.JS
Although Node.js operates in a single-thread mode, it handles concurrency
using an event-driven architecture. Additionally, it makes it easier to create
child processes so that multi-core CPU-based systems can benefit from
parallel processing.
The Node.js child process module provides three major ways to create a child
process:
1. ‘spawn’(): Launches a new process and provides streams for
communication.
2. ‘exec’(): Executes a command in a shell, buffers the output, and provides it
in a callback.
3. ‘fork’(): Creates a child process that communicates with the parent
process through IPC (inter-process communication) using message events
and send function.
3. THE EXEC() METHOD:
‘cwd’ (string, optional): The current working directory for the command.
‘Env’ (object, optional): Key-value pairs representing the environment
variables.
‘encoding’ (string, optional): The character encoding to use for the
command’s output. Defaults to ‘utf8’.
The ‘exec()’ method in the ‘child_process’ module of Node.js has several
parameters that allow you to customize the behavior of the executed
command. Here are the parameters used in the ‘exec()’ method:
1. ‘command’ (string): The command to be executed in a shell.
2. ‘options’ (object, optional): An object that allows you to specify additional
options for executing the command.
4. THE EXEC() METHOD:
‘shell’ (string, optional): The shell to be used for executing the command.
Defaults to ‘/bin/sh’.
‘timeout’ (number, optional): The maximum amount of time in
milliseconds the process is allowed to run.
‘maxBuffer’ (number, optional): The largest amount of data in bytes
allowed on stdout or stderr. If exceeded, an error is thrown.
‘killSignal’ (string or integer, optional): The signal used to kill the process
when the ‘timeout’ option is specified.
‘uid’ (number, optional): Sets the user identity of the process.
‘gid’ (number, optional): Sets the group identity of the process.
‘windowsHide’ (boolean, optional): Hides the subprocess console
window on Windows.
5. THE SPAWN() METHOD
‘cwd’ (string, optional): The current working directory for the child
process.
‘env’ (object, optional): Key-value pairs representing the environment
variables for the child process.
The ‘spawn()’ method in the ‘child_process’ module of Node.js provides
several parameters to customize the behavior of the spawned child
process. Here are the parameters used in the ‘spawn()’ method:
1. ‘command’ (string): The command to be executed. It should be the
name or path of the executable.
2. ‘args’ (array, optional): An array of command-line arguments to be
passed to the command.
3. ‘options’ (object, optional): An object that allows you to specify
additional options for spawning the child process.
6. THE SPAWN() METHOD
‘stdio’ (string, array, or object, optional): The standard
input/output/error configuration for the child process.
‘detached’ (boolean, optional): Specifies if the child process should be
detached from the parent.
‘uid’ (number, optional): Sets the user identity of the child process.
‘gid’ (number, optional): Sets the group identity of the child process.
‘shell’ (boolean or string, optional): Specifies whether to execute the
command through the system shell.
‘windowsVerbatimArguments’ (boolean, optional): Specifies whether
to quote the arguments on Windows systems.
‘windowsHide’ (boolean, optional): Hides the subprocess console
window on Windows.
7. THE SPAWN() METHOD
‘windowsHide’ (boolean, optional): Hides the subprocess console
window on Windows.
‘timeout’ (number, optional): The maximum amount of time in
milliseconds the process is allowed to run.
‘killSignal’ (string or integer, optional): The signal used to kill the
process when the ‘timeout’ option is specified.
8. CLUSTER MODULE
A cluster of child processes also referred to as worker processes, can be
created and managed using the cluster module, a built-in module in
Node.js. It offers a simple method to take advantage of multiple CPU cores,
enhancing the performance and scalability of Node.js applications.
The cluster module’s primary function is to make it possible to distribute
incoming network connections and requests among several workers,
enabling the application to support more concurrent connections. This is
accomplished by setting up a group of child processes that share a server
port. Each child process functions as a separate instance of the application,
able to respond to requests and execute parallel programs.