Synchronous and Asynchronous Programming

Using one of my favorite clichés, synchronous and asynchronous are 25-dollar terms for 5-cent concepts. Synchronous just means doing things in order, one at a time. Asynchronous means doing multiple tasks at once. This post will take a deeper look at how these terms apply to programming.

Users are expecting faster and faster response times with more and more functionality. A single button click on a website or an app can trigger a multitude of actions behind the scenes. If each of these steps had to happen one after another, the user would have to wait a very long time. Minutes in a world where users are hitting the refresh button after 2 seconds. So, how can you appease your users while delivering all the demanded functionality? Asynchronous programming.

General Meaning of Synchronous and Asynchronous

Synchronous and Asynchronous are general terms used broadly in computer programming. Before understanding how they are used in coding, it's important to know the definitions of the two terms.

Imagine every day you start out by brushing your teeth, taking a shower and making a pot of coffee. And on this particular day, you need to pay your utility bill. Accomplishing these tasks synchronously would mean doing one after the other. Once you start a task, you have to wait until you're finished with it to move on to the next. Your morning might go something like this.

Task Task Time Your Time
Wake up 0 mins 0 mins
Brush your teeth 2 mins 2 mins
Turn on the shower and hold your hand under the water waiting for it to warm up 1 min 1 min
Take a shower and get dressed 20 mins 20 mins
Add coffee and water to the coffee maker 1 min 1 min
Turn on the coffee maker and stare at it until the coffee is ready 5 mins 5 mins
Drink your coffee 10 mins 10 mins
Get in your car and drive to the utility company to drop off your bill 30 mins 30 mins
Drive home from the utility company 30 mins 30 mins
Total 1 hour 39 mins 1 hour 39 mins

1 hour and 39 mins of your time! You can get some of that back by doing some of these tasks asynchronously. To do that, start tasks that don't require your attention to complete, and while they finish take care of another task. Your morning would then look like the following.

Task Task Time Your Time
Before you go to bed add coffee and water to the coffee maker 1 min 1 min
Wake up 0 mins 0 mins
Turn on the shower and let it warm up 1 min 0 min
Coffee maker turns on and brews coffee 5 mins 0 mins
Brush your teeth 2 mins 2 mins
Take a shower and get dressed 20 mins 20 mins
Take a few extra mins to enjoy your coffee 15 mins 15 mins
Take your utility letter out to the mailbox 2 mins 2 mins
Total 46 mins 40 mins

Almost an hour of time saved by allowing tasks to execute that don't require your attention. By using the postal service rather than delivering the mail yourself, you saved an hour of time. Computers can do the same thing.

Synchronous and Asynchronous in Programming

You can apply the same concepts from your morning routine to programming. Applications with a user interface are expected to be fast and responsive. When they make a request to a server for data, the user interface can't freeze up. The request to the server will run asynchronously while the application shows a loading animation to the user. If the request was done synchronously, the user interface would be locked up waiting on a response.

Users don't like to wait on the loading indicator either. The response from the server has to be fast. If the server has a lot of work to do, it will do some of it's tasks asynchronously. This is sometimes called multi-threading. Each thread is a task the server is maintaining. A single thread is responsible for managing asynchronous tasks just like you manage your daily tasks. The main thread has a few ways of keeping track of the tasks.

Polling

To check the status of a task, the main thread can ask another thread for a status. If the task isn't done, the main thread will wait for some interval of time, then check again. This is similar to you checking the temperature of some food you're cooking. You don't have to stare at the thermometer until the food is done. You just have to check on it every few minutes.

Notification

The asynchronous threads can notify the main thread when they are complete. This is similar to pre-heating an oven. You don't have to watch the temperature on the oven while it heats up. You can go do something else and the oven will beep when it has been heated.

Callbacks

The main thread can give an asynchronous thread some additional code to execute when the task is completed. This is useful when the developer doesn't write the code for making network requests to a server. A developer will just provide some code to execute before and after the network requests execute. For example, starting and stopping a loading animation. This would be similar to starting a washing machine on a delay. You would program the washer to do a specific cycle after some given time expires.

Send It and Forget It

Sometimes, there's no need for the main thread to know the asynchronous task has completed. The task is started and it finishes whenever it finishes. This is a lot like sending a bill in mail. You put it in the mail box and trust that it will be delivered.

Event Driven Architecture

Event Driven Architecture is an architecture that has become very popular in recent years. As use cases become more complex, systems and solutions become more fragile. Prior to Event Driven Architecture, a single process might be responsible for handling all aspects of an application. Including logging, reporting, billing, data synchronization, and notifications on top of executing business logic. Event Driven Architecture allows you to distribute these tasks to various systems that communicate via events.

A common example is a purchase from Amazon. When you buy something from Amazon, you get an almost immediate response telling you the purchase was successful. However, at the moment, not all that work is done. After you got your success message an event was fired to send you a confirmation email. Another event is fired to charge your credit card and another to send the order to the warehouse for processing. These independent processes focus on their own responsibility making the whole system more resilient and easier to maintain.

Conclusion

Synchronous and Asynchronous are not terms unique to programming. Synchronous just means doing things in order, one at a time. Asynchronous means doing multiple tasks at once. These concepts are just used to help us humans make sense of the way computers work. Hopefully, after reading this post, you have a better sense of Sync and Async within the context of computing.