Go Concurrency: The Right Way to Parallelize API Calls (Context, Errgroup, and Boundary Protection)
Go backend teams that fan out to multiple downstreams risk exactly the failures this article describes: partial reads, runaway goroutines, and dependency overload. The layered approach gives a concrete, testable pattern for keeping aggregation interfaces fast and stable under load.
A typical aggregation interface needs to call logs, metrics, and alerts downstream in parallel. A naive implementation with `go func()` and `time.Sleep` works at first, but production soon reveals missing data, goroutine accumulation, ignored cancellations, and overwhelmed dependencies. The core problem isn't the number of goroutines; it's the absence of a defined lifecycle, error propagation, panic recovery, and resource ceilings.
Correct concurrency control starts with `context.WithTimeout` for a unified deadline, then `errgroup` for task management and error aggregation. But `errgroup` does not recover panics, so a `safeGo` wrapper is needed to convert panics into errors. For partial success, `errgroup.WithContext` must be avoided because it cancels all tasks on the first error; `sync.WaitGroup` lets each downstream run to completion. Callers must also check a `Partial` flag, since a nil error doesn't mean complete data.
Beyond a single request, service-level semaphores limit total downstream concurrency, while entry-level in-flight limiters reject excess requests before goroutines multiply. Connection pool settings and gateway-level limits complete the picture. The article lays out four layers: entry, request, service, and cluster. Each handles a different bottleneck: fast rejection, per-request task limits, per-instance downstream concurrency, and cross-instance traffic control.
The article exposes a common blind spot: developers treat concurrency as a per-request concern and ignore cross-request and entry-level accumulation, which is why goroutine pile-ups and downstream outages keep recurring.
The distinction between all-or-nothing and partial success is a design decision that must be explicit; reusing errgroup.WithContext for partial success silently converts it into fail-fast, defeating the purpose.
The emphasis on client-side context support and connection pool limits shows that a Loader can pass context correctly, but if the HTTP client or transport doesn't honor it, cancellation remains ineffective.
The advice to refactor concurrency first and adjust retries separately is pragmatic: combining concurrency, retries, and timeouts at once increases the blast radius of mistakes.