Last Updated: May 2, 2026
Node.js production patterns: clustering, graceful shutdown, health checks, structured logging with pino, memory management, and Docker optimization.
Clustering & Process Management
| Approach | Details |
|---|---|
| Built-in cluster | const cluster = require('cluster'); cluster.fork() |
| PM2 | pm2 start app.js -i max |
| Worker threads | new Worker('./heavy-task.js') |
Graceful Shutdown
SIGTERM handlerprocess.on('SIGTERM', () => server.close(...))KubernetesSet terminationGracePeriodSeconds to match Node timeout.
Structured Logging with Pino
pino()Fastest Node logger — JSON output, child loggers for request context.
pino-prettyDevelopment only — never in production.
Docker Optimization
| Technique | Benefit |
|---|---|
node:22-alpine | Smallest image (~50MB) |
| Multi-stage builds | Copy only production artifacts |
.dockerignore | Exclude node_modules, .git |
Pro Tip: Set up /health endpoint verifying DB and cache — Kubernetes uses this for readiness probes.