GoBackendConcurrency
Concurrency Patterns in Go
Transmission ByIrham Tri
Chronicle TimestampFebruary 10, 2024
Why Go?
Go's concurrency model based on CSP (Communicating Sequential Processes) is a game changer for backend services. Unlike Node.js event loops, Go allows us to spawn lightweight threads (goroutines) that are managed by the Go runtime.
The Fan-Out/Fan-In Pattern
One of the most useful patterns for processing large datasets.
func worker(id int, jobs <-chan int, results chan<- int) {
for j := range jobs {
results <- j * 2
}
}
func main() {
jobs := make(chan int, 100)
results := make(chan int, 100)
for w := 1; w <= 3; w++ {
go worker(w, jobs, results)
}
}
By fanning out the work to multiple workers and fanning the results back in, we reduced our image processing pipeline latency by 80%.