dstate
Distributed state replication for Rust actor frameworks.
dstate provides a framework-agnostic core for managing replicated state
across nodes in a cluster. State changes are projected into public views,
synchronized via configurable strategies (active push, change-feed, periodic
sync), and persisted through a pluggable storage interface.
Workspace Crates
| Crate | Description |
|---|---|
dstate |
Core library — traits, types, replication logic, test support |
dstate-ractor |
Adapter for the ractor actor framework |
dstate-kameo |
Adapter for the kameo actor framework |
Quick Start
Add the core crate and an adapter to your Cargo.toml:
[]
= "0.1"
= "0.1" # or dstate-kameo = "0.1"
Define a Distributed State
Implement DistributedState for simple state types where the entire state is
the public view:
use ;
use ;
For state types with private fields, delta synchronization, or view
projections, implement DeltaDistributedState instead. See the example
programs in each adapter crate's examples/ directory.
Use an Actor Runtime
use ;
use RactorRuntime; // or dstate_kameo::KameoRuntime
async
Architecture
┌─────────────────────────────────────────────┐
│ Application Code │
├─────────────────────────────────────────────┤
│ dstate (core traits + replication) │
│ ┌─────────┐ ┌──────────┐ ┌───────────┐ │
│ │ State │ │ Sync │ │ Persistence│ │
│ │ Traits │ │ Strategy │ │ Traits │ │
│ └─────────┘ └──────────┘ └───────────┘ │
├──────────────────┬──────────────────────────┤
│ dstate-ractor │ dstate-kameo │
│ (ractor adapter)│ (kameo adapter) │
└──────────────────┴──────────────────────────┘
Key Concepts
- State — The full internal data on each node (may contain private fields)
- View — A public projection of state, shared with peers
- ViewDelta — An incremental update to a view (optional, for bandwidth efficiency)
- Generation —
(incarnation, age)pair for crdt-style conflict resolution - SyncStrategy — Configures how and when state changes are replicated:
active_push()— Push every mutation immediatelyfeed_lazy_pull()— Batch changes, pull on queryfeed_with_periodic_sync()— Change feed plus periodic full syncperiodic_only()— Only periodic full snapshots
- ActorRuntime — Framework-agnostic actor spawning, timers, and groups
- ClusterEvents — Subscribe to node join/leave notifications
- StatePersistence — Async save/load for crash recovery
Choosing an Adapter
| Feature | dstate-ractor |
dstate-kameo |
|---|---|---|
| Framework | ractor | kameo |
| Mailbox | Unbounded | Bounded (default) |
| Spawn | Async (bridge thread) | Sync (cheaper) |
| Send | cast() fire-and-forget |
tell().try_send() fire-and-forget |
| Timers | tokio tasks | tokio tasks |
Both adapters expose identical ActorRuntime semantics. Choose based on your
preferred actor framework.
Testing
The core crate includes test_support with mock implementations:
TestRuntime— In-memory actor runtime with channel-based mailboxesTestClock— Deterministic clock with manualadvance()InMemoryPersistence— In-memory save/load for testingTestState/TestDeltaState— Example state implementations
use ;
use Clock;
use TestClock;
Run all tests:
License
MIT