Skip to content
GDP for Noobs
☰ All patterns
ConcurrencyIntermediate

Worker Pool

A fixed team of goroutines chewing through a queue of jobs.

01

Explain it like I'm new

Real-life analogy

Checkout lanes at a supermarket

There are 3 cashiers and a queue of customers. Each free cashier takes the next customer. More customers? The queue gets longer, but you never hire 1,000 cashiers.

😩 The problem

You have 10,000 images to resize. One goroutine per image could overwhelm memory, CPU or a downstream API.

💡 The pattern

Start N workers that range over a jobs channel and send to a results channel. Close jobs when done, and wait with a WaitGroup.

02

Play with it

An interactive model of the pattern. Click around, nothing can break.

jobs channel (0)

worker 1idle
worker 2idle
worker 3idle

results (0)

💡 Try 1 worker vs 6 workers. More workers = faster, up to a point. In real life the limit is CPU, memory or the API you're calling.

03

Walk through the code

Click a step to highlight its lines. This is a complete program you can copy and run.

main.go
1package main
2
3import (
4 "fmt"
5 "sort"
6 "sync"
7 "time"
8)
9
10type Job struct{ ID, N int }
11type Result struct{ JobID, Value int }
12
13// worker pulls jobs from the channel until it's closed.
14func worker(jobs <-chan Job, results chan<- Result, wg *sync.WaitGroup) {
15 defer wg.Done()
16 for j := range jobs {
17 time.Sleep(20 * time.Millisecond) // pretend this is slow work
18 results <- Result{JobID: j.ID, Value: j.N * j.N}
19 }
20}
21
22func main() {
23 const numJobs, numWorkers = 9, 3
24 jobs := make(chan Job, numJobs)
25 results := make(chan Result, numJobs)
26
27 // 1. Start a FIXED number of workers.
28 var wg sync.WaitGroup
29 for w := 1; w <= numWorkers; w++ {
30 wg.Add(1)
31 go worker(jobs, results, &wg)
32 }
33
34 start := time.Now()
35 // 2. Send all the jobs, then close the channel so workers know to stop.
36 for i := 1; i <= numJobs; i++ {
37 jobs <- Job{ID: i, N: i}
38 }
39 close(jobs)
40
41 // 3. Close results once every worker is done, so the range below ends.
42 go func() { wg.Wait(); close(results) }()
43
44 var all []Result
45 for r := range results {
46 all = append(all, r)
47 }
48 elapsed := time.Since(start)
49
50 // Results arrive in random order, so sort them for display.
51 sort.Slice(all, func(i, j int) bool { return all[i].JobID < all[j].JobID })
52 for _, r := range all {
53 fmt.Printf("job %d -> %d\n", r.JobID, r.Value)
54 }
55 fmt.Printf("%d jobs, %d workers. Faster than doing them one by one (180ms)? %v\n",
56 numJobs, numWorkers, elapsed < 180*time.Millisecond)
57}
terminal
Press ▶ Run to execute this program.

This output was captured by running the program with Go 1.24. Copy it into your editor or the Go Playground and try changing things.

04

When should I use it?

✅ Reach for it when…

  • •Batch processing, crawling, resizing, sending emails, anything with many similar jobs.

🚫 Skip it when…

  • •There are only a few jobs. Just start a goroutine per job.

🐹 Go tip

golang.org/x/sync/errgroup with g.SetLimit(n) gives you a worker pool with error handling in a few lines.

🌍 You've already seen it in

Background job runners, image pipelines, and database migration tools that process rows in batches.

05

Quick check

One question. No pressure.

Why do we close(jobs) after sending all jobs?

Related: Semaphore, Fan-out / Fan-in, Pipeline