Full answer
Symptoms
- All endpoints get slow at once, not only the heavy one. Health checks fail and the orchestrator restarts healthy-looking instances (which makes things worse).
- One CPU core is at 100 percent. Database and network latency look normal.
- Event loop delay (monitorEventLoopDelay) or event loop utilization is high.
Confirming
- Measure event loop delay in production and alert on p99.
- Take a CPU profile (node --cpu-prof, or the inspector) and look for a wide synchronous function: a *Sync API, JSON.parse or JSON.stringify of a large object, a regular expression with heavy backtracking, a big loop, template rendering, image processing, or password hashing.
Fixes (choose by the kind of work)
- Use the async version of an API when one exists. crypto.pbkdf2, scrypt and bcrypt run in the libuv thread pool, so the main thread stays free. Note the pool has 4 threads by default.
- CPU-heavy JavaScript: move it to a worker thread (a pool such as piscina), or to a separate service or queue.
- Large data: stream and process in chunks, and yield to the event loop between chunks (setImmediate).
- Regex: avoid nested quantifiers on user input, set input length limits.
- Never use *Sync functions in request handlers (they are fine at start-up and in scripts).
- Keep the health endpoint cheap and separate from heavy work, and set timeouts that avoid restart storms.
javascript
const http = require('node:http');
const crypto = require('node:crypto');
const server = http.createServer((req, res) => {
if (req.url === '/heavy-sync') {
crypto.pbkdf2Sync('password', 'salt', 400000, 64, 'sha512'); // blocks the event loop
res.end('heavy');
} else if (req.url === '/heavy-async') {
crypto.pbkdf2('password', 'salt', 400000, 64, 'sha512', () => res.end('heavy')); // runs in the thread pool
} else {
res.end('health');
}
}).listen(0, '127.0.0.1', async () => {
const get = (path, order) => new Promise((resolve) => {
http.get({ port: server.address().port, host: '127.0.0.1', path, agent: false }, (res) => {
res.resume();
res.on('end', () => { order.push(path); resolve(); });
});
});
for (const heavy of ['/heavy-sync', '/heavy-async']) {
const finished = [];
await Promise.all([get(heavy, finished), get('/health', finished)]); // heavy is sent first, then the health check
console.log(heavy.padEnd(13), 'responses finished in this order:', finished.join(', '));
}
server.close();
});Each run sends the heavy request first and then a health check. The order in which the responses finish shows whether the health check had to wait for the heavy work. Save it as app.js and run it with node app.js. Output (Node.js v24.22.1, run once): /heavy-sync responses finished in this order: /heavy-sync, /health /heavy-async responses finished in this order: /health, /heavy-async
Key points
- One thread runs all JavaScript, so sync work blocks everything.
- Symptoms: high latency everywhere and one core at 100 percent.
- Measure event loop delay and profile the CPU.
- Use async APIs, worker threads, chunking, or another service.
- Do not use *Sync APIs in request handlers.
Common mistakes
- Using pbkdf2Sync, readFileSync or JSON work inside a request handler.
- Scaling out instances instead of removing the blocking code.
- Health checks that share the same slow code path.
- Assuming async functions always run off the main thread.
- Increasing the thread pool size to fix a CPU-bound JavaScript loop.
Follow-ups
- Why does the async version not block the loop?
- When is a worker thread better than the thread pool?
- How do you protect against regex denial of service?
Tips
- Describe symptoms before fixes.
- Say 'measure loop delay and profile'.
- Give three fixes by kind of work.