Skip to main content

Command Palette

Search for a command to run...

Blocking vs Non-Blocking Code in Node.js

Sync code vs Async code ?

Updated
6 min read
Blocking vs Non-Blocking Code in Node.js

What blocking code means .

  • Everyone know JavaScript run or execute code line by line , means if you write 10 line code in one file , execution start from line number one and move to 10 line number , this process till run when all code should not be executed.

  • But if any one line create error then code execution terminated till when that error will not resolve . That's why this process called Blocking code , blocked line of code will not execute and not move ahead.

  • This process is similar in node.js , code execute line by line , this is a huge problem , let's discuss him next section , because see this problem you first need to understand little bit about first non blocking code , then you will understand both difference between . Hope you got my point.

What non-blocking code means .

Before discuss about what is non blocking code , let discuss first why we need them , what's the problem in blocking code , then you will understand it properly , so let's start .

  • As you know blocking code execute code line by line , so what the problem here , we are building our application using node.js, let's assume in our file having total five function, each work independently , in backend you know everything depend on database so , may be each function call database for data and process it, let's assume first 2 function do database call , so they take time , firstly first function execute then second because it is a blocking code , assume database call take less time , take one minute not too much big problem, but it is a problem , now our first two function execute , now turn third function assume in function three we write logic to upload images and videos means handling files , assume user upload 500 mb video file, very big number , server take too much time to upload them and then upload into database it's take too much time , till file not upload our code in blocking state, fourth and fifth function are ideal waiting for execution , because function processing for file upload and it's take time .

  • Here you can see what's the problem, code execution stop and blocked at one function. so what we do to solve this problem , to solve this problem came into picture non blocking code .

One of most important use full come node.js , non blocking code , means it process the request concurrently , not block any line code , execute in parallel and run independently. Means now user try to upload large file our rest of code will not be blocked , other function executing in code sequentially . In node.js most of the time we write code non blocking , because every function try to request database , and as we know database always in another continent so it's take time , so that's why we make it non blocking , to make it non blocking, we know to make any code non blocking using asynchronous code . hope you will understand it .

Why blocking slows servers .

  • why it is become slows because it block everything until this particular currently task not finishes. other upcoming task no executed till current task not finished , now take my above problem statement on blocking code and little bit think about it what's more problem come when you write a code , so always write non blocking code .

  • I have now too many example , but it's your job little bit think about it ,and figure out what's the problem can create with blocking code , never write blocking code in database call, sever call and in file uploading, because it create a huge problem and server will crash.

  • Now think about if server crash , our website goes down, now user still wait for website reupdate and open while after some time and then use them , answer is not . So this is problem and it is slows servers.

Async operations in Node.js .

  • Whenever we write asynchronous code in Node.js, it's primarily because Node.js is non-blocking. This means that while a task is running, it doesn't prevent other parts of the code from running.

  • For example, let's say we're building an authentication system. First, we register a new user by saving their details in a database. Later, when the user tries to log in, we check their details again. If the details match, we allow the user to log in and continue. If not, we display an error.

  • In this process, every request needs to check the user's data against the database. Since the database may be on another server or even on another continent, each query takes some time to respond.

  • If we were using blocking (synchronous) code, the system would wait (stay idle) until the database responded. During that time, no other requests could be processed. This is not a good approach because if one user is registering, other users trying to access the system would be blocked.

Real-world examples (file read, DB calls) .

In real-world applications, we often deal with operations like reading files or making database calls. These tasks usually take time because they depend on external systems like the file system or a database server.

  1. For example, if we read a file from the system, Node.js needs to access the disk, which is slower compared to memory operations. Similarly, when we make a database call, the request goes to another server and waits for a response.

  2. If we use blocking (synchronous) code for these operations, the program will wait until the file is read or the database responds. During this time, no other code can run, and the system becomes slow, especially when handling multiple users.

  3. To solve this, we use asynchronous code. It allows Node.js to start these operations and continue running other tasks without waiting. Once the file is read or the database response is ready, a callback, promise, or async/await handles the result.

Because of this, Node.js can efficiently handle many real-world tasks like file reading and database operations without blocking other requests.

💡
So with my above explanation i hope you understand what i learn and what's my thought process on Blocking vs Non-Blocking Code in Node.js .