Fan-out, fan-in and ordering

Fan-out means several goroutines read from one channel and share the work; fan-in means several channels are read into one. The two together are the shape of a parallel map: a queue of items, a pool, and one stream of results. The part that needs care is the end: the merged channel closes only when the last worker is done.

Fan-out: one input, three workers

Three copies of the same worker read from numbers, so the six values are split between them rather than duplicated: a receive on a channel delivers to exactly one goroutine. Each worker squares what it took and writes to its own output channel, and the program adds up everything that comes out of the merge.

package main import ( "fmt" "sync" ) func worker(in <-chan int) <-chan int { out := make(chan int) go func() { defer close(out) for n := range in { out <- n * n } }() return out } func merge(channels ...<-chan int) <-chan int { var wg sync.WaitGroup out := make(chan int) for _, ch := range channels { wg.Add(1) go func() { defer wg.Done() for v := range ch { out <- v } }() } go func() { wg.Wait() close(out) }() return out } func main() { numbers := make(chan int) go func() { defer close(numbers) for i := 1; i <= 6; i++ { numbers <- i } }() a := worker(numbers) b := worker(numbers) c := worker(numbers) sum := 0 count := 0 for v := range merge(a, b, c) { sum += v count++ } fmt.Println("values:", count) fmt.Println("sum:", sum) }

go build -o app main.go && ./app

values: 6
sum: 91

Six values and the sum of their squares, whichever worker got which: fan-out changes who does the work, not what the work is. The merge reads a channel per worker; it cannot range over a slice of channels, so one goroutine per input is the standard form.

Fan-in: a merge stage

The merge has two wait points. The forwarding goroutines end when their input closes, and a separate goroutine waits for all of them and only then closes the merged output. Closing it earlier would drop the results of a slow worker; closing it from a forwarding goroutine would close it several times and panic.

A merge of channels of different types needs a small struct or a generic function; the pattern above is the generic one, with T in place of int. The next part of the course writes that generic version in the capstone project, where three kinds of events flow into one log.

Keeping the order

A merged channel delivers results in completion order, which is not the order of the input. When the order matters, the result carries its place: each job knows its index and the consumer writes into a slice at that index. The workers can finish in any order and the output is still the input order.

package main import ( "fmt" "sync" ) type result struct { index int 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 n := range jobs { results <- result{index: n, value: n * n} } }() } go func() { for i := 1; i <= 5; i++ { jobs <- i } close(jobs) }() go func() { wg.Wait() close(results) }() ordered := make([]int, 6) for r := range results { ordered[r.index] = r.value } fmt.Println("ordered:", ordered[1:]) }

go build -o app main.go && ./app

ordered: [1 4 9 16 25]

The slice is indexed from one and the print drops the unused zero slot. The cost is a slice as large as the input, which is fine for a batch and wrong for an unbounded stream; a stream that must stay in order is processed by one worker, or with a windowed reorder buffer.

The first answer wins

The opposite of fan-in with all results is fan-in with the first: two sources are asked the same question and the faster answer is used. A select over the two channels returns as soon as one is ready, and the other goroutine is left to finish or to be cancelled by a context. This is how a cache and a database are combined without waiting for the slower one.

package main import ( "fmt" "time" ) func fetch(name string, delay time.Duration) <-chan string { out := make(chan string) go func() { time.Sleep(delay) out <- name }() return out } func main() { fast := fetch("cache", 5*time.Millisecond) slow := fetch("database", 30*time.Millisecond) select { case v := <-fast: fmt.Println("first:", v) case v := <-slow: fmt.Println("first:", v) } }

go build -o app main.go && ./app

first: cache

The database goroutine is still sleeping when main returns, and the program ends before it prints. In a service the loser is cancelled through a context and its result is discarded, which is the same pattern with a deferred cancel and a select that also watches ctx.Done().

Exercises

  1. Run the fan-out program with one worker and then with six. Which line of the output changes, and which stays the same?
  2. Close the merged output from inside a forwarding goroutine and read the panic. How many places would close the same channel?
  3. Give the ordered program a job that sleeps longer than the others. Does the output order change, and why not?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What is fan-out?

    Answer

    Several goroutines reading from one channel

    A receive delivers each value to one worker.

  2. What is fan-in?

    Answer

    Merging several channels into one

    A merge stage reads each input.

  3. Who closes the merged channel?

    Answer

    A goroutine that waits for all the forwarding goroutines

    Closing early drops results, closing twice panics.

  4. How is the input order kept?

    Answer

    By carrying the index in the result and writing at that index

    The workers finish in any order; the slice does not.

  5. What does the consumer of a merge do?

    Answer

    Ranges until the merged channel is closed

    The close is the end of the stream.

  6. What does a select over two fetches return?

    Answer

    The first ready result

    The other goroutine is left behind or cancelled.

  7. Why is the loser of a first-answer race cancelled in a service?

    Answer

    To free its resources and stop its work

    The work is no longer wanted.

  8. Which output does a fan-out change?

    Answer

    Who computes each result and in what order they finish

    Each value is still processed once.

  9. What does the ordered example print?

    Answer

    ordered: [1 4 9 16 25]

    The squares are placed at their index.

  10. What is the cost of keeping the order with an index?

    Answer

    A slice as large as the input

    Fine for a batch, wrong for an unbounded stream.

The code on this page was run with go 1.26.10 on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev