Go Concurrency Series: Concurrency Patterns

Let’s continue being a little more hands-on in our Go Concurrency Series! In this post, we’ll look into the implementation side and cover the common concurrency patterns that can be utilised in Golang.

Concurrency in Go is based on the ‘CSP’ (Communicating Sequential Processes) model, which emphasizes communication between independent processes (goroutines) by sending events(data) through channels.

So, keeping that in mind, let’s see some common concurrency patterns we can use in Go.

Worker Pool

In the Worker Pool Pattern, a fixed number of workers (goroutines) are created to process jobs. These workers fetch tasks from a queue(channel) and process them concurrently. This pattern is especially useful for managing resource utilization, as it prevents spawning an excessive number of goroutines, which could lead to higher memory usage and reduced performance.

Remember that since costs of creating goroutines is relatively cheaper, you might not always see an advantage of using worker pool, so use benchmarking to see if it works for you!

Advantages of the Worker Pool Pattern

  1. Controlled Concurrency: Offers control over how many tasks run concurrently.

--

--

Pratik Pandey - https://pratikpandey.substack.com

Senior Engineer with experience in designing and architecting large scale distributed systems.