| English | 简体中文 |
Starshard is a high-performance, lazily sharded concurrent HashMap for Rust.
It is designed for real production workloads where you need:
Production-ready (v2.2.0).
Roadmap capabilities shipped in v2.2.0:
Clone, Cached, Cow) with epoch-based cache invalidation.[dependencies]
starshard = { version = "2.2.0", features = ["async", "rayon", "serde", "lifecycle", "advanced"] }
# minimal:
# starshard = "2.2.0"
ShardedHashMap::new(64).AsyncShardedHashMap.SnapshotMode::Cached first, then SnapshotMode::Cow.try_with_*).Migration guide:
| Feature | What you get | Typical use |
|---|---|---|
async |
AsyncShardedHashMap (Tokio RwLock) |
async services and workers |
rayon |
parallel snapshot flatten in iteration | large snapshot/scan workloads |
serde |
sync serialize/deserialize + async serializable snapshot helper | persistence/export |
lifecycle |
per_shard_load, memory_stats, drain, lifecycle structs |
observability and maintenance |
advanced |
transaction/CAS/replication/diagnostic APIs | advanced concurrency and control planes |
use starshard::ShardedHashMap;
let m: ShardedHashMap<String, i32> = ShardedHashMap::new(64);
m.insert("k1".into(), 10);
assert_eq!(m.get(&"k1".into()), Some(10));
assert_eq!(m.len(), 1);
#[cfg(feature = "async")]
#[tokio::main]
async fn main() {
use starshard::AsyncShardedHashMap;
let m: AsyncShardedHashMap<String, i32> = AsyncShardedHashMap::new(64);
m.insert("k1".into(), 10).await;
assert_eq!(m.get(&"k1".into()).await, Some(10));
}
| Goal | Sync API | Async API |
|---|---|---|
| insert/update | insert(k, v) |
insert(k, v).await |
| read | get(&k) |
get(&k).await |
| delete | remove(&k) |
remove(&k).await |
| batch insert | batch_insert(items) |
batch_insert(items).await |
| batch read | batch_get(&keys) |
batch_get(&keys).await |
| conditional update | compute_if_present(&k, f) |
compute_if_present(&k, f).await |
| conditional insert | compute_if_absent(k, f) |
compute_if_absent(k, f).await |
| metrics/introspection | shard_stats() / memory_stats() |
shard_stats().await / memory_stats().await |
Choose by strictness and control level:
with_shards_and_hasher(...)with_shards_and_hasher_capped(...)ShardCountError):
try_with_shards_and_hasher(...)try_with_shards_and_hasher_capped(...)with_snapshot_mode(...)with_shards_and_hasher_and_snapshot_mode(...)with_shards_and_hasher_capped_and_snapshot_mode(...)v2.2.0)use starshard::{RebalanceOptions, ShardedHashMap};
let m: ShardedHashMap<String, i32> = ShardedHashMap::new(8);
let report = m.rebalance_to(32, RebalanceOptions::default()).unwrap();
assert_eq!(report.from_shards, 8);
assert_eq!(report.to_shards, 32);
use starshard::ShardedHashMap;
let m: ShardedHashMap<String, i32> = ShardedHashMap::new(8);
m.start_rebalance_online(32).unwrap();
while m.rebalance_status().state == "migrating" {
m.advance_rebalance(2);
}
assert_eq!(m.rebalance_status().state, "idle");
Semantics:
advance_rebalance(...) drains all source shards.v2.2.0)SnapshotMode lets you pick snapshot behavior per workload:
Clone: always rebuild snapshot entries (default, lowest write-path overhead).Cached: reuse cached snapshot while no writes occur.Cow: maintain per-shard COW snapshot views for snapshot-heavy workloads.use starshard::{ShardedHashMap, SnapshotMode};
let clone_map: ShardedHashMap<String, i32> =
ShardedHashMap::with_snapshot_mode(64, SnapshotMode::Clone);
let cached_map: ShardedHashMap<String, i32> =
ShardedHashMap::with_snapshot_mode(64, SnapshotMode::Cached);
let cow_map: ShardedHashMap<String, i32> =
ShardedHashMap::with_snapshot_mode(64, SnapshotMode::Cow);
| Workload profile | Recommended mode |
|---|---|
| high write + low snapshot frequency | Clone |
| medium write + medium snapshot frequency | Cached |
| low write + high snapshot frequency | Cow or Cached |
RwLock<HashMap<..>> under mixed load.rayon improves large snapshot flatten throughput when scan size is high.Cached and Cow with your real key distribution.Serialize / Deserialize.S::default().async_snapshot_serializable().await helper for serialization.Examples:
examples/v210_rebalance.rsexamples/snapshot_mode_clone_demo.rsexamples/snapshot_mode_cached_demo.rsexamples/snapshot_mode_cow_demo.rsexamples/mixed_workload_snapshot_tradeoff_demo.rsBenchmark entry:
benches/bench_main.rsBefore release or upgrade verification:
cargo fmt --all
cargo test --all-features
cargo check --all-features
Vec<(K, V)> as output format.RebalanceOptions fields background, batch_size, max_pause_ns are forward-compatible placeholders in v2.2.0.Dual license:
You may choose either license.