DEV Community

Cover image for golang multithreading
Lakshan Dissanayake
Lakshan Dissanayake

Posted on • Edited on

golang multithreading

In my workplace, client had reported a performance bottle-neck and as a resolution, I was assigned to identify the bottleneck and make necessary changes to make it more efficient. After some time investing the logs, I had found the root cause and it was due to the repetitive occurrence of some time-consuming activity, which ultimately impacted on the response time.

After some workaround, I decided to execute the time consuming operations asynchronously and wait for the execution of asynchronous work and aggregate the final results. Here I found 3 ways to do this.

go-routines are light weight wrappers for threads, they will do the work using a pool of threads internally.

The Bad way

TLDR: Execute jobs synchronously.


This is a bad way of doing asynchronous work since it blocks main thread and execute jobs synchronously.

The Normal way

TLDR: Execute jobs asynchronously, but number of parallel jobs are uncontrolled


In here, sync.WaitGroup is controlling the execution of background thread and it waits till the atomic counter of waitGroup is reduced to zero, which indicates all the routines have been executed. The problem with this approach is, if we get a large number of jobs in job array, there will be that number of go-routines which consumes resources.

The Better Way

TLDR: Execute jobs asynchronously and number of parallel jobs are controlled


This approach will do the work in a controlled parallel manner. So at any given time, it will use max number of go-routines to execute the jobs parallelly and will consumes less resources.

Top comments (0)