Node.js Working Mechanism
Node.js is a JavaScript runtime built on the Chrome V8 engine. It enables developers to write server-side code in JavaScript. Unlike traditional server-side technologies, Node.js adopts aEvent-drivenandnon-blocking I/Omodel, which makes it particularly suitable for handling highly concurrent network applications.
Core features
- single-threaded: Node.js uses a single thread to handle requests
- Event loop: concurrency is handled through an event-driven mechanism
- non-blocking I/O: I/O operations do not block the main thread
- Cross-platform: can run on Windows, Linux, macOS, and other systems
Node.js executes JavaScript code through the V8 engine, interacts with the operating system through the Node.js API, and handles asynchronous I/O operations through Libuv. The event loop and worker threads ensure the efficient, non-blocking nature of Node.js.
-
V8 JavaScript Engine: this is the core of Node.js, responsible for executing JavaScript code. V8 is the JavaScript engine of the Chrome browser; it compiles JavaScript code into machine code to improve execution efficiency.
-
Node.js Bindings (Node API): this layer provides a set of APIs that allow JavaScript code to interact with the operating system. These APIs cover operations on the file system, network, processes, and more.
-
Libuv (Asynchronous I/O): Libuv is a cross-platform asynchronous I/O library that runs beneath Node.js and handles asynchronous operations such as file system, network, and process operations. Libuv uses the event loop and worker threads to handle these operations without blocking the main thread.
-
Event Loop: this is one of the core concepts of Node.js. The event loop continuously checks the event queue, processes events, and executes callback functions. It ensures the non-blocking, event-driven nature of Node.js.
-
Event Queue: the event queue is used to store events about to be processed. When an asynchronous operation completes, the associated callback function is placed in the event queue, waiting to be processed by the event loop.
-
Worker Threads: these are threads used to handle blocking operations, such as file reads and writes and network requests. They allow Node.js to perform these operations without blocking the main thread.
-
Blocking Operation: these are operations that may block a thread, such as synchronous file reads and writes. In Node.js, such operations are usually placed on worker threads to avoid blocking the event loop.
-
Execute Callback: once an asynchronous operation completes, its callback function is executed. This is managed by the event loop.
Architecture components of Node.js
The architecture of Node.js can be divided into the following main layers:
1. JavaScript layer
This is the layer developers interact with directly, including:
- Core modules (such as fs, http, path, etc.)
- Third-party modules (installed via npm)
- User-defined modules
2. C++ bindings layer
This layer exposes low-level functionality to the JavaScript layer, including:
- C++ implementations of the Node.js core APIs
- Interface wrappers for the V8 engine
3. Underlying dependencies
- V8 engine: a JavaScript engine developed by Google
- libuv: a cross-platform asynchronous I/O library
- c-ares: an asynchronous DNS resolution library
- OpenSSL: cryptographic functionality support
- zlib: compression functionality support

Event loop mechanism
The core working mechanism of Node.js is theEvent loop, which is responsible for scheduling and executing all asynchronous operations.
Phases of the event loop
- timers: execute callbacks of setTimeout and setInterval
- pending callbacks: execute callbacks of system operations (such as TCP errors)
- idle, prepare: for internal use
- poll: retrieve new I/O events and execute associated callbacks
- check: execute callbacks of setImmediate
- close callbacks: execute callbacks of close events (such as socket.on('close'))
Example
// example: understanding event loop order
setTimeout
(
(
)
=>
console.
log
(
'timeout'
)
,
0
)
;
setImmediate
(
(
)
=>
console.
log
(
'immediate'
)
)
;
// output order may differ depending on when the event loop starts
Non-blocking I/O principle
I/O operations in Node.js are non-blocking, achieved through the following:
Workflow
- The application initiates an I/O request (such as reading a file)
- Node.js passes the request to libuv for processing
- libuv uses system-provided asynchronous interfaces (such as Linux's epoll)
- The main thread continues executing other tasks
- When the I/O completes, the callback function is placed in the event queue
- The event loop executes the callback at the appropriate phase

Single thread and multiple processes
Although Node.js is single-threaded, it can leverage multi-core CPUs in the following ways:
1. Child process (child_process)
Example
const
{
fork
}
=
require
(
'child_process'
)
;
const
child
=
fork
(
'child.js'
)
;
child.
on
(
'message'
,
(
msg
)
=>
{
console.
log
(
'Message from child process:'
,
msg
)
;
}
)
;
child.
send
(
{
hello
:
'world'
}
)
;
2. Cluster mode (cluster)
Example
const
cluster
=
require
(
'cluster'
)
;
const
http
=
require
(
'http'
)
;
const
numCPUs
=
require
(
'os'
)
.
cpus
(
)
.
length
;
if
(
cluster.
isMaster
)
{
// the main process forks worker processes
for
(
let i
=
0
;
i
<
numCPUs
;
i
++
)
{
cluster.
fork
(
)
;
}
}
else
{
// worker processes create an HTTP server
http.
createServer
(
(
req
,
res
)
=>
{
res.
writeHead
(
200
)
;
res.
end
(
'Hello World\n'
)
;
}
)
.
listen
(
8000
)
;
}
3. Worker Threads
Example
const
{
Worker
}
=
require
(
'worker_threads'
)
;
const
worker
=
new
Worker
(
`
const
{
parentPort
}
=
require
(
'worker_threads'
)
;
parentPort.
on
(
'message'
,
(
msg
)
=>
{
console.
log
(
'Message received:'
,
msg
)
;
parentPort.
postMessage
(
'Message received'
)
;
}
)
;
`
,
{
eval
:
true
}
)
;
worker.
on
(
'message'
,
(
msg
)
=>
{
console.
log
(
'Reply from worker thread:'
,
msg
)
;
}
)
;
worker.
postMessage
(
'Main thread message'
)
;
Performance Optimization Suggestions
1. Avoid blocking the event loop
- Offload CPU-intensive tasks to worker threads or child processes
- Avoid complex computations on the main thread
2. Use stream processing wisely
Example
// bad practice: reading a large file all at once
fs.
readFile
(
'bigfile.txt'
,
(
err
,
data
)
=>
{
// Process data
}
)
;
// good practice: using streams
const
stream
=
fs.
createReadStream
(
'bigfile.txt'
)
;
stream.
on
(
'data'
,
(
chunk
)
=>
{
// process data chunks
}
)
;
3. Connection pool management
- Database connection
- HTTP client connections
4. Memory management
- Monitor memory usage
- Avoid Memory Leaks
Frequently Asked Questions
Q: Is Node.js really single-threaded?
A: JavaScript execution is single-threaded, but Node.js uses multiple threads underneath (such as libuv's thread pool handling certain I/O operations).
Q: How to handle CPU-intensive tasks?
A: You can use Worker Threads, or split the task into smaller tasks and process them in batches via setImmediate.
Q: Why is Node.js suitable for I/O-intensive applications?
A: Because its non-blocking I/O model can handle other requests while waiting for I/O, without creating a new thread for each connection.
Summary
Node.js's working mechanism is based on the following core concepts:
- Event-driven programming model
- Non-blocking I/O operations
- Single-threaded but supports multi-process/multi-thread extension
- Efficient event loop scheduling
Understanding these mechanisms is crucial for writing high-performance Node.js applications. By making good use of its asynchronous features, you can build network applications capable of handling high concurrency.