More

    Want to know how node.js is going to work?

    You know that Node.Js applications are highly scalable and this is because of the non-blocking or asynchronous nature of node.

    What is Non-blocking or asynchronous architecture?

    What do you mean by asynchronous? Let me give you a metaphor. 

    You go to a restaurant, the waiter comes to your table, takes your order and gives it to the kitchen, then they move on to serve another table. 

    While the chef is preparing your meal, the same person can serve many different tables. 

    They don’t have to wait for the chef to cook one meal before they serve another table. 

    This is what we call non-blocking or asynchronous architecture and this is how node applications work. 

     

    What is blocking or synchronous architecture?

    The waiter is like a thread allocated to handle a request. So a single thread is used to handle multiple requests in contrast to non-blocking or asynchronous architecture. 

    We have blocking or synchronous architecture, let’s see how that works. 

    So back to our restaurant example, imagine you go to another restaurant and in this restaurant, a waiter is allocated to you. 

    They take your order and give it to the kitchen, now they are sitting in the kitchen waiting for the chef to prepare your meal. 

    At this time they are not doing anything else they’re just waiting, they’re not going to take an order from another table until your meal is ready. 

    This is what we call blocking or synchronous architecture and that’s how applications built with frameworks like a speed net or rails work out of the box. 

    So when we receive a request on the server, a thread is allocated to handle that request as part of handling that request. 

    It is likely that we’re going to query a database and as you know sometimes it may take a little while until the result is ready. 

    When the database is executing the query that the thread is sitting there waiting, it can’t be used to serve another client. 

    So we need a new thread to serve another client. imagine what would happen if we have a large number of concurrent clients at some point we’re going to run out of threads to serve these clients. 

    In that situation, new clients have to wait until three threads are available or if we don’t want them to wait, we need to add more hardware. 

     

    Is it possible to use asynchronous architecture?

    So with this kind of architecture, we are not utilizing our resources efficiently. This is the problem with blocking or synchronous architecture. 

    And as I explained that’s how applications built with frameworks like asp.net worked by default. Of course in asp.net, it is possible to use asynchronous architecture. 

    But you will have to do extra work for now in contrast as the applications are asynchronous by default. So you don’t have to do anything extra in the node. 

    We have a single thread to handle all requests, the linear request arrives that a single thread is used to handle that request. 

    If you need to query a database our thread doesn’t have to wait for the database to return the data while the database is executing our query. 

    That thread will be used to serve another client when the database prepares the result, it puts a message in what we call an event queue. 

    Node is continuously monitoring this queue in the background. When it finds an event in this queue it will take it out and process it. 

    This kind of architecture makes Nodejs development ideal for building applications that include a lot of disks or network access. 

    We can serve more clients without the need to throw in more hardware and that’s why no applications are highly scalable in contrast. 

    Nodejs should not be used for CPU intensive applications like video encoding or an image manipulation service. 

    In this kind of application, we have a lot of calculations that should be done by CPU and few operations that touch the file system or the network. 

    Since no the applications are single-threaded when performing the calculations to serve one client, other clients have to wait. And that’s why nodejs should not be used or CPU intensive applications. 

    It should only be used for building data-intensive and real-time applications.

    Recent Articles

    React Ecommerce Templates

      React is the most popular library for the development of e-commerce applications. React eCommerce templates for building eCommerce apps are a combination of several...

    What is React Yarn? How to Install It?

    Introduction to React Yarn The technical community shares hundreds and thousands of pieces of code every day in the community of JavaScript. As a result,...

    React Editor: Features, Installation and Limitations

    Why do we need the react editor?  There is a large range of code-based editors like CodeMirror, Monaco, Ace, etc., that provide us with the...

    React Babel: Everything You Need to Know About It

    Introduction About React Babel: React Babel is a very well known and well-implemented open-source transcompiler of JavaScript. We use it mainly to convert the ECMA...

    React Websocket: Usage, Installation and much more

    Introduction About React Websocket:  If you want to create a long-running connection between a client and a server then use React Websocket. Let's take an...

    Related Stories

    Leave A Reply

    Please enter your comment!
    Please enter your name here

    Stay on op - Ge the daily news in your inbox