← Back to the blog

Rust • 2026-11-05 • 3 min read

An Offline-First SQLite Cache in Rust, and the Sync Protocol Around It

Offline-first is a protocol problem, not a storage problem. The schema, the outbound queue and the conflict rules we settled on, and the ones we got wrong first.

An Offline-First SQLite Cache in Rust, and the Sync Protocol Around It — Rust

"Works offline" is easy to demo and hard to finish. Reading from a local database is the simple half. The half that takes the time is deciding what happens when two devices and a server all believe different things.

The local shape

One SQLite file per account, opened through rusqlite from the shared Rust core described in the UniFFI article. Two kinds of table.

  • Mirrors of server state, each row carrying the server's version and the time it was fetched
  • An outbox of local mutations that have not been acknowledged yet, in order, each with a client-generated id

The outbox is what makes the thing work. A local change is written to the mirror *and* appended to the outbox in one transaction, so the UI reads its own write immediately and the sync layer has an exact, ordered record of what still owes the server.

// sql
CREATE TABLE outbox (
    id           TEXT PRIMARY KEY,   -- client-generated, also the idempotency key
    entity       TEXT NOT NULL,
    entity_id    TEXT NOT NULL,
    operation    TEXT NOT NULL CHECK (operation IN ('create','update','delete')),
    payload      BLOB NOT NULL,
    base_version INTEGER,            -- server version the change was made against
    created_at   INTEGER NOT NULL,
    attempts     INTEGER NOT NULL DEFAULT 0
) STRICT;

STRICT is worth the two keystrokes: without it SQLite will happily store a string in an integer column, and the bug surfaces months later on one device.

Settings that are not optional

  • PRAGMA journal_mode = WAL so a sync write does not block a read on the UI thread
  • PRAGMA foreign_keys = ON — off by default, per connection, every time
  • PRAGMA synchronous = NORMAL under WAL: durable enough for a cache, far cheaper than FULL
  • busy_timeout set to something non-zero, or you will meet SQLITE_BUSY in production

Sending the outbox

Entries go up in order, one at a time, with the entry id as an idempotency key. The server stores that key with the result, so a retry after a timeout returns the original outcome rather than creating a second record. This is the same pattern as our idempotent n8n webhooks, and it is the thing that makes "retry forever" a safe policy.

Backoff is exponential with jitter, capped at a few minutes. Without jitter, every device that lost connectivity during the same outage reconnects in the same second.

Conflicts

We tried last-write-wins first. It is one line of code and it silently destroys user data, which you discover from a support ticket rather than from a test.

What we settled on: the client sends base_version, and the server rejects the write with a 409 if the row has moved on. The client then re-fetches, and the resolution depends on the field. Fields that are a user's own notes merge; fields that are a state machine do not and surface a choice in the UI. There are far fewer of the second kind than you expect once you enumerate them.

Deletes and clocks

A delete is a tombstone with a timestamp, kept long enough to outlive any plausible offline period, then collected. Device clocks are advisory — they are wrong, sometimes by years — so ordering is decided by server version numbers and the tombstone's timestamp is only used for garbage collection, never for a comparison that decides who wins.

Read next

COOKIES // MEASUREMENT

We count page views through Google Tag Manager. Say yes and it may store a cookie in your browser; say no and it still counts the visit, without storing anything and without an identifier. Nothing here is used for advertising.

Cookie Policy — you can change this at any time from the footer of any page.