By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
Spawning one goroutine per job is fine until the jobs arrive faster than they finish. Then memory grows with the backlog, the database connection pool saturates, and latency collapses. A worker pool fixes this with a fixed number of consumers reading from one job channel. The channel's buffer plus the worker count is your explicit concurrency budget.
A pipeline is the same idea staged: each stage reads a channel, transforms values and closes its own output. Fan-out means several goroutines read the same input; fan-in means one goroutine merges several outputs into one channel. The composition rule is always the same — the goroutine that creates a channel owns closing it.
Relay's ingest path is exactly this shape: requests push jobs onto a channel, N workers process them, and a collector gathers results. Errors are per job, not per batch: one failing job must not stop the pool. When the producer is done it closes the job channel, workers exit their range loops, and the closer of the results channel waits for the workers before closing.
package main
import (
"fmt"
"slices"
"sync"
)
type job struct {
id int
status int
}
func main() {
jobs := make(chan job)
results := make(chan string, 4)
var wg sync.WaitGroup
for worker := 0; worker < 3; worker++ {
wg.Add(1)
go func() {
defer wg.Done()
for j := range jobs {
if j.status >= 500 {
results <- fmt.Sprintf("job %d failed", j.id)
continue
}
results <- fmt.Sprintf("job %d ok", j.id)
}
}()
}
input := []job{{1, 200}, {2, 500}, {3, 404}, {4, 503}}
go func() {
for _, j := range input {
jobs <- j
}
close(jobs)
}()
go func() {
wg.Wait()
close(results)
}()
collected := make([]string, 0, len(input))
for line := range results {
collected = append(collected, line)
}
slices.Sort(collected)
for _, line := range collected {
fmt.Println(line)
}
}| Pattern | Topology | Use it for |
|---|---|---|
| Worker pool | 1 queue → N workers | Bounded parallel work such as ingestion |
| Pipeline | stage → stage → stage | Transformations with separate failure points |
| Fan-out | 1 input → N goroutines | Independent expensive sub-tasks |
| Fan-in | N channels → 1 channel | Merging results for one consumer |
| Semaphore | buffered channel of tokens | Limiting calls to an external dependency |
| Batch | collect N items or T time | Reducing per-item overhead |
Key takeaways
Bound concurrency with a fixed worker count plus a buffered job channel.
Each pipeline stage owns and closes its output channel.
Fan-out distributes jobs through one channel; fan-in merges results for a single consumer.
One failing job should produce one failed result, not stop the pool.
A closer goroutine that waits for workers and then closes results prevents leaks and deadlocks.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.