Slipstream
You have config in NATS JetStream: routing tables, TLS certs, WASM configs. Edge nodes need a local copy, kept in sync, that survives restarts without replaying the full stream.
Slipstream materializes a NATS JetStream KV bucket into a local fold on each consumer. A watch cursor (a stream sequence number) tracks position in the change stream; on restart, only the delta since the last checkpoint arrives from NATS.
NATS is a bounded log. Entries are evicted past max_bytes and max_age. Once retention compacts past a cursor, there is no replay path from NATS. The local fold is the durable state; folds across the fleet are the only full replicas.
NATS JetStream KV
┌──────────────────────────────────────────────────────┐
│ [evicted] ◄──── seq 998 seq 999 seq 1000 ──────► │ max_bytes / max_age
└───────────────────────────┬──────────────────────────┘
│ KvUpdate stream
▼
watch_applied()
┌─────────────────────┐
│ parse │
│ apply() │ ← your domain logic
│ cursor = seq 1000 │ advances after apply() returns
└──────────┬──────────┘
│
┌──────────▼──────────┐
│ local fold │ folds are the only full replicas
│ cursor = 1000 │ once NATS evicts past cursor
└──────────┬──────────┘
┌───────────────┴──────────────────────┐
│ │
▼ ▼
restart new node / cursor expired
resume from cursor = 1000 export fold to object storage
NATS delivers seq 1001+ only import + resume from embedded cursor
The cursor advances after apply() returns, not on receipt. A crash between delivery and application re-delivers on the next start instead of silently skipping. watch_applied enforces this invariant.
For folds that outgrow RAM, fjall (pure Rust) and rocksdb backends hold state on disk.
Install
[]
= "0.5"
On-disk snapshot backends are opt-in cargo features:
= { = "0.5", = ["fjall"] } # pure-Rust LSM, no C toolchain
= { = "0.5", = ["rocksdb"] } # RocksDB (requires C++ toolchain + libclang)
= { = "0.5", = ["transport"] } # export/import via object_store (S3, GCS, local)
Concepts
| Term | What It Is |
|---|---|
Connection |
NATS connection lifecycle + store factory |
KvStore |
Named bucket. Vends reader, watcher, writer |
KvReader |
Point-in-time reads: get, entry, keys, scan |
KvWatcher |
Live update stream via channel |
KvWriter |
Write, soft-delete, CAS (create, update, delete_with_version) |
WatchCursor |
Opaque position in a watch stream. Save it; pass it back on reconnect |
VersionToken |
Opaque version — NATS: u64 revision; FDB: 10-byte versionstamp |
KvEntry |
One key + value + version from a read |
KvUpdate |
One watch event: Put, Delete, or Purge |
Snapshot |
Deduplicated KV state + cursor at a point in time. Disk cache, not source of truth |
SnapshotWriter |
Append-only log of KvUpdates; survives restarts without a full NATS scan |
SnapshotStore |
Trait: the durable-fold contract — apply (data + cursor, atomically), load, get, range |
AppendLogSnapshot |
Default SnapshotStore: the append-only log + an in-RAM fold (pure-Rust, small state) |
FjallSnapshot |
On-disk SnapshotStore for folds too large for RAM; queryable (feature = "fjall") |
RocksDbSnapshot |
Same contract on RocksDB, for consumers who prefer the C++ LSM (feature = "rocksdb") |
watch_applied |
Watch loop that advances the cursor only after your apply returns, folding into any SnapshotStore |
ConnectionCapabilities |
Feature flags for runtime branching (CAS, streaming watch, global ordering) |
Usage
Connect
use ;
let conn = new;
conn.connect.await?;
Open a store
use ;
use Duration;
let store = conn.store_with_config.await?;
max_bytes is required on Synadia Cloud. Omit only for self-hosted NATS.
Read
use KvReader;
let reader = store.reader;
// Single key — filters tombstones; use entry() to include them for CAS
if let Some = reader.get.await?
// All entries under prefix
// Uses DeliverPolicy::LastPerSubject: one NATS consumer, not N round-trips.
let entries = reader.scan.await?;
// Key names only (no value transfer)
let keys = reader.keys.await?;
Write
use KvWriter;
let writer = store.writer.expect;
// Unconditional write
let version = writer.put.await?;
// Create only. Returns AlreadyExists if key has a live value.
let version = writer.create.await?;
// CAS update. Returns RevisionMismatch if version doesn't match.
let new_version = writer.update.await?;
// CAS delete. Returns RevisionMismatch on conflict.
writer.delete_with_version.await?;
// Best-effort delete — returns Ok(true) even if key didn't exist.
writer.delete.await?;
Watch
use ;
let watcher = store.watcher.expect;
let = channel;
// Watches are state-sync streams: the current value of every matching key is
// delivered first (as puts), then live updates. No separate scan needed — and
// no scan-to-watch race window.
//
// watch_all blocks until the stream ends — run it in a separate task
spawn;
while let Some = rx.recv.await
Dropping rx cancels the watch. The watcher task exits and unsubscribes automatically.
Resumable watch
The cursor is a sequence number. Persist it; pass it back on reconnect. NATS delivers only the delta since that position.
let cursor = load_cursor.unwrap_or;
match watcher.watch_all_from.await
watch_prefix_from() works the same way for prefix-filtered streams, and
watch_prefixes_from() resumes the union of several prefixes on one
multi-filter consumer.
Snapshot
For services that cache KV state locally, the snapshot persists both state and cursor to disk. On restart, load the snapshot and resume the watch from its cursor — only the delta since the last checkpoint arrives from NATS.
Startup
use snapshot;
if let Some = load? else
Runtime
use SnapshotWriter;
let mut snap = open?;
while let Some = rx.recv.await
This loop has a trap: current_cursor must track what cache.apply() has consumed, not what rx.recv() delivered. Get it wrong and a crash skips updates on resume. watch_applied runs this loop for you with that invariant enforced.
The snapshot is a cache. Delete it and the service falls back to full replay on next start.
File format
Header: b"PGSS" ++ version:u16le
Record: crc32:u32le ++ type:u8 ++ payload
| Record type | Byte | Payload |
|---|---|---|
REC_PUT |
0x01 | key_len:u16le ++ key ++ value_len:u32le ++ value ++ ver_len:u8 ++ version_bytes |
REC_DELETE |
0x02 | key_len:u16le ++ key ++ ver_len:u8 ++ version_bytes |
REC_CURSOR |
0x03 | cursor_len:u8 ++ cursor bytes |
version_bytes is the raw [VersionToken] bytes (≤10), not a fixed u64, so NATS revisions (8 bytes) and FDB versionstamps (10 bytes) both round-trip intact.
A truncated final record (crash mid-write) is discarded; earlier records are intact. A CRC failure mid-file returns SnapshotError::Corrupted.
Pluggable backends
The durable fold is a trait, [SnapshotStore], so a consumer picks where its fold lives. The contract is small — apply a batch and advance the cursor atomically, resume from the cursor on restart, and query the result:
Every backend keeps the same invariants: the fold is a pure function of the log (delete the store, replay from the cursor, get identical state), the cursor never names a revision whose data isn't durable (cursor-after-apply), and the store is a cache — a tail lost to power loss is rebuilt by resuming the watch.
| Backend | When | Notes |
|---|---|---|
AppendLogSnapshot |
Default. Fold fits in RAM (edge/tunnel-style services) | Pure-Rust, the append-only log above plus an in-RAM map serving get/range. No extra dependencies. |
FjallSnapshot |
Fold too large for RAM (e.g. routing at ~1B keys) | On-disk fjall LSM, feature = "fjall". Pure-Rust. Each apply is one atomic batch (data and cursor); durability (NO_SYNC vs fsync) is configurable. |
RocksDbSnapshot |
Same as FjallSnapshot, preferring the battle-tested C++ LSM and its tooling (ldb, sst_dump) |
On-disk RocksDB, feature = "rocksdb". Each apply is one atomic WriteBatch (data and cursor); WAL always on, per-commit fsync configurable. Tuned for billion-key route folds (hit-optimized ribbon filters, partitioned index, zstd bottommost, batched multi_get). Builds C++ (needs a toolchain + libclang). |
Pick a backend, then hand it to watch_applied — load returns the resume cursor alongside the store:
use ;
// Default in-RAM backend:
let = load?;
// Or, behind `feature = "fjall"`, an on-disk fold for a large consumer:
// let (resume, store) = FjallSnapshot::open(dir, FjallConfig { sync: false, ..Default::default() })?;
// Or the same on RocksDB, behind `feature = "rocksdb"`:
// let (resume, store) = RocksDbSnapshot::open(dir, RocksDbConfig { sync: false, ..Default::default() })?;
let final_cursor = watch_applied.await?;
The trait stops at durable fold + cursor + query. Serving structures built from the fold (routing rings, hashrings, indexes) live in the consumer — query them out of the store with get/range. A consumer with a different engine can implement SnapshotStore itself; the rest of slipstream is unchanged.
Applied watch
watch_applied drives the watch-batch-apply-checkpoint loop and enforces one rule the hand-rolled version can't: the cursor advances only after your apply returns, never on receipt. It is generic over the SnapshotStore backend, so the consumer chooses where the durable fold lives (or None to run without persistence).
use ;
let final_cursor = watch_applied.await?;
A batch closes when window elapses or it hits max updates, whichever comes first. Then, in order: apply(batch) runs to completion, the cursor advances to the batch's highest revision, the batch + cursor are folded into the store atomically (on a blocking task), and on_applied fires.
Persist the cursor on receipt instead and a crash between receive and apply loses data: the cursor reads "caught up to rev N" while rev N sits in an unapplied buffer, and the next resume starts past it. watch_applied checkpoints at the applied cursor, so a persisted cursor always means every update up to it has been applied.
parsereturningNone(corrupt bytes, irrelevant key) still advances the cursor — nothing to apply means nothing to skip.- On
CursorExpired, it falls back to a full watch automatically. With areaderwired, it first diffs the fold against the bucket's live keys and applies synthetic deletes for keys that vanished during the gap (their delete markers were evicted with the cursor) — the one case the fallback re-list can't cover. - It returns the final applied cursor on shutdown or stream close.
apply runs inline. If it panics, the panic aborts the watch.
NATS mapping
| Concept | NATS primitive |
|---|---|
| Store | JetStream KV bucket (KV_{name} stream) |
VersionToken |
Per-key revision (u64, big-endian) |
WatchCursor |
NATS revision at last checkpoint |
delete() |
Writes empty value (soft delete). Always returns Ok(true) |
KvUpdate::Purge |
Hard delete: all history removed from stream |
scan() |
DeliverPolicy::LastPerSubject: one entry per key, one consumer |
watch_*() |
DeliverPolicy::LastPerSubject: current state, then live updates |
watch_prefix() |
Native NATS subject filter ({prefix}> wildcard) |
Feature detection
let caps = conn.capabilities;
if caps.cas
if caps.streaming_watch
if caps.prefix_watch
if caps.global_ordering
Errors
| Error | Cause | Recovery |
|---|---|---|
NotConnected |
Operation before connect() |
Call connect() |
AlreadyExists |
create() on a live key |
Read current state, decide |
RevisionMismatch |
CAS conflict on update() / delete_with_version() |
Re-read, retry |
CursorExpired |
watch_*_from() cursor compacted by NATS |
Fall back to watch_all() |
WatchError |
NATS stream dropped | Re-subscribe |
Credentials
Priority order, first match wins:
creds: base64-encoded.credscontent (containers, ECS)creds_file: path to.credson disk (bare-metal, local dev)- URL-embedded
user:pass@host - No auth