Processes Best Practices
Process and worker boundaries protect security and stability - spawn safely, size pools honestly, and shut down gracefully under orchestrator signals.
Search across all documentation pages
Process and worker boundaries protect security and stability - spawn safely, size pools honestly, and shut down gracefully under orchestrator signals.
child_process or shell: true.spawn/execFile with argument arrays over exec with shell. Eliminates injection on user-influenced input.shell: true with interpolated user strings. Code review blocker.maxBuffer consciously on execFile or avoid buffered APIs for large output. Stream with spawn instead.exit or handle error. Avoid zombies and FD leaks.postMessage.worker.on('error') and restart pool workers on failure. Prevent pool shrink to zero silently.process.pid in structured logs. Debug which worker handled a request.poolMax × workers × replicas. Stay under database max connections.server.close() and await DB pool.end() before exit. Close BullMQ workers too.exec(\git checkout ${branch}`)` with user-controlled branch - command injection.
Start with min(4, cores - 1) and profile - workload dependent.
Not on single VPS; uncommon inside K8s pods where replica scaling suffices.
PM2 adds reload and monitoring - still implement drain-friendly HTTP close in app code.
Possible but ensure schedulers and cron do not run in every worker without leader election.
spawn('npm', ['run', 'build']) without shell - cross-platform with documented npm path.
Test spawn paths on Windows CI if developers use mixed OS - document .cmd edge cases.
Libraries should not call process.exit - return errors to application shutdown orchestration.
child_process spawn ffmpeg CLI - worker_threads for pure JS compute.
pool 10 × cluster 8 workers × 3 pods = 240 DB connections - verify limit.
kill -TERM <pid> while load testing with k6 or curl loop.
Stack versions: This page was written for Node.js 24.18.0 (Active LTS), npm 10+, TypeScript 5.6+, Express 5, Fastify 5, and NestJS 11.
Reviewed by Chris St. John·Last updated Jul 16, 2026