Node.js Architecture, V8 Engine & Single-Threaded Event Loop
Node.js Architecture, V8 Engine & Single-Threaded Event Loop
Node.js is an open-source, cross-platform JavaScript runtime environment built on Google Chrome's V8 JavaScript engine. Node.js executes JavaScript outside the browser, enabling developers to build scalable, high-performance network applications, microservices, and backend APIs using a single programming language across the entire stack.
1. What Makes Node.js Unique?
Traditional server environments (like Apache with PHP or Java servlets) historically spawned a new operating system thread for each incoming client request. A server handling 10,000 concurrent requests would require 10,000 threads, quickly exhausting server RAM and spending tremendous CPU cycles on thread context switching.
In contrast, Node.js operates on a single-threaded, non-blocking, event-driven architecture:
- 1Google V8 Engine: Written in C++, it parses and compiles modern JavaScript directly into machine code at runtime with Just-In-Time (JIT) compilation.
- 2Single-Threaded Execution: JavaScript application code runs on a single main thread, avoiding multithreaded race conditions and locking deadlocks.
- 3Libuv C Library: Handles all low-level asynchronous I/O operations (file system, network sockets, DNS, database pooling) using native OS kernel notifications (epoll on Linux, kqueue on macOS, IOCP on Windows) and an internal C thread pool.
2. The Architectural Diagram
3. How the Event Loop Works
The Event Loop is the heart of Node.js. It continuously monitors the Call Stack and task queues, orchestrating when asynchronous callbacks are pushed to the stack for execution.
The Event Loop cycles through several distinct phases in each "tick":
| Phase | Responsibility | Example Tasks |
|---|---|---|
| Timers | Executes callbacks scheduled by setTimeout() and setInterval() | Delayed notifications, interval timers |
| Pending Callbacks | Executes I/O callbacks deferred to the next loop iteration | System-level network socket error callbacks |
| Idle, Prepare | Used internally by Node.js for diagnostics and runtime housekeeping | Internal Libuv operations |
| Poll | Retrieves new I/O events and executes their callbacks | Incoming HTTP requests, file read buffers |
| Check | Executes callbacks registered by setImmediate() | Code explicitly deferred until after poll phase |
| Close Callbacks | Handles resource cleanup and teardown | socket.on('close', ...) |
4. Practical Code Example: Non-Blocking Execution
Output:
The main thread never waits for disk or network I/O. It delegates the I/O to Libuv and immediately continues processing subsequent synchronous statements.
Multiple Choice Questions
setImmediate() executed?Call Stack, Callback Queue, Microtask Queue & Libuv Workers
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| None | Call Stack, Callback Queue, Microtask Queue & Libuv Workers |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.