Go Academy · Lesson

Worker Pool Pattern

Bound concurrency.

Lesson 1 of 413 steps

Worker Pool Pattern is a free Go Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, and your progress syncs across the web and the CoddyKit app. The Go Academy course includes 4 lessons in total.

What Is a Worker Pool

A worker pool launches a fixed number of goroutines that all pull jobs from a shared channel. Instead of spawning one goroutine per task, you spawn N workers and feed them work.

  • Bounds the number of concurrent operations
  • Reuses goroutines instead of creating thousands
  • Protects shared resources like databases or APIs

Why Bound Concurrency

Unbounded goroutines can exhaust memory, file descriptors, or overwhelm a downstream service. A pool gives you a knob to control how much work runs at once.

If you have 10000 URLs to fetch, you do not want 10000 simultaneous HTTP requests. A pool of 20 workers keeps it sane.

The jobs Channel

Jobs flow into a jobs channel. Each worker reads from it in a for range loop. When the channel closes, the loop ends and the worker exits.

jobs := make(chan int, 100)
for j := 1; j <= 5; j++ {
    jobs <- j
}
close(jobs)

A Single Worker

A worker is just a function that ranges over the jobs channel. It processes each job and writes the result to a results channel.

func worker(id int, jobs <-chan int, results chan<- int) {
    for j := range jobs {
        results <- j * 2
    }
}

Starting Multiple Workers

Launch the pool with a simple loop. Each iteration starts one goroutine running the same worker function. They all share the same jobs and results channels.

for w := 1; w <= 3; w++ {
    go worker(w, jobs, results)
}

Directional Channel Types

Notice <-chan int (receive-only) and chan<- int (send-only) in the worker signature. These directional types let the compiler stop a worker from accidentally sending into jobs or reading from results.

Collecting Results

After feeding all jobs and closing the channel, the main goroutine reads exactly as many results as there were jobs. This synchronizes the program so it does not exit early.

sum := 0
for a := 1; a <= 5; a++ {
    sum += <-results
}
fmt.Println("total:", sum)

A Complete Runnable Pool

Here is the full pattern in one program: three workers double five numbers. Run it and observe the total.

package main

import "fmt"

func worker(id int, jobs <-chan int, results chan<- int) {
    for j := range jobs {
        results <- j * 2
    }
}

func main() {
    jobs := make(chan int, 5)
    results := make(chan int, 5)
    for w := 1; w <= 3; w++ {
        go worker(w, jobs, results)
    }
    for j := 1; j <= 5; j++ {
        jobs <- j
    }
    close(jobs)
    total := 0
    for a := 1; a <= 5; a++ {
        total += <-results
    }
    fmt.Println("total:", total)
}

Buffered vs Unbuffered

A buffered jobs channel lets the producer queue work without blocking until the buffer fills. An unbuffered channel forces a handoff: the send blocks until a worker is ready. Buffering smooths bursty producers.

Choosing the Pool Size

For CPU-bound work, size the pool near runtime.NumCPU(). For IO-bound work (network, disk), you can go much higher since workers spend time waiting, not computing.

package main

import (
    "fmt"
    "runtime"
)

func main() {
    fmt.Println("CPUs:", runtime.NumCPU())
}

Avoiding Goroutine Leaks

If you forget to close(jobs), workers block forever on an empty channel and leak. Always close the jobs channel once all work is submitted so the for range loops terminate.

Quick Check

Test your understanding of worker pools.

Recap

You learned the worker pool pattern:

  • Spawn a fixed number of workers reading from a shared jobs channel
  • Use directional channel types for safety
  • Close the jobs channel to terminate workers
  • Collect results to synchronize completion
  • Size the pool by workload type (CPU vs IO)
Free to start

Learn Go with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
51
Lessons
203

Frequently Asked Questions

Is the “Worker Pool Pattern” lesson free?

Yes — any 3 lessons of the Go Academy learning path, including “Worker Pool Pattern”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Go Academy course includes 4 lessons in total.

What will I learn in “Worker Pool Pattern”?

Bound concurrency. You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Go Academy?

No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.

How long does the “Worker Pool Pattern” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Go Academy lesson?

Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Worker Pool Pattern
  2. Fan-Out Fan-In
  3. Pipeline Stages
  4. Graceful Shutdown
← Back to Go Academy