Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
68939e5
feat(offline-transactions): add durable follower writes
daveycodez Nov 23, 2026
1684318
fix(offline-transactions): address durable follower write review
daveycodez Nov 24, 2026
7a3c3bd
fix(offline-transactions): address second durable follower write review
daveycodez Nov 24, 2026
c84758e
fix(offline-transactions): address third durable follower write review
daveycodez Nov 24, 2026
770284a
fix(offline-transactions): keep failed exclusions out of admission
daveycodez Nov 24, 2026
c89e7eb
fix(offline-transactions): address fourth durable follower write review
daveycodez Nov 25, 2026
eb6e5e3
fix(offline-transactions): address fifth durable follower write review
daveycodez Nov 25, 2026
25b5e5d
fix(offline-transactions): address sixth durable follower write review
daveycodez Nov 25, 2026
a6927b0
fix(offline-transactions): address seventh durable follower write review
daveycodez Nov 25, 2026
8d35088
fix(offline-transactions): address eighth durable follower write review
daveycodez Nov 25, 2026
d92677d
fix(offline-transactions): address ninth durable follower write review
daveycodez Nov 25, 2026
5ec1262
fix(offline-transactions): address tenth durable follower write review
daveycodez Nov 25, 2026
1b086c2
fix(offline-transactions): address eleventh durable follower write re…
daveycodez Nov 25, 2026
e693616
fix(offline-transactions): address twelfth durable follower write review
daveycodez Nov 26, 2026
d0dda25
fix(offline-transactions): address thirteenth durable follower write …
daveycodez Nov 27, 2026
6d27300
fix(offline-transactions): address fourteenth durable follower write …
daveycodez Nov 27, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/durable-follower-writes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/offline-transactions': minor
---

Add `followerWrites: 'durable'`. A follower tab can now save a write to the shared outbox, so it survives going offline or closing the tab. The current or next leader sends it and reports the result back over BroadcastChannel. The leader also records a rejection before removing the row, so a follower that misses the report still fails with the server's error. A writer deletes its record when it reads it, and a leader's replay prunes records older than seven days. The default stays `'online'`. `removeFromOutbox()` and `clearOutbox()` now also stop a removed transaction that has not started and fail its writer's transaction; in a durable follower they ask the leader, so a call that already started keeps the server's outcome.
28 changes: 28 additions & 0 deletions docs/contributing/oracle-coverage.md
Original file line number Diff line number Diff line change
Expand Up @@ -4245,6 +4245,34 @@ The post-merge review added three missing domains to existing owners:
holds real storage-read delivery across successful and permanently rejected
durable removals, with bounded scans, concurrent loads, and unfinished peers.
This is distinct from exactly-once execution across independent owners.
- [Durable follower writes](https://github.com/TanStack/db/blob/main/packages/offline-transactions/tests/durable-follower-oracle.property.test.ts)
checks delivery and settlement of `followerWrites: 'durable'` across three
tabs that write, close, reopen and hand over leadership, with controlled
election, connectivity and BroadcastChannel. It also checks that only the
leader sends a durable write, that a lost settlement (including a rejection)
is recovered for the same write, that a leader retries a saved write it could
not read, that a stale admission read cannot send a write twice, that a write
made before initialization takes the online path, and which rejection
records remain. It also covers a failed row removal after a successful send,
an undecodable saved row, a follower's failed outbox check, an admission read
across a change of leadership, and a `beforeRetry` exclusion whose settlement
is lost. It also covers a failed rejection record, a late admission during an
exclusion, a write removed before it was sent, a save acknowledged after a
handover, a faulted former leader that saves for its successor, a failed
start, a save that ends after its tab closed, and tabs that name their
collections differently under one key. It also covers a failed replay
that runs again on connectivity, a replay that outlives its term,
overlapping removals of a running write, a rejection recorded after its
writer listed the outcome records, unreadable outcome records and
decisions, and a removed running write whose call fails with a retryable
error. It also covers an added message while an unsent write's removal is
being decided, a removed running write whose error record fails before its
leader stops leading, a connectivity change during an outbox check that
then fails, a failed read of a sent row's phase during its removal, a
readable decision beside an unreadable one, and a removed running write
whose call fails retryably before the removal's deletion lands or between
the failure's read and rewrite of the row. It does not cover real browser
contexts.
- Settlement-time drop of optimistic state is owned by the
[optimistic history model](https://github.com/TanStack/db/blob/main/packages/db/tests/optimistic-history-oracle.ts)
and
Expand Down
4 changes: 3 additions & 1 deletion docs/guides/offline-transactions.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ Register `saveBatch` in `mutationFns` before you use this example. `commit()` ca

On the web, the executor tests IndexedDB first. It uses localStorage if IndexedDB is unavailable. You can pass a `storage` adapter to choose a different store.

One tab leads outbox processing. The executor prefers Web Locks and then BroadcastChannel for leader election. A follower uses the normal online mutation path without an outbox entry. The same online-only path applies when durable storage is unavailable.
One tab leads outbox processing. The executor prefers Web Locks and then BroadcastChannel for leader election. By default, a follower uses the normal online mutation path without an outbox entry; with `followerWrites: 'durable'`, an initialized follower saves to the shared outbox instead, as described below. The same online-only path applies when durable storage is unavailable.

If neither election API exists, the current fallback treats the tab as the leader. It does not coordinate multiple tabs.

Expand All @@ -189,6 +189,8 @@ console.log(executor.storageDiagnostic)
console.log(executor.isOfflineEnabled)
```

Set `followerWrites: 'durable'` to keep a follower's writes when it goes offline or closes. The follower saves each write to the shared outbox, and the current or next leader sends it. A write whose tab closes before it can tell the current leader about the save waits for the next leader's replay. The follower's transaction settles with the leader's result. This requires BroadcastChannel: a follower without it uses the online path, while a tab that leads still saves its own writes to the outbox. Every tab that shares the outbox must set the same `followerWrites` mode: a default-mode tab opens no channel, so as leader it never hears a follower's saved write, and it records no outcome for a write it rejects or removes. A follower's write made before `waitForInit()` resolves also uses the online path, while a tab that already leads, for example in `onLeadershipChange(true)` during startup, waits for initialization instead; await `waitForInit()` before writing. A reloaded follower does not show another tab's pending writes optimistically until they sync. The leader records a rejection before it removes the row, so a follower that misses the report still fails with the server's error; a write with no record completed. A rejection record stays until its tab reads it; a leader's replay prunes records older than seven days, and a write saved longer ago than that keeps waiting instead of reading a missing record as success. Every tab must register its collections under the same keys, which also name the channel the tabs share; collection ids may differ. Every tab must also use the same outbox store: the channel does not include the store, and a removal reaches only the leader's store, so a write saved to another store stays pending there even after its removal resolves. Without `storage`, each tab probes on its own and falls back to localStorage when IndexedDB is unavailable; pass the same `storage`, such as `new IndexedDBAdapter()`, in every tab to rule that out. A leader that cannot read a follower's saved write reports it and keeps the write pending, and a leader whose outbox replay failed replays again when connectivity changes. Removing a saved write with `removeFromOutbox()` or `clearOutbox()` before it is sent fails its writer's transaction. A follower asks the leader to make the removal, because only the leader knows whether the server call started; a call that already started keeps the server's outcome, while a write waiting to retry after a retryable failure fails with the removal error. An executor that replaces a disposed one over the same Collection objects also settles the writes its predecessor saved when another tab settled them meanwhile. Without BroadcastChannel, and in the default mode, a tab that saved a write as leader and lost leadership before it was sent learns no outcome from the outbox, so its transaction keeps waiting.

Use `onStorageFailure` when the built-in web stores are unavailable. Use `onLeadershipChange` when the UI needs to show which tab owns the outbox. A follower can become the leader after the current leader leaves.

## React Native and Expo
Expand Down
1 change: 1 addition & 0 deletions packages/db/tests/oracle-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ const staticOracleProperties = [
`offline.executor-connectivity-barrier`,
`offline.executor-connectivity-query-db`,
`offline.executor-environment`,
`offline.durable-follower-writes`,
`offline.fifo-retry`,
`offline.leadership.serial-work`,
`offline.leadership.report`,
Expand Down
5 changes: 5 additions & 0 deletions packages/db/tests/oracle-replay-manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1791,6 +1791,11 @@ const ownerGroups: ReadonlyArray<
`executor-environment`,
`offline-transactions/tests/executor-environment-oracle.ts`,
],
[
`offline-transactions/tests/durable-follower-oracle.property.test.ts`,
`offline`,
`durable-follower-writes`,
],
[
`offline-transactions/tests/fifo-retry-oracle.property.test.ts`,
`offline`,
Expand Down
9 changes: 5 additions & 4 deletions packages/offline-transactions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ An optimistic change does not prove that the outbox write succeeded. Handle tran
Only one tab acts as the "leader" to safely manage the outbox:

- **Leader tab**: Full offline support with outbox persistence
- **Non-leader tabs**: Online-only mode for safety
- **Non-leader tabs**: Online-only mode by default, or a shared durable outbox with `followerWrites: 'durable'`
- **Leadership transfer**: Automatic failover when leader tab closes

### FIFO Sequential Processing
Expand Down Expand Up @@ -138,6 +138,7 @@ interface OfflineConfig {
onUnknownMutationFn?: (name: string, tx: OfflineTransaction) => void
onLeadershipChange?: (isLeader: boolean) => void
onlineDetector?: OnlineDetector
followerWrites?: 'online' | 'durable'
}
```

Expand Down Expand Up @@ -217,14 +218,14 @@ mutation function after restart; an unmarked row may replay.

#### Properties

- `isOfflineEnabled: boolean` - Whether this tab can persist offline transactions
- `isOfflineEnabled: boolean` - Whether this tab leads and processes the outbox. With `followerWrites: 'durable'`, an initialized follower can also save writes while this is `false`

#### Methods

- `createOfflineTransaction(options)` - Create a manual offline transaction
- `waitForTransactionCompletion(id)` - Wait for a specific transaction to complete. Call it before the transaction settles: a waiter for a transaction that this executor recently settled (one of its last 1,000) rejects. A waiter for an older settled id keeps waiting. A waiter for an id the executor does not know yet keeps waiting, because another tab can still queue that transaction.
- `removeFromOutbox(id)` - Manually remove transaction from outbox
- `clearOutbox()` - Manually remove all outbox transactions
- `removeFromOutbox(id)` - Manually remove transaction from outbox. A removed transaction that has not started does not run, and its writer's transaction fails, also in another tab with `followerWrites: 'durable'`
- `clearOutbox()` - Manually remove all outbox transactions, with the same effect on each
- `peekOutbox()` - View all pending transactions
- `dispose()` - Stop new processing and release executor resources. Pending durable transactions and optimistic state on surviving Collections remain live.

Expand Down
Loading