State Sync provides a conflictβfree replicated keyβvalue store that allows agents to maintain a shared, eventuallyβconsistent view of the swarmβs state without central coordination. It uses ConflictβFree Replicated Data Types (CRDTs) to guarantee merge convergence.
-
KeyβValue Store
- Insert, update, delete operations on arbitrary keys (stringβlike).
- Values can be primitive types (integers, floats, strings) or nested maps/lists.
- Tombstoneβfree garbage collection (e.g., using LSEQ trees).
-
CRDT Semantics
- Operationβbased CRDTs (opβbased) for low overhead.
- Support for
AWMap(addβwin map) andLSeq(list with unique positions). - Merge of concurrent updates yields the same result on all replicas.
-
Delta Synchronization
- Transmit only the changes (deltas) between peers.
- Compress deltas when possible.
- Support for causal ordering (vector clocks).
-
Persistence
- Optional snapshotting to disk for crash recovery.
- Incremental log of operations for audit.
-
Integration with Transport
- Subscribe to specific keys or prefixes.
- Automatically propagate deltas via Mesh Transport.
- Handle network partitions gracefully.
- Merge latency: < 10 ms for typical map sizes (< 1000 entries).
- Memory overhead: < 2Γ the size of the stored data.
- Scalability: Support at least 10β―000 keys per agent.
- Concurrency: Allow concurrent reads and writes from multiple threads.
βββββββββββββββββββββββββββββββββββββββββββ
β State Sync Core β
βββββββββββββββ¬ββββββββββββββ¬ββββββββββββββ€
β CRDT β Delta β Persistenceβ
β Engine β Manager β Layer β
βββββββββββββββΌββββββββββββββΌββββββββββββββ€
β ConflictβFree Merge β
βββββββββββββββββββββββββββββββββββββββββββ€
β Transport Adapter β
βββββββββββββββββββββββββββββββββββββββββββ
- Implements the actual CRDT data structures.
- Provides
CrdtMapandCrdtSeqabstractions. - Exposes
apply_op(operation: Op)andmerge(other: State).
- Tracks local changes since the last synchronization.
- Generates compact deltas for transmission.
- Applies incoming deltas to the local state.
- Optional disk storage via
serdeandbincode. - Snapshots at configurable intervals.
- Recovery from snapshot + operation log.
- Listens for incoming delta messages from Mesh Transport.
- Sends deltas to peers that subscribe to relevant keys.
- Implements backβpressure when network is saturated.
pub type Key = String;
pub type Value = CrdtValue; // enum for supported types
pub enum CrdtValue {
Integer(i64),
Float(f64),
Text(String),
Map(CrdtMap),
Seq(CrdtSeq),
Boolean(bool),
Bytes(Vec<u8>),
}
pub struct CrdtMap {
inner: aw_map::AWMap<Key, Value>,
vclock: VectorClock,
}
pub struct Op {
pub id: OpId,
pub key: Key,
pub change: Change,
pub causal_deps: Vec<OpId>,
}
pub enum Change {
Set(Value),
Delete,
Increment(i64),
// ... other operations
}
pub struct Delta {
pub source: AgentId,
pub ops: Vec<Op>,
pub timestamp: Timestamp,
}- Each operation is assigned a unique ID (agent ID + logical timestamp).
- Operations are stored in a causalβorder DAG.
- When two states merge, the CRDT engine combines the DAGs and recomputes the resulting values according to the CRDT semantics (addβwins, lastβwriteβwins, etc.).
- The merge is deterministic and commutative.
- Create
crates/state-syncwithautomergeorcrdtsas dependency. - Implement a wrapper around
AWMapfromcrdtscrate. - Provide
get,set,deleteAPI. - Unit test merge of concurrent updates.
- Integrate with Mesh Transport: send deltas as broadcast or targeted messages.
- Implement subscription mechanism (keys of interest).
- Add vector clocks for causal consistency.
- Add snapshotting via
serde. - Write operation log to disk (optional).
- Benchmarks for merge performance.
- Support CRDT sequences (LSeq) for ordered lists.
- Support counters (PNβCounter).
- Support registers (LWWβRegister).
crdts(orautomergeβrs) for CRDT algorithmsserdefor serializationtokiofor async operationstracingfor logging
- Propertyβbased tests: Use
proptestto verify CRDT invariants (commutativity, associativity, idempotence). - Network simulation: Run multiple inβmemory agents that exchange deltas and verify eventual consistency.
- Fault injection: Simulate packet loss, duplication, and reordering.
- Should we use operationβbased or stateβbased CRDTs? Opβbased reduces bandwidth but requires reliable broadcast.
- How to handle garbage collection of old operations? Use dotβkernel approach?
- Should we support custom CRDT types defined by the user?