This might seem like a basic question, but I could not find any documentation :
What is the difference between forking & spawning a node.js process? I have read
child_process.spawn method launches a new process with a given command. It has the following signature −
child_process.spawn(command[, args][, options])
Read more about options
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.
child_process.fork method is a special case of spawn() to create Node processes. It has the following signature −
child_process.fork(modulePath[, args][, options])
The fork method returns an object with a built-in communication channel in addition to having all the methods in a normal ChildProcess instance.
Spawn is a command designed to run system commands. When you run spawn, you send it a system command that will be run on its own process, but does not execute any further code within your node process. You can add listeners for the process you have spawned, to allow your code interact with the spawned process, but no new V8 instance is created(unless of course your command is another Node command, but in this case you should use fork!) and only one copy of your node module is active on the processor.
Fork is a special instance of spawn, that runs a fresh instance of the V8 engine. Meaning, you can essentially create multiple workers, running on the exact same Node code base, or perhaps a different module for a specific task. This is most useful for creating a worker pool. While node's async event model allows a single core of a machine to be used fairly efficiently, it doesn't allow a node process to make use of multi core machines. Easiest way to accomplish this is to run multiple copies of the same program, on a single processor.
A good rule of thumb is one to two node processes per core, perhaps more for machines with a good ram clock/cpu clock ratio, or for node processes heavy on I/O and light on CPU work, to minimize the down time the event loop is waiting for new events. However, the latter suggestion is a micro-optimization, and would need careful benchmarking to ensure your situation suits the need for many processes/core. You can actually decrease performance by spawning too many workers for your machine/scenario.
Ultimately you could use spawn in a way that did the above, by sending spawn a Node command. But this would be silly, because fork does some things to optimize the process of creating V8 instances. Just making it clear, that ultimately spawn encompasses fork. Fork is just optimal for this particular, and very useful, use case.
http://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback
TLDR
Spawn
When a spawn is created - It creates a streaming interface between parent and child process.
streaming interface means - buffering data in binary format in ONE TIME
Fork
When a fork is created - It creates a communication channel between parent and child process
communication channel means - messaging
Difference
Well both look kind of doing same data transfer, Except below difference
spawn will be useful when you want to do continuous data buffer in binary/encoding format , Eg - Transfer 1gb video file,image,log files in ONE TIME
fork will be useful when you want to do messaging
Eg - JSON
or XML
data messaging
Conslusion
spawn should be used for streaming big data/files/images FROM spawn process TO parent process
fork should be used for doing Json/Xml messaging .