# IndexedDB Transactions and Storage Quotas vs Origin Private File System (OPFS)

- **Canonical URL:** https://frontend.rocks/vs/indexeddb-transactions-quotas-vs-opfs-origin-private-fs/
- **Relation:** Contrasts With
- **Last Verified:** 2026-11-15
- **Verdict:** OPFS provides raw byte-level file handles without structured-clone or transaction overhead.

## Comparison Matrix

| Dimension | IndexedDB Transactions and Storage Quotas | Origin Private File System (OPFS) |
| :--- | :--- | :--- |
| **Maturity** | Baseline · Widely | Baseline · Widely |
| **Domain** | Web APIs | Web APIs |
| **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. | Origin-sandboxed virtual file system backed by raw OS files, offering synchronous in-place byte-range I/O inside Dedicated Workers. |
| **Prerequisites** | Event Loop, Macrotasks, and Microtask Queue (IDB transaction auto-commit is tied directly to microtask queue exhaustion.) | Dedicated Web Workers and Transferables (FileSystemSyncAccessHandle is only exposed inside DedicatedWorkerGlobalScope.) |
| **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 OPFS: For small key-value settings or indexed JSON queries, IndexedDB avoids manual byte-offset management and exclusive file locking. |

## 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);
});
```

## Origin Private File System (OPFS) — Under the Hood
If [IndexedDB](/concepts/indexeddb-transactions-quotas) is a notary public stamping every envelope, OPFS is a raw file descriptor handed directly to your [Dedicated Web Worker](/concepts/dedicated-web-workers).

1. **Private Root**: `navigator.storage.getDirectory()` returns a `FileSystemDirectoryHandle` invisible to the user's OS file picker.
2. **Exclusive Sync Handle**: Calling `fileHandle.createSyncAccessHandle()` in a worker acquires an exclusive lock and exposes `read(buf, { at })` and `write(buf, { at })`.
3. **Zero Serialization**: Bytes move straight from a Wasm linear memory `Uint8Array` to the OS file buffer without `structuredClone` or transaction wrappers.

```js
// Inside a Dedicated Worker
const root = await navigator.storage.getDirectory();
const file = await root.getFileHandle('db.sqlite', { create: true });
const access = await file.createSyncAccessHandle();

const page = new Uint8Array(4096);
access.read(page, { at: 0 });
access.write(page, { at: 4096 });
access.flush();
access.close();
```

## 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))
- [Origin Private File System (OPFS) Lab](https://frontend.rocks/concepts/opfs-origin-private-fs/#lab) ([Markdown](https://frontend.rocks/concepts/opfs-origin-private-fs.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.
- [Autopsy #12: Moving the Entire App Engine Off the Main Thread](https://frontend.rocks/autopsies/zero-main-thread-ui-worker/) ([Markdown](https://frontend.rocks/autopsies/zero-main-thread-ui-worker.md)): Rich spreadsheet and graph app drops to 4 FPS during 100k-row sorts until state and OPFS move to a Worker.

## Primary Sources
- https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API/Using_IndexedDB
- https://w3c.github.io/IndexedDB/
- https://developer.mozilla.org/en-US/docs/Web/API/File_System_API/Origin_private_file_system
- https://fs.spec.whatwg.org/
