InterviewHubPRO
Section Overview
Q3

Question 3 of 60

Part 1: Intro, Candid Experience & Architecture (1 - 5)

5%
Node.js & Libuv•Part 1: Intro, Candid Experience & Architecture (1 - 5)

Describe a critical production incident or performance bottleneck you resolved in Node.js.

How to Explain in an Interview:

  • Problem: In an Express payment gateway, service instances intermittently stopped responding, leading to Kubernetes liveness probe failures and container restart loops.
  • Root Cause: A CPU-intensive PDF invoice generation library was being invoked synchronously inside request handlers, blocking the single-threaded Node.js Event Loop for up to 3.8 seconds per request. Because the main thread was completely frozen, Node.js could not respond to inbound health check TCP pings.
  • Fix:
    1. Extracted PDF generation to a dedicated Worker Thread pool using Piscina, running computation on isolated OS worker threads.
    2. Implemented monitorEventLoopDelay from perf_hooks to expose Event Loop lag metrics to Prometheus. Container CPU leveled out, and health check timeouts were eliminated.
javascript
// Solution: Worker Thread pool for heavy CPU processing
const Piscina = require('piscina');
const piscina = new Piscina({ filename: path.resolve(__dirname, 'pdf-worker.js') });

app.post('/invoice', async (req, res) => {
  // Offloads heavy CPU work without freezing the main Event Loop thread:
  const pdfBuffer = await piscina.run(req.body);
  res.setHeader('Content-Type', 'application/pdf');
  res.send(pdfBuffer);
});