Synchronous vs asynchronous programming and their use cases

Concurrent and parallel restaurant orders

Concurrent/Asynchronous restaurants

  1. Get the food
  2. Gossiping with friends
  3. Eating the food
Task 1 = In Progress
Task 2 = In Progress
Task 3 = Waiting for execution (dependent on task 1)
Task 1 = Complete
Task 2 = In Progress (50% complete)
Task 3 = In Progress
  • When you’re eating, you’re not talking. But your friends are talking so the conversation is still going on.
  • When you’re not eating, you’re talking.

Parallel/Synchronous restaurants

Is asynchronous programming better than synchronous programming?

Synchronous vs Asynchronous: Technical differences

Async: The good side

Async: The bad side

Is async faster than sync in general?

Use cases

Synchronous programming

Asynchronous programming

  • I/O intensive web applications: You already know it’s best for handling web requests, database requests etc. If your server has 4 processor cores, you can spawn 4 different processes with one thread each, effectively increasing system throughput up to 4x times.
  • Hybrid web application: In most cases, you might need both CPU and I/O intensive tasks. In that case, you can use a combination of both by creating separate services. Take Uber as an example. Uber might have a separate RouteCalculator service for calculating the best route. This is CPU intensive task so this can be written in Python. On the other hand, for fetching user data or creating and updating users, Uber might have another UserService which is written in Node.js. The final resulting application is the union of both services where they communicate with each other effectively. The most popular web apps you see is written in this way using microservices (or similar) architectures.
  • Causing side-effects: For complex applications, there are a lot of secondary tasks that are not directly related to the end-user. For example, when a user registers on Facebook, it will notify the other services, send and aggregate event logs, generate friend suggestions for the future etc. As these are not directly related to the immediate registration, Facebook can reply with a success response while queuing those tasks asynchronously for later execution.
  • ML applications: Machine learning involves mathematical calculations. If your application works with ML, you can issue an asynchronous request to run ML jobs (written in Python maybe) while you finish up other tasks in your main application.
  • Performance improvements: Using asynchronous operations might drastically improve application performance, or at least, make it look like a better performer. Separating async and sync operations is a crucial task for best performance. For example, when you move folders in Google Drive, the folders are immediately moved from the UI side, making it seem faster. But the actual folder move operation (backend) is done asynchronously which would take slightly more time.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ahmed Sadman Muhib

Ahmed Sadman Muhib

I’m a Software Engineer interested in Web, Cloud and Cross-platform technologies. Favorite things of mine are reading, watching movies and eating burgers