Menu

Post image 1
Post image 2
Post image 3
1 / 3
0

A Lock-Free Counter in Go: atomic, sync.Map, or Just a Mutex?

DEV Community·Gabriel Anhaia·28 days ago
#ZJglIYZe
#when#go#concurrency#atomic#int64#sync
Reading 0:00
15s threshold

Book: The Complete Guide to Go Programming Also by me: Thinking in Go (2-book series) — Complete Guide to Go Programming + Hexagonal Architecture in Go My project: Hermes IDE | GitHub — an IDE for developers who ship with Claude Code and other AI coding tools Me: xgabriel.com | GitHub A team I talked to had one prometheus counter incremented from every request handler. They reached for sync.Map because the metric registry was already keyed by name, and the rest of the registry used one. Under load test the counter showed up in CPU profiles. Not as the top frame, but high enough that one engineer started a thread on Slack asking whether Go was the wrong language for hot-path counters. The language was fine. The container was the problem. You have a few reasonable ways to write a shared counter in Go. Most teams pick one out of habit and never benchmark the others. The habit is usually wrong for the workload, and the cost shows up only when traffic doubles.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.

Read More