# BroadcastChannel and Web Locks API vs IndexedDB Transactions and Storage Quotas

- **Canonical URL:** https://frontend.rocks/vs/broadcast-channel-web-locks-vs-indexeddb-transactions-quotas/
- **Relation:** Contrasts With
- **Last Verified:** 2026-11-15
- **Verdict:** IndexedDB locks object stores per transaction, whereas Web Locks and BroadcastChannel coordinate multi-tab state explicitly.

## Comparison Matrix

| Dimension | BroadcastChannel and Web Locks API | IndexedDB Transactions and Storage Quotas |
| :--- | :--- | :--- |
| **Maturity** | Baseline · Widely | Baseline · Widely |
| **Domain** | Web APIs | Web APIs |
| **Mental Model** | Origin-scoped pub/sub messaging and async mutual-exclusion locks coordinated by the browser process across tabs, iframes, and workers. | Transactional client-side B-tree object store whose transactions auto-commit on microtask drain and whose storage limits depend on origin-wide disk quotas. |
| **Prerequisites** | Event Loop, Macrotasks, and Microtask Queue (BroadcastChannel messages and Web Lock grants arrive as asynchronous tasks on each context's event loop.) | Event Loop, Macrotasks, and Microtask Queue (IDB transaction auto-commit is tied directly to microtask queue exhaustion.) |
| **When Not to Use** | When not to use BroadcastChannel: Cross-origin iframes cannot share a BroadcastChannel; use window.postMessage() with explicit targetOrigin checks instead. | 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. |

## BroadcastChannel and Web Locks API — Under the Hood
`localStorage` `storage` events were a leaky bulletin board; `BroadcastChannel` is a dedicated intercom, and `navigator.locks` is the physical keycard that ensures only one tab writes to [OPFS](/concepts/opfs-origin-private-fs) or [IndexedDB](/concepts/indexeddb-transactions-quotas) at a time.

1. **Origin Partitioning**: Both APIs are partitioned by `(origin, top-level site)` in modern storage-partitioned browsers, blocking cross-site tracking.
2. **Sender Exclusion**: `channel.postMessage(msg)` dispatches `message` events to *other* contexts on the channel, never back to the sender instance.
3. **Crash-Safe Leases**: Because the browser process owns the lock table, killing a tab's renderer process immediately releases its held locks to the next queued waiter.

```js
const bus = new BroadcastChannel('auth-sync');

async function refreshSessionOnce() {
  await navigator.locks.request('auth-refresh', { ifAvailable: true }, async (lock) => {
    if (!lock) return; // Another tab already holds the refresh lock
    const token = await fetch('/api/refresh', { method: 'POST' }).then((r) => r.text());
    bus.postMessage({ type: 'TOKEN_UPDATED', token });
  });
}
```

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

## Interactive Labs & Dissections
- [BroadcastChannel and Web Locks API Lab](https://frontend.rocks/concepts/broadcast-channel-web-locks/#lab) ([Markdown](https://frontend.rocks/concepts/broadcast-channel-web-locks.md))
- [IndexedDB Transactions and Storage Quotas Lab](https://frontend.rocks/concepts/indexeddb-transactions-quotas/#lab) ([Markdown](https://frontend.rocks/concepts/indexeddb-transactions-quotas.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 #10: The Multi-Tab WebSocket Storm & Backpressure Collapse](https://frontend.rocks/autopsies/multi-tab-websocket-thundering-herd/) ([Markdown](https://frontend.rocks/autopsies/multi-tab-websocket-thundering-herd.md)): Users opening 8 tabs exhaust realtime gateway sockets and freeze the main thread during 400 msg/s bursts.

## Primary Sources
- https://developer.mozilla.org/en-US/docs/Web/API/Broadcast_Channel_API
- https://developer.mozilla.org/en-US/docs/Web/API/Web_Locks_API
- https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API/Using_IndexedDB
- https://w3c.github.io/IndexedDB/
