Skip to content
Qiyang.Dev
Go back

Efficient Node.js

Edit page

Streams

Streams help Node.js process large amounts of data without loading everything into memory at once. They handle data in smaller chunks over time.

There are four main types of streams. Readable streams produce data, while writable streams receive it. Duplex streams can both read and write. Transform streams are duplex streams that also change the data as it passes through them.

Working with streams means handling different states and events. For example, a readable stream can be flowing or paused, and a writable stream may process data more slowly than a readable stream produces it. The pipeline method makes streams easier to use by handling many of these details, including errors.

Child processes

Child processes let a Node.js application run other programs, scripts, or shell commands. They can also communicate with the parent process when needed.

The node:child_process module provides asynchronous functions such as spawn, exec, and execFile. It also provides synchronous versions that wait for the child process to finish. The spawn function streams output, so it is useful for commands that produce a large amount of data. The exec function runs a command in a shell; spawn can also use a shell when configured to do so.

The fork function starts a Node.js script in a separate process. This lets the main process continue with other work while the child process runs.

Scaling

There are several ways to scale a Node.js application, including cloning, decomposing, and splitting. One approach is to run multiple copies of a server and share work between them.

The built-in node:cluster module lets a primary process create and manage worker processes. Workers can share a port, and the primary process can distribute incoming connections among them. This can help a server use multiple CPU cores.

By default on most platforms, node:cluster distributes connections among workers in turn. Other routing approaches or external services can help with caching and connections that need to keep state.

The primary process can monitor workers, replace those that fail, and restart them one at a time. This can help keep the server available when a worker stops or needs to restart.


Edit page
Share this post:

Previous Post
Minimal Expo Starter
Next Post
Slider with Popover