Theme
Q050. What is DashMap, and how does sharding improve concurrency?
Short Answer
DashMap<K, V> is a concurrent hash map designed for shared access through &self. Internally it partitions entries across multiple shards, each protected independently. Operations on different shards can proceed concurrently, reducing contention compared with one RwLock<HashMap<K, V>>. It is useful for workloads dominated by independent per-key operations, but sharding does not make compound multi-key transactions atomic.
Deep Dive
The key's hash selects a shard, then the operation locks only that shard. This spreads lock traffic when keys are well distributed and enough operations overlap. It does not guarantee speed: hot keys, skewed hashes, frequent full-map iteration, or small workloads can remove the benefit.
The API returns reference-like guards for methods such as get and get_mut. Their lifetimes keep a shard locked. That is convenient for one-key updates but must be handled carefully around nested map calls and suspension points.
Use DashMap when the data model is naturally per-key and measurements show lock contention. For multi-entry invariants or atomic snapshots, an explicitly locked HashMap may provide clearer correctness.
Internal Model
Conceptually:
text
hash(key) -> shard index -> shard lock -> local hash tableThe implementation chooses and manages shards internally. Contention is reduced only when concurrent operations land on different shards. Each shard still has lock and cache-coherence costs.
Example
rust
use dashmap::DashMap;
fn main() {
let positions = DashMap::new();
positions.insert("alice", 10_i64);
positions
.entry("alice")
.and_modify(|position| *position += 5)
.or_insert(5);
assert_eq!(positions.get("alice").map(|value| *value), Some(15));
}Common Mistakes
- Describing
DashMapas a lock-free hash map. - Assuming operations on several keys form one atomic transaction.
- Expecting sharding to help when most traffic targets one hot key.
Follow-ups
- How does the hash function affect shard distribution?
- Why can iteration be more expensive than point operations?
- How would you maintain an invariant across two keys?
Related
- Q046. When should you use a
Mutexversus anRwLock? - Q051. How do
DashMapreferences and guards create deadlock risks? - Q052. When should you use
DashMapversusRwLock<HashMap>?
References
- DashMap Documentation
- Rust Standard Library