Is singleflight the right way to prevent a cache stampede in my Go API?
Start with singleflight plus stale-while-revalidate, and add a Redis distributed lock only once per-pod rebuilds measurably hurt Postgres.
Tag
Concurrent execution: goroutines, channels, locks and taming race conditions. questions under this tag, answered directly.
6 answered questions carry this tag.
Start with singleflight plus stale-while-revalidate, and add a Redis distributed lock only once per-pod rebuilds measurably hurt Postgres.
Keep channel capacity near the worker count, let blocking propagate to the Kafka poll loop, and when consumer lag climbs grow workers, not the buffer.
Stay in FPM and start with a Guzzle Pool pinned to 20-50 concurrency; move to AMPHP v3 only if the codebase grows async, Swoole only when re-architecting.
Move to errgroup.WithContext with a request-level timeout on top and thread ctx into every HTTP and DB call; an ignored ctx means no cancellation at all.
Cancellation propagates only if you pass r.Context() into every downstream call, leave no context.Background() in the chain, and use WithoutCancel.
Export NumGoroutine() as a metric, take a pprof goroutine dump at peak, make the spawn site behind the parked stacks honor ctx.Done(), then add goleak.