capypad
0 day streak
go / expert
Snippet

High-Concurrency Map with Lock Striping

Lock striping is a pattern to reduce contention on a shared resource by splitting it into multiple independent 'shards', each with its own lock. In a highly concurrent environment, a single global mutex on a map becomes a bottleneck. By hashing the key to select a shard, multiple goroutines can read and write to different parts of the map simultaneously.

snippet.go
go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
type Shard struct {
sync.RWMutex
m map[string]string
}
 
type ShardedMap []*Shard
 
func (s ShardedMap) getShard(key string) *Shard {
return s[uint32(crc32.ChecksumIEEE([]byte(key)))%uint32(len(s))]
}
 
func (s ShardedMap) Get(key string) string {
shard := s.getShard(key)
shard.RLock()
defer shard.RUnlock()
return shard.m[key]
}
Breakdown
1
type Shard struct
Defines a segment of the data protected by its own mutex.
2
s[uint32(crc32.ChecksumIEEE([]byte(key)))%uint32(len(s))]
Determines which shard handles the given key via hashing.