What is Asynchronous Programming?

Rajat Sikder
5 min readJun 28, 2018

--

taken from all mighty internet

Recently we are writing a lot of asynchronous applications at work. This made me wonder if I am applying asynchronous concepts for the right use cases. Therefore, I decided to spend some time learning the basics. I wanted to share this knowledge with you in the hopes that you might find it useful.

In order to understand asynchronous concepts better, we must take a look at synchronous programming. Lets look at an example:

  1. Lets assume we are building a restaurant that will make pizzas for us
  2. Let us assume that the restaurant has 2 available employees to make pizza (Chef 1 and Chef 2)
  3. Each employee will need to complete the following tasks to make a single delicious pizza

Task 1. Make a soft bread dough for the pizza

Task 2. Apply base sauce with yummy cheese and one topping on top of the freshly made bread dough (I prefer sun dried tomato)

Task 3. Bake the pizza in the oven until done (requires 15 mins)

Task 4. serve it to the customer.

How will Synchronous work look for the above restaurant receiving pizza orders ? Lets look at 3 different situations:

Situation 1 :
1. Only 1 pizza request comes in at Time 0

Situation 2 :
1. Pizza request 1 comes in at Time 0 and
2. Pizza request 2 comes in at Time 1

Note : From the above situation 1 and situation 2, we can conclude the following

  1. One chef is assigned for the lifetime of the pizza, starting with task 1 and ending with task 4.
  2. A new task can start only when the previous task has completed.
  3. Only after a chef completes task 4, he/she is available to take another pizza request.
  4. During task 3, a chef has to wait 15 mins until the pizza is baked. During this IDLE period the chef can not take any new pizza request. This period the chef’s time is wasted waiting for task 3 to be complete

Situation 3:
1. Pizza request 1 comes at time 0
2. Pizza request 2 comes at time 1
3. Pizza request 3 comes at time 2

Note: From above situation 3, we can conclude the following:

  1. When Pizza Request 3 comes at Time 2, there is no chef available to take the request
  2. The pizza order 3 has to wait from Time 2 to Time 4, when Chef 1 becomes available again.
  3. A lot of time wasted waiting to take new order although chef 1 was sitting IDLE at Time 2. This is very inefficient. (Question : What if the chef 1 could take the pizza request 3 during that IDLE time 2 ?)
  4. Now imagine a situation where
    a. 5 pizza requests come at Time 0,
    b. 3 pizza requests come at Time 2,
    c. 7 pizza requests come at Time 3 and so on ……..
  5. In such a situation, there will be a lot of waiting time, and backlog of pizza request will keep growing creating a bottleneck. Thus customers have to wait a long period before getting their pizza.
  6. As a result the shop wont be able to SCALE.
  7. Why not increase the number of chefs? Hiring new chefs are costly and adding more chef will require a restaurant with larger space.

So what can we do to make the process as efficient as possible? Is there a way to utilize all the idle time wasted?

The answer is YES!. We can apply asynchronous concept here. Lets see what happens if we do asynchronous work for situation 3:
1. Pizza request 1 comes at time 0
2. Pizza request 2 comes at time 1
3. Pizza request 3 comes at time 2

Note: In asynchronous situation 3, we can conclude the following

  1. When new pizza 3 request comes at Time 2, chef 1 who is idle takes the order and starts processing. Therefore the pizza request does not have to wait
  2. Once Chef 1 take the new order, he/she goes back to pizza order 1 in order to complete the remaining task.
  3. Meanwhile, Chef 2 who is idle at time 3, starts preparing pizza order 3 and then goes back to pizza 2 order at Time 4 to complete remaining tasks
  4. At time 4, chef 1 is free and takes over pizza order 3.
  5. From above its clear that pizza order 3 was completed earlier (time 6) for asynchronous. Whereas for synchronous it finished at time 8. Thus this process was more efficient and faster.
  6. Does that mean, this process will be able to handle excessive pizza request? The answer is No! But it at least makes the best use of its available resources. In order to deal with the excess pizza request, the owner has to open up new branches and distribute pizza request equally to all branches. As a result reducing load on any single branch.

Thanks for your time. I will dig deeper into asynchronous programming (.NET) in my next blog. Please leave some comments and let me know if you found it useful.

--

--