What does event-driven and non-blocking I/O mean?

Abshir Jama
2 min readDec 15, 2019

--

You may have heard a lot that Nodejs is an event-driven, non-blocking I/O. What does that mean? It sounded really confusing when I first heard it. At the end of this discussion, you will have a strong grasp of what this statement means.

Let us dive right into it.

Let us break the statement into two parts. EVENT-DRIVEN and NON-BLOCKING I/O

What is event-driven?

Event driven means when you make a call, your node server catches the request and registers a function. Let me repeat, Nodejs registers a function instead of the actual data. Why though? Here is why. Instead of having the event-cycle to wait for the data while it’s fetching (which could take sometime), node simply registers an event that will be called when the fetching is done and frees the event-cycle to take the next request.

What is non-blocking I/O

Node treats your event-cycle as a waiter at a restaurant. When it delivers a request to it, it frees it right away. Meaning that the waiter doesn’t have to wait till the food is ready to take the next order. Think of a restaurant where each customer will be served by one separate waiter. That will be hard to manage and would be space consuming as well, isn’t it?

So, node says, let us not do that. Let us have a one waiter, like a normal restaurant and take orders from all of the customers and serve them when their food is ready. One person might order a special food that could take forever to cook, and another one might just order a soup or something light that would take a minute or two. So, the latter should not be blocked by the first.

The event-cycle is like a waiter, whenever a request is made, node takes that and registers callback function (like saying the food is being cooked, go and take other orders). Now, the event-cycle is freed and ready to take the next request and the end-user feels a smooth transition between requests.

I hope this brief explanation helps.

--

--