# How Node.js Handles Multiple Requests with a Single Thread

# Single-Threaded Nature of Node.js

Node.js runs on a **single main thread**, meaning:

*   One thread executes JavaScript code
    
*   No traditional multi-threaded request handling (like Java, Python with threads)
    

### Key implication:

If Node.js processed requests synchronously, it would block:

```javascript
// Blocking example (bad)
const data = fs.readFileSync("file.txt");
```

This would freeze the server until the operation completes.

# The Event Loop: Core of Concurrency

The **event loop** is the engine that enables Node.js to handle multiple operations efficiently.

### How it works:

1.  Incoming requests are registered
    
2.  Heavy operations are offloaded
    
3.  Callbacks are queued
    
4.  Event loop executes them when ready
    

### Example:

```javascript
console.log("Start");

setTimeout(() => {
  console.log("Async Task Done");
}, 2000);

console.log("End");
```

**Output:**

```shell
Start
End
Async Task Done
```

# Delegating Tasks to Background Workers

Node.js does NOT do everything itself.

It delegates tasks to:

### 🔹 OS Kernel (via libuv)

*   Network requests
    
*   File system operations
    
*   Timers
    

### 🔹 Thread Pool (libuv worker pool)

*   CPU-heavy tasks (crypto, compression)
    
*   Some file operations
    

### Example:

```javascript
fs.readFile("file.txt", (err, data) => {
  console.log("File read complete");
});
```

*   Node registers the task
    
*   OS handles it
    
*   Callback runs later
    

# Handling Multiple Client Requests

Let’s say 1000 users hit your server simultaneously.

### Traditional (blocking model):

*   1 thread per request
    
*   High memory usage
    
*   Context switching overhead
    

### Node.js model:

*   Single thread handles all requests
    
*   Uses async callbacks/promises
    
*   No waiting/blocking
    

### Example server:

```javascript
const http = require("http");

http.createServer((req, res) => {
  setTimeout(() => {
    res.end("Response sent");
  }, 2000);
}).listen(3000);
```

# Why Node.js Scales So Well

### 1\. Non-blocking I/O

*   No waiting for operations
    
*   Efficient CPU usage
    

### 2\. Event-driven architecture

*   Reacts to events instead of polling
    

### 3\. Low memory footprint

*   No thread per request
    

### 4\. High concurrency

*   Handles many requests simultaneously
    

### 5\. Fast execution (V8 engine)

*   Compiles JS to machine code
