Synchronous and Asynchronous Programming in Go


Go is a programming language with inherent asynchronous programming abilities created and open-sourced by Google. So,why do I care ? here’s why:

Programming models has evolved from sequential (or synchronous or linear) to parallel (or threaded or distributed) and then to asynchronous (or concurrent or non-blocking), each with its own pros and cons. Most of the enterprises use distributed/parallel programming; although it speeds up things quite a bit, it’s a nightmare to manage the thread pools, and the delays induced by blocking especially if you have too many distributed microservices juggling and doing magic in the back to provide a comprehensive response which is the key motivation behind the advent of concurrent/async programming.

To understand more about these three programming models, I highly recommend taking a look at this blog post

Now that ‘why async programming?’ is taken care, let’s go to ‘why Go?’ I was introduced to async programming via Python and Twisted, and it was not bad except for the callback hell, additional libraries you have to install and learn , synchronization and the global interpreter lock which keeps me from fully leveraging multiple cores on the cpu. When I started wondering about these imperfections, situations have conspired to take me to Golang which I think addresses all the aforementioned issues. It’s a compiled language and lets you use all cores, comes with inbuilt async programming abilities, easy to use channels for synchronization as well as rich standard libraries and has an inbuilt production-ready webserver too.

Now, Let’s look at some numbers..

This benchmarking app I wrote has the goal of getting the details of 10 movie titles from an API of a data provider; Here’s the average time took to finish the execution in all the three programming models:

Synchronous: 998.18970995 ms
Asynchronous: 191.4927846 ms
Asynchronous + Parallel (on 8 core CPU): 188.14559590000002 ms

Async is like 81% faster than Synchronous programming.

Async + Parallel is like 3% than Async, and I am sure this will be huge depending on what you are trying to do.

Source Code can be found on my github @ https://github.com/svalleru/gobenchmark