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
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
