Skip to main content
Node.js Intermediate System & Architecture Topic: Runtime & Event Loop

Explain the Node.js libuv event loop and why CPU-heavy work blocks all clients

Quick Summary: Node.js executes JavaScript on a single main thread orchestrated by libuv’s multi-phase event loop (timers, pending, poll, check, close); OS async kernels handle network sockets while heavy synchronous CPU loops stall every concurrent request.

Written by Ravi Shankar Mishra (Founder & Lead Systems Architect, MockAttempt Academy) Reviewed by MockAttempt Engineering Editorial Desk
Last verified:

Direct Interview Answer

Every incoming HTTP request shares one V8 JavaScript main thread. Non-blocking sockets use OS mechanisms (epoll/kqueue) via libuv without allocating threads, while fs, DNS lookup, and crypto.pbkdf2 offload to libuv’s worker pool (default size 4).

Between each event loop phase, Node drains process.nextTick() first, then the V8 Microtask Queue (Promise.then), before entering the next phase.

Detailed Technical Explanation

If any handler runs synchronous image resizing, PDF parsing, or massive JSON regex matching on the main thread, P99 latency spikes for every user. Offload CPU-intensive computations cleanly to worker_threads or external job queues (BullMQ).

Practical Walkthrough

Demonstrating nextTick, Promise microtasks, setTimeout, and setImmediate ordering in Node.js.

Code Example — JAVASCRIPT
const fs = require("fs");

fs.readFile(__filename, () => {
    setTimeout(() => console.log("4: timers phase"), 0);
    setImmediate(() => console.log("3: check phase (runs before timer inside I/O)"));
    Promise.resolve().then(() => console.log("2: promise microtask"));
    process.nextTick(() => console.log("1: process.nextTick"));
});
Expected Output / Verification
1: process.nextTick
2: promise microtask
3: check phase (runs before timer inside I/O)
4: timers phase

Real-World Production Use Case

Diagnosing sudden API latency spikes in Express/NestJS microservices caused by synchronous JWT signing loops or small UV_THREADPOOL_SIZE under bcrypt/crypto bursts.

Common Interview Mistakes to Avoid

  • Calling fs.readFileSync or crypto.pbkdf2Sync inside HTTP request handlers.
  • Recursing process.nextTick() infinitely, starving the I/O poll phase completely.
Topics: #nodejs #libuv #event-loop #worker-threads
Learn. Practise. Prove. Get Career-Ready.

Ready to Build Your IT Career?

Explore industry-led programmes and develop practical skills through training, assessments and interview preparation.