Reliable Sync
Syncraft Labs owns the client half of offline-first: optimistic writes, durable IndexedDB state, and an outbox journal. The other half is a server contract that can survive retries, partial failures, and long offline periods.
This guide describes the protocol Syncraft 0.9 expects. It is the same contract used by AgriTrack Pro in production.
The Sync Loop at a Glance
Section titled “The Sync Loop at a Glance”A correct round trip has four phases, always in this order:
- Push pending outbox entries.
- Acknowledge each entry individually.
- Reconcile local state with the server response (for example, apply the authoritative version returned by the server).
- Pull changes newer than the last cursor - only when the outbox is empty.
Skipping step 2 is what causes lost writes. Skipping the “outbox empty” guard on step 4 is what causes resurrected edits: a pull applied while local mutations are still queued can overwrite them before they are pushed.
Per-Entry Acknowledgements
Section titled “Per-Entry Acknowledgements”Since 0.9, pusher can return one OutboxAck per entry:
type OutboxAckStatus = "applied" | "conflict" | "rejected" | "retry";
interface OutboxAck { id: string; // OutboxEntry.id status: OutboxAckStatus; error?: { code: string; message: string; retryable?: boolean; };}| Status | Meaning | Syncraft behaviour |
|---|---|---|
applied |
The server committed the mutation. | Entry is removed from the outbox. |
conflict |
The server refused the mutation because its version is stale. | Entry is removed and reported through onPushResult. |
rejected |
Permanent refusal (validation, authorization, deleted entity). | Entry is removed and reported through onPushResult. |
retry |
The server could not commit the mutation right now. | Entry stays queued and the loop backs off exponentially. |
Two safety rules are built in:
- Missing acknowledgements are
retry. A partial response never silently deletes an unconfirmed mutation. voidis treated as “everything applied” for backwards compatibility, and logs a warning. Do not rely on it in production.
const { data, update, pendingCount } = useSync<ActivityState>("activities", { initialState: { activities: [] }, pusher: async (entries) => { const response = await fetch("/api/sync/push", { method: "POST", headers: { "Content-Type": "application/json", Accept: "application/json", }, body: JSON.stringify({ mutations: entries.map((entry) => ({ mutation_id: entry.id, patches: entry.patches, })), }), });
if (!response.ok) { // Throwing keeps every entry queued and triggers backoff. throw new Error(`Push failed with HTTP ${response.status}`); }
const { results } = await response.json(); return results; },});Server Contract: Push
Section titled “Server Contract: Push”The client sends the raw outbox entries. Your server decides what a mutation means, but it must be able to process the same mutation_id twice and return the same answer.
Request
Section titled “Request”{ "mutations": [ { "mutation_id": "8f14e45f-ea0a-4c8b-9f2a-6a1f0f2f9d10", "entity": "activities", "entity_id": "3d0f0b7e-7a1c-4c33-9a7e-2f5c1f6a8b21", "base_version": 4, "patches": [ { "op": "replace", "path": ["status"], "value": "completed" } ], "created_at": 1764000000000 } ]}mutation_id is the outbox entry id. It is stable across retries, which makes it the idempotency key.
Processing rules
Section titled “Processing rules”- One transaction per mutation. A mutation is applied completely or not at all.
- Idempotency first. If
mutation_idwas already processed, return the stored acknowledgement instead of applying the patches again. - Check the base version. If
base_versionno longer matches the entity version, do not apply the patch. Returnconflictwith the current server version so the client can pull it. - Validate before writing. Return
rejectedwith a stableerror.codefor permanent failures. - Ordering. Entries are pushed oldest-first. When two mutations target the same entity, apply them in the order received; reject the second with
conflictif itsbase_versionis stale. - Return one result per mutation. The response array may be in any order, but every
mutation_idshould appear exactly once.
Response
Section titled “Response”{ "results": [ { "id": "8f14e45f-ea0a-4c8b-9f2a-6a1f0f2f9d10", "status": "applied", "error": null }, { "id": "1c9a3d2b-5f4e-4a6b-8c7d-9e0f1a2b3c4d", "status": "conflict", "error": { "code": "version_conflict", "message": "Activity was modified on another device.", "retryable": false } } ]}Return HTTP 200 when the batch was processed, even if individual mutations were rejected. Reserve non-2xx responses for transport or server failures that should retry the whole batch.
Server Contract: Pull
Section titled “Server Contract: Pull”A pull returns everything that changed after the client’s cursor. Use a monotonic server-side sequence - a bigint journal id, not a client timestamp.
GET /api/sync/pull?cursor=1042{ "changes": [ { "cursor": 1043, "entity": "activities", "entity_id": "3d0f0b7e-7a1c-4c33-9a7e-2f5c1f6a8b21", "operation": "upsert", "version": 5, "payload": { "id": 91, "status": "completed", "version": 5 } } ], "next_cursor": 1043, "has_more": false}Rules that keep the journal correct:
- Append one row per committed mutation inside the same transaction that changes the entity.
- Never reuse or reorder cursor values. Clients treat the cursor as opaque.
- Page the response. Return
has_more: trueand let the client call again withnext_cursoruntil it isfalse. - Handle expired cursors. If
cursoris older than your retention window, returnreset_requiredplus a full snapshot. The client must replace its local state and restart from the new cursor.
On the client, apply the pulled snapshot through applyRemoteState() or refetch() - never through update(). update() appends to the outbox, which would echo server data back as a local mutation.
// Pull only after the outbox has drained.const pull = async (cursor: number) => { const response = await fetch(`/api/sync/pull?cursor=${cursor}`); const page = await response.json();
const nextState = mergeChanges(data, page.changes); await applyRemoteState(nextState); // server-authoritative, no outbox entry localStorage.setItem("sync:cursor", String(page.next_cursor));
if (page.has_more) { await pull(page.next_cursor); }};Conflict Policy: Server Wins
Section titled “Conflict Policy: Server Wins”Server-wins is the simplest policy that still converges: the server is the only writer of record, and clients replay their intent against it.
When the server returns conflict:
- The outbox entry is removed - retrying it would fail forever.
onPushResultreceives the acknowledgement.- The client pulls the authoritative entity and shows a notification.
const [conflicts, setConflicts] = useState<OutboxAck[]>([]);
useSync<ActivityState>("activities", { initialState: { activities: [] }, pusher: pushActivities, onPushResult: (results) => { const conflictsThisRound = results.filter( (result) => result.status === "conflict" || result.status === "rejected", ); if (conflictsThisRound.length > 0) { setConflicts((previous) => [...previous, ...conflictsThisRound]); } },});Render the conflict list as a non-blocking banner or toast. Do not silently discard it: from the user’s point of view, their edit was accepted locally and then disappeared.
Retry and Backoff
Section titled “Retry and Backoff”The background loop already handles retry scheduling:
retryacknowledgements keep entries in the outbox and schedule the next attempt with exponential backoff (1s,2s,4s, … capped at 30s).- A thrown
pushererror keeps the whole batch queued and backs off the same way. navigator.onLine === falsepauses the loop without consuming retries.- A non-retryable
SyncraftErrorhalts the queue until the next manualrefetch()or remount.
Your server should distinguish transient from permanent failures:
| Situation | Ack status | Why |
|---|---|---|
| Database timeout, lock contention | retry |
The same mutation can succeed later. |
Stale base_version |
conflict |
Retrying cannot fix it; the client must pull. |
| Validation error, forbidden action | rejected |
Retrying will never succeed. |
| Unknown entity id | rejected |
The entity was deleted or never existed. |
Compaction
Section titled “Compaction”Compaction merges consecutive mutations into a single entry before pushing. It reduces payload size, but it also destroys per-entry idempotency.
compaction: "none"(default) - push every entry. Required when the server acks entries individually.compaction: "merge"- push one merged entry. Only safe when the server applies the merged entry atomically and the merged entry keeps the original ids for acknowledgement.
If you enable merge, the server must return one acknowledgement for the compacted entry and Syncraft fans it out to every original id. A partial response is treated as retry for all of them.
Observable Status
Section titled “Observable Status”Expose the queue state in your UI so “saved” is never a guess:
const { pendingCount, isSyncing, isOffline, error } = useSync<ActivityState>( "activities", options,);| Signal | Use it for |
|---|---|
pendingCount |
“3 changes waiting to sync” badge |
isSyncing |
Spinner while a push or pull is running |
isOffline |
Offline banner |
error |
Last transport or store error |
onPushResult |
Per-entry conflicts and rejections |
pendingCount updates after local writes, acknowledgements, purge(), and cross-tab broadcasts, so it stays correct when the user has the app open in two tabs.
Reference Implementation
Section titled “Reference Implementation”A complete loop with React:
function useActivitySync() { const cursorRef = useRef(Number(localStorage.getItem("sync:cursor") ?? 0));
const sync = useSync<ActivityState>("activities", { initialState: { activities: [] }, pusher: pushActivities, onSyncSuccess: () => { // Push finished; pull when the outbox is empty. }, onPushResult: reportConflicts, });
useEffect(() => { if (sync.pendingCount !== 0 || sync.isSyncing || sync.isOffline) return;
let cancelled = false; void (async () => { const page = await fetchChanges(cursorRef.current); if (cancelled) return; await sync.applyRemoteState(mergeChanges(sync.data, page.changes)); cursorRef.current = page.next_cursor; localStorage.setItem("sync:cursor", String(page.next_cursor)); })();
return () => { cancelled = true; }; }, [sync.pendingCount, sync.isSyncing, sync.isOffline]);
return sync;}The important detail is the guard: the effect only pulls when pendingCount === 0. That is the client-side half of the convergence rule.
Production Checklist
Section titled “Production Checklist”-
pusherreturnsOutboxAck[], notvoid. - Every mutation has a stable
mutation_idequal to the outbox entryid. - The server stores processed mutation ids and replays the original result.
- Each mutation is applied in a single transaction with its journal row.
- Pull uses a monotonic cursor and supports
has_more. - Expired cursors return
reset_requiredand a full snapshot. - The client pulls only when
pendingCountis0. -
conflictandrejectedsurface in the UI instead of disappearing. -
retrystays queued and backs off. - Local data is purged on logout and account switch (
purge()). -
schemaVersionis bumped andmigrateis provided whenever the persisted shape changes.
