# IndexedDB Transactions and Storage Quotas vs Local-First Sync & CRDTs

- **Canonical URL:** https://frontend.rocks/vs/indexeddb-transactions-quotas-vs-local-first-sync-crdt/
- **Relation:** Contrasts With
- **Last Verified:** 2026-11-15
- **Verdict:** IndexedDB persists single-device B-tree records, whereas CRDTs merge concurrent multi-device edits deterministically.

## Comparison Matrix

| Dimension | IndexedDB Transactions and Storage Quotas | Local-First Sync & CRDTs |
| :--- | :--- | :--- |
| **Maturity** | Baseline · Widely | Experimental |
| **Domain** | Web APIs | State & Architecture |
| **Mental Model** | Transactional client-side B-tree object store whose transactions auto-commit on microtask drain and whose storage limits depend on origin-wide disk quotas. | Persists every edit locally in IndexedDB/OPFS at 0 ms network wait and uses Conflict-free Replicated Data Types (CRDTs) so offline peers merge deterministically. |
| **Prerequisites** | Event Loop, Macrotasks, and Microtask Queue (IDB transaction auto-commit is tied directly to microtask queue exhaustion.) | Service Worker Lifecycle (Offline-capable local-first apps pair IndexedDB persistence with a Service Worker shell.) |
| **When Not to Use** | When not to use IndexedDB: For sequential binary file streaming, SQLite page files, or append-only logs, use OPFS instead of serializing chunks into B-tree records; for multi-tab mutexes or offline conflict resolution, pair it with BroadcastChannel & Web Locks and Local-First Sync & CRDTs. | When not to use CRDTs: For strict financial ledgers, inventory counts, or unique-username claims where two offline writes cannot both succeed, use server-authoritative transactions instead. |

## IndexedDB Transactions and Storage Quotas — Under the Hood
An IndexedDB transaction behaves like a bank vault's dead-man switch: the door locks shut the instant you stop feeding it requests, even if you only stepped away to check the network.

1. **Scope Locking**: Multiple `readonly` transactions execute concurrently; a `readwrite` transaction blocks overlapping `readwrite` transactions on the same object store.
2. **Microtask Auto-Commit**: After `IDBRequest.onsuccess` fires, if no new request is queued before microtasks drain, the engine commits the transaction automatically.
3. **Quota & Eviction**: Browsers allocate a percentage of free disk per origin (`navigator.storage.estimate()`) and evict `best-effort` origins under disk pressure unless granted `navigator.storage.persist()`.

```js
// Safe pattern: resolve async I/O BEFORE opening the transaction
const payload = await fetch('/api/doc/42').then((r) => r.json());
const tx = db.transaction('docs', 'readwrite');
const store = tx.objectStore('docs');
store.put(payload);
await new Promise((resolve, reject) => {
  tx.oncomplete = () => resolve();
  tx.onerror = () => reject(tx.error);
});
```

## Local-First Sync & CRDTs — Under the Hood
Unlike [Optimistic UI](/concepts/optimistic-ui-rollback), where the server remains judge and jury and failed mutations roll back, a CRDT works like an append-only ledger with mathematical commutativity (`merge(A, B) === merge(B, A)`). Paired with a [Service Worker](/concepts/service-worker-lifecycle) for offline asset booting, the client never blocks on a round-trip.

1. **Local persistence**: Edits commit immediately to IndexedDB or Origin Private File System (OPFS) in sub-millisecond transactions.
2. **Causal IDs**: Each character or map key receives a unique Lamport/Hybrid Logical Clock `(clock, clientId)` tuple, immune to skewed laptop system clocks.
3. **State-vector sync**: On reconnect, peers exchange compact version vectors and stream only missing operations, converging identically with zero central lock.

```js
// Commutative LWW-Register merge: order-independent convergence
function mergeRegister(local, remote) {
  if (remote.hlc > local.hlc) return remote;
  if (remote.hlc === local.hlc && remote.clientId > local.clientId) return remote;
  return local;
}
```

## Interactive Labs & Dissections
- [IndexedDB Transactions and Storage Quotas Lab](https://frontend.rocks/concepts/indexeddb-transactions-quotas/#lab) ([Markdown](https://frontend.rocks/concepts/indexeddb-transactions-quotas.md))
- [Local-First Sync & CRDTs Lab](https://frontend.rocks/concepts/local-first-sync-crdt/#lab) ([Markdown](https://frontend.rocks/concepts/local-first-sync-crdt.md))

## Related Architecture Autopsies
- [Autopsy #08: Surviving a 4-Hour Tunnel: Local-First Sync & Conflicts](https://frontend.rocks/autopsies/offline-first-conflict-storm/) ([Markdown](https://frontend.rocks/autopsies/offline-first-conflict-storm.md)): A user edits a document offline on a train and silently overwrites a teammate's merged changes upon reconnection.

## Primary Sources
- https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API/Using_IndexedDB
- https://w3c.github.io/IndexedDB/
- https://www.inkandswitch.com/local-first/
- https://crdt.tech/
- https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API
