Node.js
Production tuning
Learn production tuning and Node.js flags.
Production tuning
Production tuning is the set of adjustments that keep a Node.js application at its best in production. Memory uses --max-old-space-size, CPU uses flame graphs, GC uses --expose-gc, and library choice belongs here too.
A tuningAdvice(issue) function models these decisions: 'memory' returns 'limit heap', 'cpu' returns 'profile flame', others return 'check flags'. Every tuning has a cost in production; a wrong flag can cause unexpected crashes. So changes ship gradually and each is followed by measurement. This simple model reduces the issue to a category and points at the right knob; in real settings each tuning needs its own experiment.
Flags and measurement
In production, measurement must precede any flag change. --max-old-space-size raises the heap ceiling but can hide a leak. --expose-gc allows manual garbage collection calls but misuse adds latency.
Example: tuningAdvice('memory') returns 'limit heap'; first cap the heap. tuningAdvice('cpu') returns 'profile flame'; find the bottleneck with a flame graph. No single setting stands alone; each must be tested under the real workload that runs the app. This is why good teams ship settings through an experiment-measure-compare loop. Even a correct flag can cause trouble if applied for the wrong reason.
Exercise Write the tuning advice function
Write function tuningAdvice(issue). Add if (issue === 'memory') return 'limit heap';, if (issue === 'cpu') return 'profile flame'; and return 'check flags';. Print console.log(tuningAdvice('memory')) and console.log(tuningAdvice('cpu')).
Show example solution
function tuningAdvice(issue) {
if (issue === 'memory') return 'limit heap';
if (issue === 'cpu') return 'profile flame';
return 'check flags';
}
console.log(tuningAdvice('memory'));
console.log(tuningAdvice('cpu'));
Test yourself
-
Complete the memory branch
if (issue === 'memory') return '____';
Show answer
if (issue === 'memory') return 'limit heap';
-
What does tuningAdvice return for a CPU bottleneck?
tuningAdvice('cpu')
- limit heap
- check flags
- profile flame
- more memory
Show answer
profile flame — cpu returns 'profile flame'; a flame graph shows the bottleneck.
-
Match each piece to its role
- memory
- limit heap
- cpu
- profile flame
- --max-old-space-size
- Heap ceiling
- --expose-gc
- Manual GC
Next lesson: Summary - Performance engineering