Published on

Non-blocking I/O in JavaScript

What is Non-blocking I/O in JavaScript?

Non-blocking I/O is simplify the way Javascript handles input/output without blocking the main thread and go back to handle it with the callbacks or promises.

Why Non-blocking I/O is important?

Non-blocking I/O thread handles thousand of concurrent connections without waiting for slow tasks (reading files, calling apis, querying databases,...). This would be efficient for the early-stage projects with heavy I/O operations. You build a server and it has multiple queries to read files, call APIs, or query databases but you don't block the main thread to handle any specific task. Instead, the call stack handle it and push the result to the queue and then the event loop will pick it up and execute the callback or promise when the main thread is free.

This is not a new but Javascript take it as core concept to running the code. When handle a bunch of requests, the main thread will throw the result to the queue that we feel as multiple-threading. This mechanism allows the server to serve a large number of requests concurrently, hold it and process each request incrementally (handle it first and then go to the callback later).

To more understand, imagine it like a rice stall. When they have multiple customers at a time (example 100 customers). They serve the first with rice, braised pork and soup. Then they go to the second customer and serve them next. Every customer take their plate of rice and sit at the table to eat. After finishing the meal they come back and pay the rice stall.

In the example above, the rice stall is the main thread. They serve each customer (request) one by one but they don't wait for them to finish eating (callback). Instead, they go to the next customer and serve them. When the customers finish eating, they come back to pay (callback execution). This way, the rice stall can handle many customers without waiting for each one to finish before moving on.

Our system is designed like that and it is very fast to handle a large number of requests.

When to use Non-blocking I/O?

Use it when your application is:

  • I/O bound: the performance is limited by the speed of input/output operations (e.g., reading files, network requests).
  • High concurrency: the application needs to handle many simultaneous connections or requests.
  • Event-driven: the application relies on events and callbacks to handle asynchronous operations.

Where to use Non-blocking I/O?

Non-blocking I/O suitable for

  • Web servers, APIs backend: handle thousands of requests concurrently.
  • Real-time apps: chat applications, online games, live updates.
  • Microservices: working as a service to call other services without blocking the main thread or interact with the files.

Conclusion

Non-blocking I/O is a powerful feature of Javascript that allows the system to handle multiple requests concurrently. I believe that you already use it in your code but not realize it. It it a core concept of Javascript and it is very important to understand how it works. It helps you to write efficient code and improve the performance of your application.