Golang / GoLang Concurrency Mastery Interview Questions
How do you implement a high-performance sharded concurrent map in Go?
A single mutex protecting one map is a bottleneck under high concurrency. A sharded map divides the key space across N independent maps, each with its own mutex, reducing contention by approximately N-fold. Goroutines with keys in different shards can operate in parallel without competing.
import ( "hash/fnv" "fmt" "sync" ) const numShards = 32 type ShardedMap[K comparable, V any] struct { shards [numShards]struct { sync.RWMutex m map[K]V } } func NewShardedMap[K comparable, V any]() *ShardedMap[K, V] { sm := &ShardedMap[K, V]{} for i := range sm.shards { sm.shards[i].m = make(map[K]V) } return sm } func (sm *ShardedMap[K, V]) shardFor(key K) *struct { sync.RWMutex; m map[K]V } { h := fnv.New32a() fmt.Fprint(h, key) return &sm.shards[h.Sum32()%numShards] } func (sm *ShardedMap[K, V]) Set(key K, val V) { s := sm.shardFor(key) s.Lock(); defer s.Unlock() s.m[key] = val } func (sm *ShardedMap[K, V]) Get(key K) (V, bool) { s := sm.shardFor(key) s.RLock(); defer s.RUnlock() v, ok := s.m[key] return v, ok } // With 32 shards: 32 goroutines with different keys can write simultaneously // vs a single mutex where all goroutines serialise into one queue
More Related questions...