One Global Lock Around the Whole Map Versus Striped Locks
TradeoffConcurrencyMidSeniorUpdated
You're locking a 1M-entry in-memory map. One big lock is simple but throughput is poor under writes.
Short answer: Striped locks (e.g., 16 segments, each with its own lock). Striped / fine-grained locks let unrelated keys be updated concurrently, under uniform keys you get roughly N-way write parallelism.
The Two Options
A
One global lock around the whole map
B
Striped locks (e.g., 16 segments, each with its own lock)
Drill This: Your Pick First
The Better Pick
Option B
Striped locks (e.g., 16 segments, each with its own lock)
Striped / fine-grained locks let unrelated keys be updated concurrently, under uniform keys you get roughly N-way write parallelism. This is exactly how Java's pre-Java-8
ConcurrentHashMap worked.When the Other One Wins
One global lock around the whole map
If operations frequently touch many keys (
putAll, atomic multi-key updates) a single lock is simpler and avoids deadlock from acquiring multiple stripes.