The Fan-Out / Fan-In Pattern
When processing bursty message queues, unconstrained concurrency inevitably leads to Out-Of-Memory (OOM) crashes or downstream database connection pool exhaustion.
Core Principles
- Bounded Buffers: Never use unbounded channels or in-memory queues.
- Worker Pool Sizing: Worker count should align with
runtime.NumCPU()for CPU-bound tasks, and target connection limits for I/O-bound tasks. - Coordinated Shutdown: Gracefully drain active jobs using
context.Contextandsync.WaitGroup.
Personal engineering field notes compiled from production systems. Patterns and benchmarks are updated as runtimes evolve.