Recent posts

Jan 9, 2026

Atomic Integer vs Mutex in Go: Why You're Paying for a Lock You Don't Need

TL;DR Mutex (Pessimistic): Blocks all goroutines. It’s safe but slow under contention. Atomic (Optimistic): No blocking, retries on collision. Fast for simple operations. Use Atomic when: Single operation on a shared int/bool (counters, flags, gauges) Use Mutex when: Multiple operations must be atomic together, or failure/retry is unacceptable Benchmark result: Atomic significantly outperforms Mutex as goroutine count increases You’re building a high-throughput API server in Go. Requests are flooding in, and you need to count them across multiple goroutines. Simple enough, right? You reach for sync.Mutex, wrap your counter, and move on.

Read More...