A dive into nodejs I/O

Network Sockets, Files, DNS and threads

Hussein Nasser
8 min readOct 11, 2022

Node is a non-blocking JavaScript runtime. You can concurrently run an HTTP Server, read files from disk, send UDP datagrams, accept TCP connections from clients and still have room to execute JavaScript code operations without blocking. Most of these operations are known as I/O, you send an input to a device, file or a socket and it replies back with an output. Node achieves non-blocking I/O with mostly a single thread executed asynchronously using a library called lib_uv.

The exception being DNS queries, which use a thread pool. This means when you use fetch or axios to make an HTTP request to a domain, the DNS resolution for that domain will most probably go through the thread pool, while the actual request itself will be sent asynchronously on the main thread.

So when does Node block and when it doesn’t? I attempt to answer this question in this article by diving deep into the NodeJS I/O system.

libuv library in NodeJS

Socket I/O

When a backend application binds to an address and a port it creates a socket. Attempts to conenct to the socket will spawn connections that can be accessed through file descriptors. File descriptors are integer values representing TCP connections, UDP sockets or even literal files on disk. When a client sends data on the…

--

--

Hussein Nasser

Software Engineer passionate about Backend Engineering, Get my backend course https://backend.win