Highly concurrent in-memory counter in GoLang
Oct 6, 2025Naveen Kumar Jakuva Premkumar

Summary
This post details the implementation and benchmarking of a highly concurrent in-memory counter in GoLang for tracking marketing campaign usage. It compares a mutex-based approach with the use of `sync.Map`, demonstrating that `sync.Map` offers a 3x speed advantage and significantly higher throughput for read-heavy workloads with a finite set of frequently accessed keys. The flushing mechanism was also optimized by atomically removing keys from the map using `LoadAndDelete` instead of locking the entire map. This optimization resulted in a 68% decrease in usage tracking update queries and substantial reductions in database CPU utilization.