ReasonJun

Node.js : Blocking / non-Blocking 본문

Backend/Node.js

Node.js : Blocking / non-Blocking

ReasonJun 2023. 9. 3. 23:50
728x90

In Node.js, blocking and non-blocking refer to how I/O (input/output) operations are handled by the runtime environment. Understanding the difference between these two approaches is crucial for building efficient and responsive applications, especially in scenarios with high concurrency, such as web servers. Let's explore both concepts:

 

Blocking I/O (Synchronous):

  • In a blocking or synchronous I/O operation, the program waits for the operation to complete before proceeding to the next task. During this wait time, the entire program is "blocked" or halted.
  • This means that if an I/O operation takes a long time to complete, it will hold up the entire application, making it unresponsive to other tasks.
  • Traditional programming languages often use blocking I/O operations by default because they are conceptually simpler to work with.
  • Example in Node.js (blocking file read):
const fs = require('fs');

// Blocking I/O (Synchronous)
const data = fs.readFileSync('file.txt');
console.log(data.toString());

console.log('Program continues...');

In this example, the program will not proceed to the "Program continues..." line until the file read operation is finished.

 

Non-Blocking I/O (Asynchronous):

  • In non-blocking or asynchronous I/O operations, the program does not wait for the operation to complete before moving on to the next task. Instead, it initiates the operation and continues executing other code.
  • When the I/O operation is finished, a callback function (or a Promise) is triggered to handle the results. This approach allows the application to remain responsive, even when dealing with potentially slow I/O operations.
  • Node.js heavily relies on non-blocking I/O, making it well-suited for building scalable, high-performance applications.
  • Example in Node.js (non-blocking file read):
const fs = require('fs');

// Non-blocking I/O (Asynchronous)
fs.readFile('file.txt', (err, data) => {
  if (err) {
    console.error(err);
    return;
  }
  console.log(data.toString());
});

console.log('Program continues...');

In this example, the program will immediately proceed to the "Program continues..." line without waiting for the file read operation to finish. The callback function is invoked once the operation is complete.

 

In Node.js, it's generally recommended to use non-blocking, asynchronous I/O operations whenever possible, especially for tasks like handling HTTP requests, file I/O, and database queries. This approach allows Node.js applications to handle many concurrent connections efficiently, making them highly scalable and responsive.

 

Blocking operations should be used sparingly and only in situations where you specifically want to wait for an operation to complete before proceeding. However, excessive use of blocking operations can lead to poor application performance and responsiveness, which is counter to Node.js's strengths.

728x90

'Backend > Node.js' 카테고리의 다른 글

Node.js : How make asynchronous L/O (event loop)  (0) 2023.09.04
Single Thread / Multi Thread  (0) 2023.09.04
Node.js : bindings  (0) 2023.09.02
In Node.js, the relationship between V8 and libuv  (0) 2023.09.02
Node.js : libuv concept  (0) 2023.09.02
Comments