Skip to Content
Skip to content
Choose Between sync.Mutex, sync.RWMutex, and sync/atomic
Advanced
Tutorials

Choose Between sync.Mutex, sync.RWMutex, and sync/atomic

Compare sync.Mutex, sync.RWMutex, and sync/atomic on real read-heavy and write-heavy Go benchmarks, and see when RWMutex slows down under contention. After this, you can pick the simplest synchronization primitive that fits your workload.

Updated 4 min read GoConcurrency

When two goroutines touch the same state, Go gives you three main tools in the standard library: sync.Mutex, sync.RWMutex, and sync/atomic. Picking the wrong one rarely breaks correctness, but it can quietly cost you 10x in throughput or introduce subtle lock-ordering bugs.

The short version: reach for sync.Mutex first, switch a hot single counter or flag to sync/atomic, and only use sync.RWMutex when benchmarks prove readers spend real time inside the lock.

Skip ahead

Already know the trade-offs? Jump to the 99%-read benchmark numbers or the decision table.

Benchmarking 99% reads vs. 1% writes

A common assumption is that sync.RWMutex always beats sync.Mutex on read-heavy workloads. Let's test that on a shared int64 counter where 99% of calls are reads and 1% are writes:

package syncbench

import (
	"sync"
	"sync/atomic"
	"testing"
)

func BenchmarkMutexRead99(b *testing.B) {
	var mu sync.Mutex
	var v int64
	b.RunParallel(func(pb *testing.PB) {
		i := 0
		for pb.Next() {
			i++
			mu.Lock()
			if i%100 == 0 {
				v++
			} else {
				_ = v
			}
			mu.Unlock()
		}
	})
}

func BenchmarkRWMutexRead99(b *testing.B) {
	var mu sync.RWMutex
	var v int64
	b.RunParallel(func(pb *testing.PB) {
		i := 0
		for pb.Next() {
			i++
			if i%100 == 0 {
				mu.Lock()
				v++
				mu.Unlock()
			} else {
				mu.RLock()
				_ = v
				mu.RUnlock()
			}
		}
	})
}

func BenchmarkAtomicRead99(b *testing.B) {
	var v atomic.Int64
	b.RunParallel(func(pb *testing.PB) {
		i := 0
		for pb.Next() {
			i++
			if i%100 == 0 {
				v.Add(1)
			} else {
				_ = v.Load()
			}
		}
	})
}

Here is the output in golang:trixie (Go 1.27):

$ go test -bench=. -benchmem -benchtime=200ms
goos: linux
goarch: amd64
pkg: example.com/syncbench
cpu: QEMU Virtual CPU version 2.5+
BenchmarkMutexRead99-2     	27176330	         9.073 ns/op	       0 B/op	       0 allocs/op
BenchmarkRWMutexRead99-2   	27857293	         8.188 ns/op	       0 B/op	       0 allocs/op
BenchmarkAtomicRead99-2    	491158420	         0.4812 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	example.com/syncbench	0.792s

Two things stand out immediately:

  1. sync.RWMutex only shaved ~0.9 ns off sync.Mutex here, because every RLock() and RUnlock() still performs an atomic write to the shared reader counter—causing CPU cache-line bouncing across cores.
  2. atomic.Int64 ran at 0.48 ns/op—roughly 17x faster than both mutexes—because reads never write to a shared lock word at all.

Which primitive fits which job?

Primitive Best for Avoid when
sync/atomic (atomic.Int64, atomic.Bool, atomic.Pointer[T]) Single counters, feature flags, health states, read-only config swaps You need two fields to change together (an invariant across multiple variables)
sync.Mutex Maps, slices, multi-field structs, short critical sections A single integer counter on a hot path where atomics work
sync.RWMutex Read-heavy caches where readers hold the lock for microseconds+ (e.g., scanning a map or slice) Tiny nanoseconds-long reads on many cores, or frequent writes

💡 Tip

Since Go 1.19, prefer typed wrappers like atomic.Int64, atomic.Bool, and atomic.Pointer[T] over raw atomic.AddInt64(&x, 1). They prevent accidental non-atomic reads and make alignment bugs impossible.

Common mistakes

  • Recursive locking with sync.RWMutex: Calling RLock(), then calling another helper that calls RLock() on the same mutex, deadlocks if a writer calls Lock() in between (Go's RWMutex blocks new readers while a writer is waiting).
  • Mixing atomic and plain reads: Reading x directly while another goroutine calls atomic.AddInt64(&x, 1) is a data race. Every access must use atomic methods.
  • Copying a mutex by value: Passing a struct containing a sync.Mutex by value copies the lock state and triggers a go vet warning. Pass a pointer instead.

⚠ Warning

Never "upgrade" a read lock to a write lock while holding RLock(). Two readers trying to upgrade at once will deadlock permanently. Always release RUnlock(), acquire Lock(), and re-check the condition.

What happens if you skip this

Reaching for sync.RWMutex out of habit adds complexity and cache contention with no speedup on short reads. Conversely, splitting a multi-field update across two separate atomic values lets another goroutine observe a half-updated state that a single sync.Mutex would have prevented.

Related course: Go Advanced.

About

Compare sync.Mutex, sync.RWMutex, and sync/atomic on real read-heavy and write-heavy Go benchmarks, and see when RWMutex slows down under contention. After this, you can pick the simplest synchronization primitive that fits your workload.

Responsible
Jay P
Last Update
11/27/2026
Reading Time
4 min
Was this helpful?
​ 0 ​ 0

Comments

Sign in to comment

No comments yet.