ave-actors-store
Event-sourced persistence for actors built on ave-actors-actor. Actors record state changes as an immutable event log and recover their state on restart by replaying events and loading snapshots.
This crate is part of the ave-actors workspace.
Core concepts
| Concept | Description |
|---|---|
PersistentActor |
Trait extending Actor with event sourcing — implement apply and call persist |
LightPersistence |
Strategy: stores event + state snapshot on every write (fast recovery) |
FullPersistence |
Strategy: stores events only, replays on recovery (smaller footprint, full audit trail) |
InitializedActor<A> |
Required wrapper returned by PersistentActor::initial(params) |
DbManager<C, S> |
Backend factory trait — implement to plug in a custom database |
Collection |
Ordered key-value storage for the event log |
State |
Single-value storage for state snapshots |
Main API
| API | Receives | Returns | Purpose |
|---|---|---|---|
PersistentActor::create_initial |
InitParams |
Self |
Builds the base state used on first start and recovery without a snapshot |
PersistentActor::initial |
InitParams |
InitializedActor<Self> |
Wraps the actor so the actor system accepts it as persistent |
PersistentActor::apply |
&Event |
Result<(), ActorError> |
Applies one event to in-memory state |
PersistentActor::start_store |
store name, optional prefix, ActorContext, backend manager, optional EncryptedKey |
Result<(), ActorError> |
Opens the backend and recovers persisted state into the actor |
PersistentActor::persist |
&Event, ActorContext |
Result<(), ActorError> |
Applies and durably records one event, rolling back memory on failure |
PersistentActor::snapshot |
ActorContext |
Result<(), ActorError> |
Forces an immediate snapshot of the current actor state |
Quick start
use ;
use ;
use async_trait;
use ;
use ;
use info_span;
// --- Events ---
// --- Messages & responses ---
// --- Actor ---
// --- PersistentActor ---
Create and use the actor:
use ;
use PersistentActor;
use CancellationToken;
async
Persistence strategies
| Strategy | Write | Recovery | When to use |
|---|---|---|---|
LightPersistence |
Event + state snapshot | Load snapshot (no replay) | When recovery speed matters most |
FullPersistence |
Event only | Load last snapshot + replay remaining events | When storage efficiency or a full audit trail matters |
For FullPersistence, snapshots are taken automatically every N events (default: 100). Override to tune:
Set compact_on_snapshot() -> bool to true to delete events already covered by a snapshot and reduce storage use:
Encryption
Pass an [EncryptedKey] to start_store to encrypt all events and snapshots at rest using XChaCha20-Poly1305:
use EncryptedKey;
async
The key itself is held in memory encrypted via ASCON AEAD (see ave-actors-actor's EncryptedKey).
Storage backends
| Crate | Backend | Use case |
|---|---|---|
ave-actors-store (built-in) |
MemoryManager |
Tests and ephemeral state |
ave-actors-sqlite |
SQLite (via rusqlite) |
Single-node, embedded persistence |
ave-actors-rocksdb |
RocksDB | High-throughput, multi-actor workloads |
All three backends implement DbManager<C, S> and can be swapped without changing actor code.
Implementing a custom backend
Implement DbManager, Collection, and State from ave_actors_store::database:
use ;
Use the test_store_trait! macro from ave_actors_store to verify your implementation:
test_store_trait!