Go Worker Pool Pattern Explained Simply

Updated 4 min read
Go Worker Pool featured image: cards for jobs, workers and results

A Go worker pool runs many jobs with a fixed number of goroutines. So your program stays fast without overloading the machine. In this post, you will build a pool with channels and a WaitGroup. Then you will collect results and limit work with a simple semaphore. Each step comes with code that you can run yourself.

This lesson combines three earlier topics. First, review goroutines in Go for beginners and channels in Go with examples. We also use sync.WaitGroup from the Go sync package.

What Is a Go Worker Pool?

Imagine a restaurant kitchen with three cooks and a rail of order tickets. New tickets go on the rail, and any free cook grabs the next one. The kitchen never hires a new cook for every order. Instead, three cooks share the work.

A Go worker pool works the same way. A jobs channel is the ticket rail, the workers are the cooks, and a results channel collects the finished dishes. The diagram below shows the full flow.

Go Worker Pool diagram showing nine jobs shared by three workers that send results to one channel

Jobs flow from one channel to three workers, and each worker sends its result into a shared results channel.

Why Use a Go Worker Pool?

Goroutines are cheap, but the work inside them is not. For example, starting 10,000 goroutines that all call a database at once can exhaust its connections. A Go worker pool caps how much work runs at the same time, so the system stays stable.

Building a Go Worker Pool Step by Step

Step 1: The Worker

type Result struct {
	Job    int
	Square int
	Worker int
}

func worker(id int, jobs <-chan int, results chan<- Result, wg *sync.WaitGroup) {
	defer wg.Done()
	for j := range jobs {
		time.Sleep(100 * time.Millisecond) // pretend this is slow work
		results <- Result{Job: j, Square: j * j, Worker: id}
	}
}

Each worker loops over the jobs channel until it closes. The arrows in <-chan and chan<- make the channels receive-only and send-only, so the compiler stops mistakes.

Step 2: Start Workers and Send Jobs

jobs := make(chan int, 10)
results := make(chan Result, 10)

var wg sync.WaitGroup
for w := 1; w <= 3; w++ {
	wg.Add(1)
	go worker(w, jobs, results, &wg)
}

for j := 1; j <= 9; j++ {
	jobs <- j
}
close(jobs)

Closing jobs tells the workers that no more work is coming. Without it, their range loops would wait forever.

Step 3: Collect the Results

go func() {
	wg.Wait()
	close(results)
}()

sum, count := 0, 0
for r := range results {
	sum += r.Square
	count++
}
fmt.Println("results:", count, "sum of squares:", sum)
// results: 9 sum of squares: 285
// took about 300ms

A small goroutine waits for every worker, then closes results. As a result, the final range loop ends on its own. Nine jobs of 100 ms each finish in about 300 ms, because three workers share them.

A Simpler Limit: The Semaphore Pattern

sem := make(chan struct{}, 2)
for i := 0; i < 6; i++ {
	wg.Add(1)
	go func(n int) {
		defer wg.Done()
		sem <- struct{}{}        // take a slot
		defer func() { <-sem }() // give it back
		// do the work here
	}(i)
}
wg.Wait()
// max running at once: 2

Sometimes you just want “at most N at a time”. In that case, a buffered channel works as a semaphore. Each goroutine takes a slot before working and returns it afterwards. So only two run together, even though six were started.

Choosing the Go Worker Pool Size

There is no magic number. For CPU-heavy work, start with runtime.NumCPU(). For network or database calls, however, the limit usually comes from the other system, such as a database connection limit. So measure first, then adjust the number of workers.

Common Mistakes with a Go Worker Pool

Problem you seeWhy it happensHow to fix it
fatal error: all goroutines are asleep – deadlock!The code never closed jobs or results.Close jobs after sending, close results after wg.Wait().
Results are missingmain exited before workers finished.Wait on the WaitGroup or drain results.
WARNING: DATA RACEWorkers update a shared variable.Send results through a channel or use a mutex.

Run your program with go run -race . to catch the last problem early. Worker pools also appear in many system design interviews, so practise with my Go interview questions and answers.

Official Resources

These pages come straight from the Go team, so they stay accurate and up to date.

Quick Recap

  • A Go worker pool runs many jobs with a fixed number of goroutines.
  • Workers read from a jobs channel and write to a results channel.
  • Close jobs when sending is done, and close results after wg.Wait().
  • A buffered channel can act as a simple semaphore.
  • Size the pool by measuring, not guessing.

What’s Next?

You can now process jobs in parallel safely. Next, we will work with dates, durations and timers using the time package.

Next lesson: Go Time Package: Dates, Durations and Timers