This article details the design and implementation of a zero-allocation worker pool in Go for ingesting high-volume (10kHz) sensor data in an offline, air-gapped system. It focuses on critical architectural decisions to prevent GC pauses, manage backpressure, and ensure data integrity in a real-time, high-throughput environment. The solution employs a fixed-size worker pool with a buffered channel and `sync.Pool` for object recycling.
Read original on Dev.to #systemdesignThe core problem addressed is ingesting 10,000 optical frames per second from a biosensor without dropping samples or incurring significant latency. This requires a robust system capable of handling high throughput and precise timing, specifically in an embedded, offline context where resource management and predictability are paramount. Two primary constraints guided the design: preventing the receive loop from stalling due to slow downstream processing and eliminating garbage collection (GC) pauses on the hot path to avoid silent data drops.
The solution implements a fixed-size worker pool responsible for processing incoming `OpticalFrame` data. Data is pushed into the pool via a gRPC client-streaming call and then passed to a DSP forwarder. A key component is a buffered channel (`jobQueue`) that acts as a buffer between the gRPC handler and the workers, allowing the ingestion to proceed without blocking while providing headroom for downstream processing. The `sync.Pool` mechanism is crucial for achieving zero allocations per frame on the hot path.
type FramePool struct {
jobQueue chan *pb.OpticalFrame
wg sync.WaitGroup
forwarder *dsp.Forwarder
}
var frameSyncPool = sync.Pool{
New: func() any { return make([]byte, 0, 1024) },
}
func NewFramePool(workers, queueSize int, forwarder *dsp.Forwarder) *FramePool {
p := &FramePool{
jobQueue: make(chan *pb.OpticalFrame, queueSize),
forwarder: forwarder,
}
for i := 0; i < workers; i++ {
p.wg.Add(1)
go p.worker(i)
}
return p
}
func (p *FramePool) Enqueue(frame *pb.OpticalFrame) {
p.jobQueue <- frame // One channel send, blocks if queue is full
}Backpressure over Drops
The design prioritizes backpressure over silently dropping frames. If the `jobQueue` fills up, the `Enqueue` operation blocks, which propagates back through gRPC flow control to the sensor. This ensures data integrity by explicitly signaling overload rather than losing critical data points.
Inside the worker goroutines, `sync.Pool` is utilized to recycle `[]byte` scratch space. Instead of allocating a new buffer for each frame and relying on the garbage collector, workers acquire a buffer from `frameSyncPool`, use it for processing, and then return it. This drastically reduces allocations on the hot path, effectively mitigating GC pauses and ensuring stable, low-latency processing at high data rates.