Worker Pool Pattern
Bound concurrency.
Worker Pool Pattern is a free Go Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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)
Frequently asked questions
Is the “Worker Pool Pattern” lesson free?
Yes — the full text of “Worker Pool Pattern” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.
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; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
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
- Worker Pool Pattern
- Fan-Out Fan-In
- Pipeline Stages
- Graceful Shutdown