Please enable JavaScript.
Coggle requires JavaScript to display documents.
Node Js - Scale Node Application (Why? (allows us to fork work from the…
Node Js - Scale Node Application
Why?
allows us to fork work from the main application to
separate
processes
that can be processed in parallel with each other and the main application
Most servers have
multiple processors
child_process
and
cluster built-in modules
available in Node.js
child_process module
What?
to facilitate the creation of
child processes
to leverage
parallel processing
on multi-core CPU based system
Methods
spawn(command, [args], [options])
: Launches a new child process and returns a child process object.
exec(command,[options],callback )
: Launches a new child process and runs the given command in a shell.
Fork(modulepath,[args],[options])
: Specialized form of spawn() method for creating child processes.
cluster Module
What?
built on top of
child_process.fork()
method which is used to do the
load balancing
between the child processes running in parallel
Method
cluster.fork():
Spawn a new child process and all the child processes share the same server ports.
Events
fork
: Emitted when a new worker is forked.
online
: Emitted when the worker is running.
listening
: Emitted when worker becomes online and start listening to the requests.
exit
: Emitted when a worker is killed.