Principles

  • Keep goroutine count bounded; size pools to CPU/core and downstream QPS.
  • Apply backpressure: bounded channels + select with default to shed load early.
  • Context everywhere: cancel on timeouts/parent cancellation; close resources.
  • Prefer immutability; minimize shared state. Use channels for coordination.

Patterns

  • Worker pool with buffered jobs; fan-out/fan-in via contexts.
  • Rate limit with time.Ticker or golang.org/x/time/rate.
  • Semaphore via buffered channel for limited resources (DB, disk, external API).

Sync cheatsheet

  • sync.Mutex for critical sections; avoid long hold times.
  • sync.WaitGroup for bounded concurrent tasks; errgroup for cancellation on first error.
  • sync.Map only for high-concurrency, write-light cases; prefer map+mutex otherwise.

Instrument & guard

  • pprof: net/http/pprof + CPU/mem profiles in staging under load.
  • Trace blocking: GODEBUG=schedtrace=1000,scavtrace=1 when diagnosing.
  • Metrics: goroutines, GC pause, allocations, queue depth, worker utilization.

Checklist

  • Context per request; timeouts set at ingress.
  • Bounded goroutines; pools sized and observable.
  • Backpressure on queues; drop/timeout strategy defined.
  • pprof/metrics enabled in non-prod and behind auth in prod.
  • Load tests for saturation behavior and graceful degradation.