SwarmKeyDb now supports an offline-first execution path for Bee/Swarm-backed deployments.
OfflineCapableKeyValueStore queues put and delete operations when the backend is unreachable.OfflineSyncService replays queued operations in order when connectivity returns.IConnectivityProbe plus HttpHealthConnectivityProbe poll the Bee /health endpoint.InMemoryOfflineJournal and SqliteOfflineJournal support ephemeral or restart-safe buffering.SwarmKeyDbOptions.OnConflict allows applications to override the default local-wins replay when queued data conflicts with remote state./health, /ready, /metrics, and /dashboard expose offline queue depth and last sync status.App / Redis client
|
v
OfflineCapableKeyValueStore
| local cache (serves reads during outages)
| offline journal (memory/sqlite)
v
Crdt / compression / encryption decorators
v
Swarm / Bee backend
OfflineSyncService ---> IConnectivityProbe (/health)
|
+--> replays queued operations in sequence order
+--> invokes OnConflict(local, remote) when both sides changed
using SwarmKeyDb;
var remoteStore = new SwarmKeyValueStore(new BeeSwarmClient(beeUrl, postageBatchId), new FileKeyIndex("data/index.json"));
var offlineStore = new OfflineCapableKeyValueStore(
remoteStore,
new SqliteOfflineJournal("data/offline-journal.sqlite"),
new HttpHealthConnectivityProbe(beeUrl),
new SwarmKeyDbOptions
{
OfflineMode = OfflineMode.Auto,
OfflineJournal = OfflineJournalType.Sqlite,
OfflineSyncIntervalMs = 2_000,
OnConflict = context => context.LocalValue
});
var sync = new OfflineSyncService(offlineStore, TimeSpan.FromSeconds(2));
await sync.StartAsync();
var write = await offlineStore.PutWithResultAsync("user:profile", "{\"name\":\"Ada\"}"u8.ToArray());
Console.WriteLine($"queued={write.Queued} depth={write.QueueDepth}");
var read = await offlineStore.GetWithResultAsync("user:profile");
Console.WriteLine($"fromCache={read.FromCache} cachedAt={read.CachedAt}");
During replay, queued writes are compared with the current remote value:
SwarmKeyDbOptions.OnConflict is invoked with both payloadsBecause replay still writes through the CRDT-enabled store pipeline, existing merge envelopes and key-specific merge strategies continue to apply.
| Variable | Values | Default | Purpose |
|---|---|---|---|
SWARM_KEYDB_OFFLINE_MODE |
never, auto, always |
never |
Enables offline-first behavior. |
SWARM_KEYDB_OFFLINE_JOURNAL |
memory, sqlite |
memory |
Selects the local operation journal implementation. |
SWARM_KEYDB_OFFLINE_SYNC_INTERVAL_MS |
integer | 5000 |
Poll interval for background replay attempts. |
When sqlite mode is selected, the server stores the journal in <data-dir>/offline-journal.sqlite.
GET /health and GET /ready include offline_queue_depth and last_successful_sync_utcGET /metrics exposes swarmkeydb_offline_queue_depth and swarmkeydb_offline_last_sync_unix_timeGET /dashboard shows the offline queue depth and last successful sync timestampSWARM_KEYDB_OFFLINE_MODE=auto.SET/DEL commands; the server journals them locally.OfflineSyncService to drain the queue and verify /health shows offline_queue_depth: 0.