Marcell Ciszek Druzynski

Asynchronous vs Synchronous programming

What is the difference between asynchronous and synchronous programming? When to use asynchronous and synchronous programming?

February 01, 2024

When working as a developer, knowing about synchronous and asynchronous models are important. By the term, it gives us a clue on what each programming model does and the main differences between them. Synchronous tasks happen in order, step by step. It has to finish its task until go to next task. Asynchronous tasks can be executed in random order. How do we use this models and when are they useful, let’s go through examples and build a better mental model about synchronous and asynchronous programming.

Synchronous programming

Synchronous is blocking, what we mean with blocking is that when something is executed synchronously, it has to be finished until it can go to the next line and do the next task. It is known as the single thread model. The tasks that come after the current task has the previous task to finish. A good example is to use the built-in alert method that exists in browsers. The alert modal freezes the whole screen until we close the dialog. Nothing can be run in the background until we closed the alert dialog.

Another example is a bash script where the interpreter goes line by line, the first piece of code has to be executed until the next piece of code will be run, this is typical for scripting languages.

A lifetime example would be that two persons speak through the phone. While one speaks, the other is listening.

Asynchronous programming

The asynchronous model follows the multithreaded model that is common in networking and different communications. The asynchronous model in non-blocking compared to the synchronous model. It does not block further execution while one or more tasks are in progress.

When we use asynchronous programming multiple tasks can be run concurrently without blocking each other, they do not have to wait for each other.

When two persons are texting with each other, they are communicating in an asynchronous way. You do not have to wait for your friend to respond with a message, you can keep sending messages while your friend is typing.

Another example is video tutorials where, they are recorded and we can watch them whenever it suits us the best.

Asynchronous vs Synchronous

There is no model that is better over the other. It all comes down to what you need and what problem you are trying to solve. Asynchronous programming is non-blocking architecture, each task will not be independent on another. Tasks can run at the same time.

Synchronous programming is a blocking architecture, where each task is dependent on the operation before it. Each task needs to know what happened before moving on to the next.

We can identify the differences in this way:

  • Asynchronous programming can run in parallel, and is multithreaded. Synchronous programming is single threaded and can run once task at the time.
  • Asynchronous programming is non-blocking, where it can send multiple requests to a service/server. While synchronous programming will send one request at the time and wait for that request to come back since synchronous is a blocking architecture.
  • Asynchronous increases throughput because multiple operations can run at the same time. Synchronisation is slower and more structured.
  • Synchronies code is easier to read and follow the flow since we can read the code line by line.

We know the two architectures offer different advantages but for different contributors, asynchronous goes more hand in hand with users and synchronous programming for developers.

How we can describe this phrase in a more detailed way Asynchronous programming makes the user experience less laggy, while each request can run concurrently in the background. The user will experience a more snappy web app. When the user makes a request, he/she does not have to experience a blocking/freezing web page since the task runs asynchronously.

On the other hand, synchronous programming is popular by developers. Synchronous code is much easier to write compared to asynchronous code. It has a good support among all programming languages, and by default is the standard programming method.

We know the two architectures offer different advantages but for different contributors, asynchronous programming goes more hand in hand with the users that uses the application while the synchronous model is more pleased by developers.

Common use cases

Where and when we use Asynchronous programming

When somewhere in our program we do not have to follow a fixed sequence of tasks, where operations are not independent of each other and when a blocking model is not a solution to our problem. We want to make the UI responsive, aka Responsive UI. For example when the using a e-commerce application the user should get an identification that the process is going on while the payment is currently processing, it would be a bad experience if that whole site would be freezes and nothing would be shown to the user while the payment is processing. Would the user even know that he/she has paid for the product? Working in React we now have <suspense> as React primitive which allows us to render the UI asynchronous, where we can avoid the waterfall rendering where each component has to wait until the previous components has loaded and are ready. This allows us to write great UI's thanks to the asynchronous model with <Suspense> .

Where and when we use Synchronous programming

While the Asynchronous model is powerful and fulfils its purpose, but it makes things more complicated. The synchronous model is more straight forward and is easier to understand does not require tracking and measuring process flows, compared to the asynchronous model. The obvious reason is that tasks belongs to each other, and the next task can’t run until the current is finished.

When having an application that doing finance calculations, for example, you need to know your base income before you can calculate a proper budget for the month. The first task in this case, calculating the monthly income, has to be finished before the calculator can calculate a budget for the rest of the month.

Summery

Synchronous programming is more strict and comes with some more rules, while asynchronous programming is more adaptive compared to the synchronous model. The Asynchronous model can do multiple things at once to notify the user when the job is done. Synchronous model is a blocking architecture and does one task at the time.

The asynchronous model is more suited for the users were more things will be done faster and giving the users a faster and better UX, user experience.

The synchronous model is more enjoyable by developers and makes the coding easier to understand, common in reactive systems.