What would be the reason for using asynchronous programming on a web server?

  softwareengineering

4

I have a python/php background and just about to start work on a NodeJS project which is why I have this question. I have worked a bit on frontend JS but this is the first time I will be using JS for some backend work…I fully understand and appreciate the asynchronous nature of JS when it comes to frontend development. But I will be writing a bunch of APIs in NodeJS for the backend. Being new to NodeJS, I cannot help but think: What would be the reason to make asynchronous calls in my backend code? Are there specific scenarios where asynchronous calls will be better on the backend server? The API has to eventually return data in JSON format to the frontend caller….

4

6

Asynchronous calls are better when your application is io bound opposed to being cpu bound (in case of web applications, almost always). Talking to the database, receiving and sending packets on the network, reading files, etc. is slower than the CPU. Therefore while the program is waiting for the result of an async io call, it can work on processing other requests, resulting in better utilization of resources and responsivenes

It’s also important to mention that OS threads are expensive so it’s better if one thread can process multiple requests without being blocked. Imagine if fronted js would be synchronous and all tabs would run on their own OS threads: you could still use other tabs while js is blocking on one tab, but it would be a worse overall experience

LEAVE A COMMENT