Level: AdvancedInterview

Go Concurrency Patterns Questions: Pools, Pipelines (Part 18)

Updated 10 min read
Go Concurrency Patterns Questions featured image: worker pool, pipeline and errgroup cards

These Go concurrency patterns questions cover worker pools, pipelines, fan-out, fan-in, semaphores, rate limits, errgroup and cancellation. We gathered them from public interview lists, coding rounds and community threads. Then we rewrote them in plain words and grouped them by pattern.

This post is questions only, so answer each one in your own words first. When you need help, read Go worker pool pattern, Go pipeline pattern with fan-out and fan-in and Go rate limiting. Also, finish Part 17 on context first, because every good pattern needs a clean way to stop.

How to Use These Go Concurrency Patterns Questions

Answer each of these Go concurrency patterns questions out loud, then sketch the goroutines and channels on paper. Most pattern bugs come from a missing close or a goroutine that never exits. Finally, try the scenario questions, since they come from real backend systems.

The table below lists each section and its number of questions.

SectionWhat it testsQuestions
Pattern basicsWhy patterns exist and how to pick one14
Worker poolsJobs, workers and results15
PipelinesStages, fan-out and fan-in15
Limits and backpressureSemaphores, rate limits and bounded work15
Errors and cancellationerrgroup, context and first result14
Pattern coding tasksClassic interview programs14
Go Concurrency Patterns Questions diagram showing six question areas from pattern basics to coding tasks
The six areas covered in this question list, from pattern basics to coding tasks.

Go Concurrency Patterns Questions on the Basics

Before you answer, study Go goroutines for beginners and Go channels with examples. These Go concurrency patterns questions check why patterns exist before you write any of them.

  1. What is a concurrency pattern, and why do teams reuse the same few patterns?
  2. Which patterns appear most often in Go backends?
  3. How do you decide between a worker pool and a goroutine per task?
  4. What is the generator pattern, and why does it return a receive-only channel?
  5. Why should the goroutine that creates a channel usually also close it?
  6. What does ownership of a channel mean in a pattern?
  7. How do you make sure every goroutine in a pattern can exit?
  8. When is a simple loop faster than any concurrency pattern?
  9. How do CPU-bound and IO-bound tasks change your pattern choice?
  10. What is the or-done pattern, and when do you need it?
  11. How does the tee pattern copy one stream into two?
  12. Why do many senior developers prefer plain code over clever patterns?
  13. Scenario: when a service starts one goroutine per request and crashes under load, which pattern would you add?
  14. Scenario: when a teammate builds a five stage pipeline for a tiny task, what would you say in the review?

Go Concurrency Patterns Questions on Worker Pools

Before you answer, study Go worker pool pattern. Worker pools are the most common pattern in interviews, so these Go concurrency patterns questions cover them in detail.

  1. What is a worker pool, and which three parts does it have?
  2. How do you choose the number of workers for CPU work and for network work?
  3. Who should close the jobs channel, and who should close the results channel?
  4. Why do you often need a separate goroutine that waits and then closes results?
  5. How do you keep track of which job produced which result?
  6. How do you return results in the same order as the jobs?
  7. What happens when a worker panics, and how do you protect the pool?
  8. How do you stop a worker pool early when the caller gives up?
  9. Should the jobs channel have a buffer, and how large?
  10. How do you report progress from a worker pool?
  11. How do you resize a worker pool at run time, and is it worth it?
  12. Why is a worker pool safer than unlimited goroutines for database calls?
  13. How do you test a worker pool for leaks and races?
  14. Scenario: when a pool of ten workers processes one million rows but memory keeps growing, what would you check?
  15. Scenario: when results must keep the input order for a report, how would you change the pool?

Go Concurrency Patterns Questions on Pipelines

Before you answer, study Go pipeline pattern with fan-out and fan-in. These Go concurrency patterns questions test how you join stages with channels and stop them all together.

  1. What is a pipeline, and what does each stage receive and return?
  2. How do you connect three stages so that each runs in its own goroutine?
  3. What is fan-out, and when does it speed up a pipeline?
  4. What is fan-in, and how does a merge function use a WaitGroup?
  5. Why does fan-out break the order of values, and how can you restore it?
  6. How do you stop every stage when the last stage stops reading?
  7. How does a done channel or a context flow through all stages?
  8. What is backpressure in a pipeline, and how do unbuffered channels give it to you?
  9. Where should you add buffers in a pipeline, and why?
  10. What should happen when the middle stage of a pipeline hits an error?
  11. How do you measure which stage is the slowest?
  12. How would you batch values in a stage, for example 100 rows per insert?
  13. What is the bridge pattern for a channel of channels?
  14. Scenario: when a pipeline hangs after the consumer returns early, how would you fix the leak?
  15. Scenario: when one slow stage holds back the whole pipeline, how would you scale only that stage?

Go Concurrency Patterns Questions on Limits

Before you answer, study Go rate limiting and Go circuit breaker pattern. Limits protect your service and others, so these Go concurrency patterns questions focus on safety under load.

  1. How does a buffered channel work as a counting semaphore?
  2. What does the semaphore package in golang.org/x/sync add?
  3. How does errgroup.SetLimit bound the number of running goroutines?
  4. What is the difference between a concurrency limit and a rate limit?
  5. Why does a token bucket allow short bursts?
  6. How do you use golang.org/x/time/rate to limit calls per second?
  7. How do you limit requests per user instead of per server?
  8. What is load shedding, and when should a service reject work early?
  9. How does a circuit breaker stop a slow dependency from pulling you down?
  10. What does singleflight do, and how does it stop a cache stampede?
  11. How do you pick a timeout for each call in a fan-out?
  12. Why can an unbounded queue hide a slow consumer until memory runs out?
  13. How do you test that a limit really holds under load?
  14. Scenario: when a partner API allows only 50 calls per second, how would you design the client?
  15. Scenario: when a cache expires and a thousand requests hit the database at once, which pattern would you use?

Errors and Cancellation in Patterns

Before you answer, study Go context for beginners and Go error wrapping. These Go concurrency patterns questions check that your patterns fail cleanly instead of hanging.

  1. How does errgroup.Group collect the first error from many goroutines?
  2. What does errgroup.WithContext cancel, and when?
  3. Can you collect all errors instead of only the first one, and how?
  4. How do you return the first successful result and cancel the rest?
  5. How do you add a timeout to a whole fan-out at once?
  6. Why should each worker check ctx.Done between jobs?
  7. How do you pass errors through a pipeline, inside the value or on a separate channel?
  8. What happens to in-flight jobs when someone cancels the context?
  9. How do you retry a failed job without blocking the other workers?
  10. How do you shut down a pattern gracefully on SIGTERM?
  11. Why should you drain a results channel after cancelling?
  12. How do you log errors from many workers without flooding the logs?
  13. Scenario: when one of five parallel calls fails, how would you cancel the other four and return a clear error?
  14. Scenario: when a shutdown takes minutes because workers ignore cancellation, how would you fix them?

Go Concurrency Patterns Questions for Coding Rounds

Before you answer, write each program by hand and run it with -race. Interviewers watch who closes each channel and how the program ends.

  1. Build a worker pool with three workers that squares ten numbers.
  2. Write a generator that yields the first N Fibonacci numbers on a channel.
  3. Build a three stage pipeline that reads lines, filters blanks and counts words.
  4. Write a generic merge function that fans in any number of channels.
  5. Fan out URL checks to five workers and fan in the status codes.
  6. Limit parallel downloads to four with a semaphore channel.
  7. Call three mirrors and return the first answer, then cancel the others.
  8. Write a rate limited loop that sends at most ten requests per second.
  9. Run tasks with errgroup and stop on the first error.
  10. Batch incoming events into groups of 100 or every second, whichever comes first.
  11. Build a tiny pub-sub with subscribe and publish methods.
  12. Keep results in input order while using five workers.
  13. Scenario: when an interviewer asks you to crawl a site with at most ten requests at once, how would you design it?
  14. Scenario: when a log processor must handle bursts without dropping lines, how would you add backpressure?

Code Reading Questions

Read each program carefully and predict the output before you run it. Then explain the result in your own words.

package main

import "fmt"

func gen(nums ...int) <-chan int {
    out := make(chan int)
    go func() {
        defer close(out)
        for _, n := range nums {
            out <- n
        }
    }()
    return out
}

func square(in <-chan int) <-chan int {
    out := make(chan int)
    go func() {
        defer close(out)
        for n := range in {
            out <- n * n
        }
    }()
    return out
}

func main() {
    for v := range square(square(gen(1, 2, 3))) {
        fmt.Print(v, " ")
    }
    fmt.Println()
    total := 0
    for v := range square(gen(4, 5)) {
        total += v
    }
    fmt.Println("total:", total)
}
  1. What does each line print, and why do the values keep their order?
  2. What would happen if square forgot to close its out channel?

The second program is a small worker pool with a closer goroutine.

package main

import (
    "fmt"
    "slices"
    "sync"
)

type result struct {
    job, value int
}

func main() {
    jobs := make(chan int)
    results := make(chan result)
    var wg sync.WaitGroup
    for w := 1; w <= 3; w++ {
        wg.Add(1)
        go func() {
            defer wg.Done()
            for j := range jobs {
                results <- result{job: j, value: j * 10}
            }
        }()
    }
    go func() {
        for j := 1; j <= 5; j++ {
            jobs <- j
        }
        close(jobs)
    }()
    go func() {
        wg.Wait()
        close(results)
    }()
    var got []int
    for r := range results {
        got = append(got, r.value)
    }
    slices.Sort(got)
    fmt.Println(got, len(got))
}
  1. What does the program print, and why does it need slices.Sort?
  2. Why does the WaitGroup wait inside its own goroutine instead of in main?

Quick Revision Table for Concurrency Patterns

Use the table below as a final check before your interview. It maps each pattern to the problem it solves.

PatternSolvesMain tool
Worker poolToo many goroutinesJob channel and WaitGroup
PipelineWork in clear stepsOne channel per stage
Fan-out, fan-inOne slow stageMany workers and a merge
SemaphoreToo many calls at onceBuffered channel
Rate limiterToo many calls per secondToken bucket
errgroupErrors from many goroutinesGroup with context

Also, many interviewers ask you to compare worker pools and pipelines directly. The diagram below shows the difference side by side.

Go concurrency patterns diagram comparing a worker pool with a pipeline
A worker pool runs the same task on many workers, while a pipeline splits work into steps.

Official Resources

These official pages explain the rules behind these questions.

Quick Recap

  • These Go concurrency patterns questions cover basics, worker pools, pipelines, limits, errors and coding tasks.
  • A worker pool caps the number of goroutines and shares one job channel.
  • A pipeline links stages with channels, and fan-in merges many channels into one.
  • Semaphores limit how many calls run at once, while rate limiters limit calls per second.
  • Use errgroup with a context, so one failure stops the rest.
  • Every goroutine in a pattern needs a clear owner and a clear way to exit.

What’s Next?

By now, you have Go concurrency patterns questions for every common pattern. Study the answers in your own notes, then check them against the Go interview topics study guide.

Next up: Go HTTP and REST API interview questions (Part 19)