Blocking I/O in Node.js

The Node.js event model of using event callbacks is great until you run into the problem of functions that block on I/O. Blocking I/O stops the execution of the current thread and waits for a response before continuing. Some examples of blocking I/O are:

Image Reading a file

Image Querying a database

Image Requesting a socket

Image Accessing a remote service

Node.js uses event callbacks to avoid having to wait for blocking I/O. Therefore, any requests that perform blocking I/O are performed on a different thread in the background. Node.js implements a thread pool in the background. When an event that blocks I/O is retrieved from the event queue, Node.js retrieves a thread from the thread pool and executes the function there instead of on the main event loop thread. This prevents the blocking I/O from holding up the rest of the events in the event queue.

The function that is executed on the blocking thread can still add events back to the event queue to be processed. For example, a database query call is typically passed a callback function that parses the results and may schedule additional work on the event queue before sending a response.

Figure 4.3 illustrates the full Node.js event model, including the event queue, event loop, and thread pool. Notice that the event loop either executes the function on the event loop thread itself or, for blocking I/O, executes the function on a separate thread.

Image

Figure 4.3 In the Node.js event model work is added as a function with callback to the event queue and then picked up on the event loop thread. The function is then executed on the event loop thread in case of non-blocking or on a separate thread in the case of blocking.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.93.30