Why asynchronous is a trend?

In the last decade, asynchronous code became a very popular approach, but have you ever wondered why we did such a shift from synchronous code to asynchronous code? Let’s dive deeper into this theme and find out why we did that shift as well as what ar…


This content originally appeared on Level Up Coding - Medium and was authored by Ayzrian

In the last decade, asynchronous code became a very popular approach, but have you ever wondered why we did such a shift from synchronous code to asynchronous code? Let’s dive deeper into this theme and find out why we did that shift as well as what are the pros and cons of async code.

How servers work in a synchronous mode

To understand why asynchronous code became so popular, we need to understand how synchronous servers work. Let’s start with the basics, we have a client (PC) and a server, and the client talks to the server over the internet.

A client (computer) and a server talking over internet.

Each time a server receives a request it creates a new thread. Then all the operations related to the request are executed inside this thread, that works great because you have user context and everything is isolated.

Multiple threads created for multiple requests

Why synchronous server is difficult to scale

Now when we know that usually a thread is created for each request, it’s time to talk about multitasking and concurrency. Each computer has a CPU (Central Processing Unit) and the CPU is responsible for doing calculations and executing the operations that we programmed with our code. CPU has a concrete amount of cores and each core can execute one thread at a time.

Cores executing two different threads

Usually, we have way more threads to execute than cores, so our OS (Operating System) manages our hardware in a way to allocate processor time to all threads in the most efficient ways. I won’t go too deep into this topic, so let’s assume that it just gives each thread an equal amount of processor time. And this is called pseudoparallelism, our OS switches the thread that each CPU executes, so it looks like parallel execution of each thread.

CPU switching different threads to execute each evently

And here comes the problem, switching the thread for the CPU is not a cheap operation, and the more threads you have the more time your CPU spends switching between threads rather than executing the actual code, which leads to a laggy response due to process time starvation.

How asynchronous server utilizes resources

Finally, it is time to talk about asynchronous code, the idea of an asynchronous code is to have the main thread (a thread where your business logic is executed) that responds to clients while offloading most operations like I/O (Input/Output, e.g. reading a file) and CPU-intensive operations to the thread pool (also called as a worker pool). The pool should not have more threads than the number of CPU cores on your server.

Multiple requests handled by a main thread and async engine using pool of threads

Each time you need to do something off the main thread you are “scheduling” a task, by putting it in a queue, and then when you have an idle thread from a pool, it will work on your task. In that way, we are solving the problem of scaling your server to some extent because now your CPU spends more time working on the requests, which noticeably increases the number of concurrent requests your single server can handle.

Drawbacks of asynchronous server

The main drawback of the async code is that you have only one “main” thread where all your business logic is executed most of the time, the rest is usually handled by your async engine.

Since your business logic is executed in a single thread if you have a piece of code that runs too long (usually we call such a thing a blocker) your server will become slow and will hardly respond to other clients, because it will be busy handling only one of the multiple requests.

Summary

Asynchronous code is a tool and like any other tool you need to know when to use it, it is not a silver bullet that can solve all your problems, but rather a double-edged sword that can hurt you as well if used badly.

In the next article, we will talk about overcoming the drawbacks of the asynchronous server, on the example of Node.js.


Why asynchronous is a trend? was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Ayzrian


Print Share Comment Cite Upload Translate Updates
APA

Ayzrian | Sciencx (2022-07-25T13:43:30+00:00) Why asynchronous is a trend?. Retrieved from https://www.scien.cx/2022/07/25/why-asynchronous-is-a-trend/

MLA
" » Why asynchronous is a trend?." Ayzrian | Sciencx - Monday July 25, 2022, https://www.scien.cx/2022/07/25/why-asynchronous-is-a-trend/
HARVARD
Ayzrian | Sciencx Monday July 25, 2022 » Why asynchronous is a trend?., viewed ,<https://www.scien.cx/2022/07/25/why-asynchronous-is-a-trend/>
VANCOUVER
Ayzrian | Sciencx - » Why asynchronous is a trend?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/07/25/why-asynchronous-is-a-trend/
CHICAGO
" » Why asynchronous is a trend?." Ayzrian | Sciencx - Accessed . https://www.scien.cx/2022/07/25/why-asynchronous-is-a-trend/
IEEE
" » Why asynchronous is a trend?." Ayzrian | Sciencx [Online]. Available: https://www.scien.cx/2022/07/25/why-asynchronous-is-a-trend/. [Accessed: ]
rf:citation
» Why asynchronous is a trend? | Ayzrian | Sciencx | https://www.scien.cx/2022/07/25/why-asynchronous-is-a-trend/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.