Go

Concurrency Patterns

Worker pools, pipelines, fan-out/fan-in, cancellation, context, errgroup, semaphores

24 interview questionsยท
Mid-Level
1

What is a worker pool in Go?

Answer

A worker pool is a concurrency pattern that uses a fixed number of goroutines (workers) to process tasks from a shared channel. This pattern allows limiting the number of active goroutines and controlling system resource consumption. Unlike creating one goroutine per task, the worker pool reuses a set of workers to process a large volume of tasks efficiently.

2

What is the main advantage of using a worker pool instead of creating one goroutine per task?

Answer

Limiting the number of active goroutines allows controlling system resource consumption (memory and CPU) and avoiding overload. Creating one goroutine per task can result in thousands or millions of concurrent goroutines, saturating the scheduler and exhausting memory. The worker pool maintains an optimal number of workers that process tasks sequentially, ensuring predictable performance even under high load.

3

What is a pipeline in the context of concurrency in Go?

Answer

A pipeline is a series of stages connected by channels, where each stage is a group of goroutines executing the same function. Each stage receives data via input channels, performs processing, then sends results via output channels. This pattern allows decomposing complex processing into simple and reusable stages, facilitating composition and testing of each component independently.

4

How to properly close a channel in a worker pool to signal the end of processing?

5

What is the difference between fan-out and fan-in?

+21 interview questions

Master Go for your next interview

Access all questions, flashcards, technical tests, code review exercises and interview simulators.

Start for free