Blocking and non-blocking: Asynchronous nature of Node.js Flashcards
Synchronous way :
- each statement is basically processed one after another.
- Each line of code basically wait the result(the execution) of the previous line.
Synchronous code is also called “blocking code” because, again :
a certain operation can only be executed after the one before has finished.
💡 This can become a problem specially with slow operations, because each line “block the execution of the rest of the code.”
Asynchronous way
- we offload heavy work to basically be worked on in the background.
- And then, once that work is done, a callback function that we register before is called to handle the result.
- During all the time, the rest of the code still be executing; without being blocked by the heavy task, which is now running in the background.
💡 what this means is that we can effectively defer our reaction into the future in order to make the code non-blocking
why does is actually have to be this way? why we need to use asynchronous way?
-the first thing that we need to understand is the fact that a Node.js process, which is where our application is running, there’s only one single thread.
the thread is just like a set of instructions that is run in the computer’s CPU.So basically, the thread is where our code is actually executed in a machine’s processor.
Remember, Node.js is basically single-threaded and so, for each Node application, there’s only one thread.
That’s just the way Node.js was designed.
-what that means is that all the users accessing your application are all using the same thread.
so, whenever they’re interacting with the application, the code that is run for each user will be executed all in the same thread at the same place in the computer running the application.And that is true no matter if you have five users, like in this diagram, or 5,000 or 5 million.
What’s the problem with blocking code execution in Node.js? PART-1
-what this also means is that when one user locks the single thread with synchronous code, like we just saw before, then all other users will have to wait for that execution to finish.
And that might not be a huge problem if you have like five users, but it definitely will for thousands or even millions of users at the same time.
imagine there’s a user accessing your application and there’s a huge synchronous file read in your code that will take like one second to load. This will mean, of course, that for that one second, all other users will have to wait because the entire execution is blocked for that one second.
if those other users want to do some simple tasks, like logging into your application or just requesting some data, they won’t be able to do so.
They will have to wait until the file is finished reading. Only when that happens they will finally be able to perform the simpler tasks, one after another.
💡 which is obviously a terrible experience for your users. And so, it’s really your job as a developer to avoid these kinds of situations by using asynchronous code.
What’s the problem with blocking code execution in Node.js? PART-2
-for the same situation, we should, of course, use the asynchronous file read function, which instead of blocking the single thread, does the heavy work in the background, where it basically stays until it’s finished reading the data from the file.
–Of course, we then also **register a callback function to be called once the data is available.
And in this scenario, all the other users can then perform their tasks in a single thread, one after another, while the file is still being read in the background.
-Now, once the data is read, our callback function will, of course, get called to be executed in the main single thread in order to process the read data.
recap Asynchronous nature of Node.js
- the creator of Node.js found this model to be the best solution for building highly performant and scalable web applications.
- I/O simply stands for input-output, which is basically stuff like accessing the file system and handling network requests.“I/O” refers primarily to interaction with the system’s disk and network supported bylibuv
That’s an overview of how Node.js handles asynchronous behavior in order to implement the non-blocking I/O operations(model)
why do we actually use callback so many times in Node.js?
Node JS is all built around callbacks in order to implement an asynchronous behavior by calling callbacks as soon as the operation that it’s doing has been finished.
In other programming languages, like PHP, it works very differently because you get, basically, one new thread for each
new user, which is a completely different paradigm and really works completely different.*
💡 it’s important to know that, when we use callbacks in our code, that doesn’t automatically make it asynchronous; passing functions around into other functions is quite common in JavaScript, but of course, again, that doesn’t make them asynchronous automatically.
- It only works this way for some functions in the Node APIs , such as the readFile function and many, many more.