,

Architectural Overview of I/O in Node.js

Posted by






NodeJS Architecture – I/O

NodeJS Architecture – I/O

Node.js is an open-source server-side runtime environment built on the V8 JavaScript engine developed by Google. It allows developers to run JavaScript code outside of a web browser and has gained popularity for its efficient handling of input and output (I/O) operations.

Understanding I/O in NodeJS

Node.js follows an event-driven architecture, which means it uses a single-threaded event loop to process requests asynchronously. This design choice allows Node.js to handle a large number of concurrent connections without blocking the execution of other code.

Input and output operations in Node.js involve reading or writing data to external resources such as files, databases, network sockets, or other services. These operations often involve long waiting times, which can negatively impact performance in traditional server models.

Non-Blocking and Asynchronous I/O

Node.js utilizes non-blocking and asynchronous I/O operations, which means that while waiting for a particular operation to complete, the application can continue executing other tasks. This approach maximizes CPU utilization and overall performance.

When a non-blocking I/O operation is initiated, Node.js registers a callback function that will be executed when the operation completes. During this waiting period, the event loop continues to process other incoming requests or execute other code.

Event-Driven and Event Loop

The event-driven architecture of Node.js allows developers to build highly scalable and responsive applications. Asynchronous I/O operations combined with the event loop enable efficient handling of multiple concurrent requests.

The event loop is at the heart of Node.js’ architecture. It continuously checks for events, such as I/O operations completing or timers expiring, and triggers the corresponding callback functions. This mechanism guarantees that the application remains responsive and can quickly shift between different tasks.

Benefits of NodeJS I/O Architecture

The I/O architecture of Node.js offers several advantages:

  • Scalability: Node.js can handle a large number of concurrent connections efficiently, making it suitable for applications that require high scalability.
  • Performance: Asynchronous I/O operations and the event loop maximize CPU utilization and ensure responsiveness.
  • Productivity: Node.js uses JavaScript as its programming language, allowing front-end developers to leverage their existing skills for server-side development.
  • Community and Ecosystem: Node.js has a vibrant and active community, providing a vast library of modules and packages to extend its functionality.

Conclusion

Node.js’ I/O architecture, based on non-blocking and asynchronous operations combined with the event-driven event loop, allows for scalable, high-performance applications. It enhances productivity for developers and benefits from a thriving ecosystem. By leveraging these features, Node.js has become a popular choice for building server-side applications.


0 0 votes
Article Rating
17 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Ali Adel
1 year ago

Node can be multithreaded with the worker thread. or am I wrong?

Adesina Timilehin
1 year ago

Thank you very much. This is very valuable information.

Sandeep Parmar
1 year ago

Really Intringing , Changed my perspective of NodeJS. And as alwyas great content.

wrlee
1 year ago

The definition of "blocking" is not simply semantics, it's a matter of stratifying the kinds of blocking you are talking about. Simply stated, blocking is when the application's execution is waiting for something to happen. Waiting for the kernel to transfer a buffer to app/user space is quite different than waiting for data to appear on a socket, but both are "blocking," at different _layers_—the former is more local while the later is dependent on external activity. We might even describe the wait for a CPU instruction to complete to be blocking—a hyper-local form of blocking. So we can analyze blocking by looking at the system architecture and what the application is waiting on: user, internet network (and other non-deterministic external systems), infrastructure architecture (e.g., external storage vs internal storage), operating system, bus architecture, CPU. The solution for mitigating blocking effects on applications depends on the solutions to circumventing delays between and within those layers.

Igor Roztropiński
1 year ago

Love these deeep dives 😉

elakstein
1 year ago

If you are doing cpu intensive task then it will block other tasks as everything is running on a single thread.

Let's say you do a math heavy calculation and it takes 3 seconds to complete. Then your application will be blocked for those 3 seconds and it won't be able to do anything else.

In cases like this, multi threaded application will do better, as these type of computation you can do in a separate thread.

Biplob Manna
1 year ago

Very informative, but now I have more questions about how the underlying syscalls work. Time to dive into the rabbit hole.

Tenth Legion Studios
1 year ago

Great video. A more in depth video on this I found useful was by Kprotty about Zig's IO. He goes super deep into all of this, and does an iceberg on concurrent models and IO. Very cool. ScyllaDB is at the bottom of this iceberg. It also goes in depth into rust tokio and GOs IO model. And it expands a lot on IOuring. Also Bun (competitor to node) basically just uses KProttys implementation of IO to greatly outperform node. Bun uses zig under the hood and is basically rewriting the parts of node that interact with the OS kernel. Link Here: https://www.youtube.com/watch?v=Ul8OO4vQMTw

Orión González
1 year ago

Where do the quotes come from? I've done a google search and I got no results aside from your post

Isuru Cumaranathunga
1 year ago

Really Really Really appreciate your effort and thank you so much for this valuable content Hussein. I love you content so much. Hussein what about the Vert.x library in JAVA. It is also using this asynchronous processing using a concept called verticles and event bus. I highly appreciate if you could do a content on that also. Looking forward for another great content like this. Thanks

Mahedi Hassan Shawon
1 year ago

Thanks! for these contents

dog4ik
1 year ago

I was always wondering how single threaded js backends can accept so many concurrent connections. Great article!

Ab. R.
1 year ago

I read that async operations like network request and async file reading are executed once the stack is empty, so, until then, where are these async operations stored? did Node use any queue data structure for that

Nada Que Ver
1 year ago

tyvm mr. nasser. kickass content

Dante Osborne
1 year ago

How would you compare Node.js to Spring Boot? afaik Netflix used to have the first but now is using the latter. Which one would you recommend for a new platform with a small amount of resources? (I opted for Spring Boot btw)

Shiv raj
1 year ago

super helpful following you since I discovered you .Can you please make a video about auth server and resource server relating to your recent linkedIn post?

B R
B R
1 year ago

Nice examples here too in one of your earlier videos. Thanks Hussein! https://www.youtube.com/watch?v=gMtchRodC2I