A simple story to explain asynchronous programming

Oliver Bisseker
Oct 9 · 3 min read

I’m will explain asynchronous programming using a simple ‘real life’ example. I hope this will help shed some light on a subject that is often made to seem more complex than it is.

Asynchronous programming is not the same as parallel or multi-threaded programming.

What is Asynchronous programming?

A computer is a set of resources that can be used to run programs. When you run a program you use of those resources, but so does every other program running on the computer. There aren’t enough resources for every program so they have to share. That means your program will wait for other programs to finish with a resource before making use of it and they’ll wait for your program.

Asynchronous programming is a form of programming that tells a computer it can free up resources your program is using while waiting for “long running” tasks to complete. For example, you make a GET request to a 3rd party API. You don’t know how long it will take for a response but imagine it’s 2 minutes.

Using synchronous programming the resources being used would wait for the response not doing anything. Now imagine the computer is running 100 synchronous programs all making the same call, that’s over 3 hours of wasted resource time.

Real life example

Imagine you have a list of tasks to complete:
- Add up 100 numbers.
- Make yourself some toast.
- Wash up last night’s dishes.

You’re sat at your desk finishing some accounting; you have 100 numbers to add up that are printed on 4 sheets of paper. You start adding up the numbers, but find that the 3rd page is missing. You remember what number you got up to and go to make some toast.

While your bread is in the toaster you wash up last night’s dishes. The mail arrives, so you collect that and it contains the 3rd page of numbers. Your toast pops out of the toaster; you go put the page of numbers on your desk, and then go butter your toast. Now go add those numbers up, and don’t forget to eat your toast at some point.

Asynchronous programming isn’t about doing multiple things at exactly the same time. It’s about filling as much of the processing time as possible constructively by always working on a task rather than waiting for something. If, in the above example, you’d waited for the 3rd page of numbers, you wouldn’t have completed the other tasks given to you within the same time frame.

Benefits

The benefits of asynchronous programming aren’t always seen by your program but by the computer as a whole. Your program might require a response from an API before it can move forwards, meaning you’re waiting for (lets say) 2 minutes, no matter what. By freeing up resources for that time other programs can make use of them. Now imagine the other programs also use asynchronous programming. The computer would have a lot more free resources meaning that your program can access resources easier as well.

New frameworks are making use of asynchronous programming to an even greater extent. For example .Net Core will process multiple asynchronous calls in parallel within the same program if there are enough resources available. So when using asynchronous programming in .Net Core you can do multiple things at the same time. However, this brings it’s own set of risks that you have to mitigate through good coding practices.

Thank you for reading through this article, I hope it has helped to demystify asynchronous programming.

Join the Capgemini Microsoft team, open roles here.

Capgemini Microsoft team

To share best practices, knowledge and experiences of the Capgemini Microsoft team

Oliver Bisseker

Written by

Capgemini Microsoft team

To share best practices, knowledge and experiences of the Capgemini Microsoft team

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade