Chapter 7. Child Processes
Running a Node instance in a single process works fine for a small application. As the application gets more complex and serves more users, a single process is not going to be enough to handle the increasing workload. No matter how powerful your server may be, a single thread can support only a limited load.
The fact that your Node code runs in a single thread does not mean that you can’t take advantage of multiple processes and, of course, multiple machines as well.
Using multiple processes is the best way to scale a Node application. Node is designed for building distributed applications with many nodes. This is why it’s named Node! Scalability is baked into the platform, and it’s not something you start thinking about later in the lifetime of an application.
Introducing Child Processes
We can easily create a child process in a main Node process using the node:child_process built-in module. These child processes can communicate with each other with an event-driven messaging system because process objects implement the EventEmitter structure under the hood.
The node:child_process module enables us to efficiently run OS commands (like find, grep, etc.) from within a Node process, safely control the arguments passed to these commands, and do whatever we want with the commands’ output. This gives a Node process a lot more power over what it can do.
In addition, the node:child_process module supports both buffers and streams to process the input and output ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access