Sync / Replicated state
When things go wrong
| Scenario | Behavior |
|---|---|
| Offline | Edits apply and save locally; pending grows; retries on backoff |
| Server rejects edit | Dropped from outbox; rest replayed; UI reverts |
| Local storage write fails | Edit reverted; onPersistenceFailure updates UI |
| Concurrent edit on server | Base advances; local pending edits rebase on top |
| Retried operation | opId deduplicated from journal history |
| Replica far behind | Catches up via more pages or compacted checkpoint |
| Server history wiped | New epoch resets replica from 0 and resends outbox |
| Actor mismatch on replica | Durable rejects operations from another actor |
| Browser storage evicted | Replica syncs from server; unsent outbox edits lost |
| Two tabs share one storage | CAS fails stale writer instead of corrupting history |
| Unknown storage version | Fails loudly preserving bytes for migration |
| Old message format | Upcast via declared migrations |
| Malformed server answer | Fails exchange; local outbox preserved |
| Crash after external effect | Provider call repeats with same key via effect ledger |
When a server lost its history (epoch changed), a replica that never observed an epoch fails until storage is cleared. For external side effects (emails, charges), pair Durable's runEffect/recover ledger with provider idempotency keys; see external effects.
Deployment responsibilities you keep
- Authentication: Validate tokens/cookies per socket connection and close expired connections.
- Compaction and retention: Schedule
compactandvacuum, and reject operations older than retained deduplication windows. - Effect idempotency keys: Provide stable semantic keys to external providers.
- Parked reactions: Monitor and manually resolve reactions parked after repeated permanent failures.
- Schema migrations: Author
migrationsfor durable Messages and coordinatesharedsnapshot upgrades. - Storage eviction: Request persistent browser storage and warn users before clearing site data with unsent edits.
Using Sync or Durable on their own
- Sync without Durable: Implement the exchange contract (cursor validation, deterministic rejections, deduplicated appends, pagination/checkpoints, and epochs) on any server.
- Durable without Sync: Supply your own operation and snapshot codecs, empty state, reducer, and ID extractor; see the Durable README.
When to use another tool
- Online-only state without multi-device offline sync: plain Foldkit or
foldkit-remote. - Leaderless peer-to-peer merging across every node without a server: a CRDT (
Sync.lww.registerhandles per-field LWW within Sync). - Arbitrary ad-hoc SQL querying: store and query tables with Drizzle and Remote; Durable is an operation log.
Next
- Explore working apps in
examples/todo-app,examples/sync, andexamples/cloudflare. - Edit large SQL tables offline via a journal in Editing server data.
- Read the package APIs in
foldkit-syncandfoldkit-durable.