Buffered Channels
Buffered channels have a capacity — sends succeed without blocking until the buffer fills.
Introduction
Buffered channels have a capacity — sends succeed without blocking until the buffer fills. They implement queues, semaphores, and backpressure in high-throughput systems. Choosing the right buffer size affects latency, memory, and deadlock risk.
Understanding when buffer size 0 (sync), 1 (handoff), or N (pipeline) is appropriate separates production-ready concurrent code from tutorial examples. This lesson covers semaphore patterns, rate limiting, and avoiding buffer-related deadlocks.
Buffered channels appear in worker pools, logging pipelines, and Kubernetes controller work queues — patterns you will implement in the final project.
The story
A Cloudflare log ingestion worker uses a buffered channel of capacity 1000 to decouple HTTP handlers from slow S3 uploads. Handlers enqueue log batches without blocking until the buffer fills — then backpressure signals the load balancer to shed traffic before the process OOMs.
Choosing buffer size is an engineering trade-off: too small adds latency under burst; too large hides backpressure until memory exhaustion — production tuning uses metrics on channel length via custom instrumentation.
Understanding the topic
Key concepts
- make(chan T, n) creates buffer of n elements.
- len(ch) current items; cap(ch) buffer capacity.
- Send non-blocking when len < cap; blocks when full.
- Receive non-blocking when len > 0; blocks when empty.
- Semaphore: chan struct{} with cap N limits concurrency.
- Too large buffer hides backpressure until OOM.
Step-by-step explanation
- Producer sends until buffer full, then blocks.
- Consumer receives, freeing slot for blocked sender.
- Zero buffer: synchronous handoff.
- Size-1 buffer: async handoff with one in-flight item.
- Close drains remaining buffer then signals done.
- Select with default enables non-blocking try-send/receive.
Practical code example
Semaphore limiting concurrent API calls with buffered channel:
package mainimport ("fmt""sync""time")func fetch(id int, sem chan struct{}, wg *sync.WaitGroup) {defer wg.Done()sem <- struct{}{} // acquiredefer func() { <-sem }() // releasefmt.Printf("fetch %d started\n", id)time.Sleep(100 * time.Millisecond)fmt.Printf("fetch %d done\n", id)}func main() {const maxConcurrent = 2sem := make(chan struct{}, maxConcurrent)var wg sync.WaitGroupfor i := 1; i <= 5; i++ {wg.Add(1)go fetch(i, sem, &wg)}wg.Wait()}
Line-by-line code explanation
func fetch(id int, sem chan struct{}, wg *sync.WaitGroup) {is run once per goroutine. It receives a channel used as a semaphore and a pointer to the shared WaitGroup. The empty struct is used because it carries no data.defer wg.Done()is the first defer. Deferred calls run in reverse order, so this one runs last, after the semaphore slot has been released.sem <- struct{}{}acquires a slot by sending an empty struct value into the channel. If the buffer already holds two values, this send blocks until another goroutine frees one.defer func() { <-sem }()releases the slot. Receiving from the channel removes one value from the buffer, and the defer makes sure that happens when the function returns.fmt.Printf("fetch %d started\n", id)runs only once a slot has been acquired. After a 100 millisecond sleep standing in for real work, the function prints that fetch %d is done.const maxConcurrent = 2names the limit, so at most two fetches can be working at the same time.sem := make(chan struct{}, maxConcurrent)creates a buffered channel with capacity 2. That capacity is what limits how many goroutines can hold a slot.go fetch(i, sem, &wg)starts one goroutine for each of five fetches. Each pass of the loop calls wg.Add(1) first, so all five goroutines start, but only two at a time get past the acquire step.wg.Wait()blocks main until all five fetches have called Done. Without it, main could return and end the program while goroutines are still running.
Key takeaway: Buffered channel as counting semaphore. maxConcurrent=2 limits parallel fetches. Adjust buffer to match resource limits (DB connections, API rate).
Real-world use
Where you'll use this in production
- Limiting concurrent database queries to pool size.
- Job queue between API handler and background workers.
- Batched log shipping with buffer before flush.
- Token bucket rate limiters for external API calls.
Best practices
- Size buffer based on measured producer/consumer rates.
- Monitor channel len in metrics for queue depth alerting.
- Use semaphore pattern for resource-bound concurrency.
- Prefer small buffers — large buffers mask overload.
- Combine with context timeout on blocked sends.
Common mistakes
- Huge buffer causing memory bloat under load spike.
- Deadlock: all goroutines blocked on full channel, none receiving.
- Assuming send always succeeds — blocks when full.
- Using buffer to 'fix' race without proper sync.
- Not handling shutdown when buffer has pending items.
Advanced interview questions
Interview Prep
Practice concise answers, then expand each card for the explanation.
1BeginnerQuestionlen vs cap on channel?+
Answer
2BeginnerQuestionSemaphore with channel?+
Answer
3IntermediateQuestionChoose buffer size?+
Answer
4IntermediateQuestionNon-blocking channel operation?+
Answer
5AdvancedQuestionDesign backpressure for slow consumer.+
Answer
Summary
Buffered channels queue up to cap elements before blocking. Use as semaphores to limit concurrent resource usage. Size buffers deliberately — oversized buffers hide overload. Monitor queue depth in production systems. Next lesson: select — multiplexing channel operations.