Locking & Cross-Tab Coordination
WebDB is engineered from the ground up for multi-tab browser environments. When users open multiple tabs, windows, or Web Workers pointing to the same database on the same origin, WebDB guarantees strict ACID isolation, cache coherency, and sub-5ms crash recovery without requiring a central server or polling.
1. The Multi-Tab Browser Challenge
Running a relational database inside a browser introduces fundamental concurrency challenges that traditional server-side databases (like PostgreSQL or MySQL) do not face:
- Independent Memory Heaps & Buffer Pools: Each browser tab possesses its own isolated JavaScript heap and
WebAssembly.Memory. If Tab A commits an update that alters Page 4, Tab B's in-memory buffer pool will still hold the stale image of Page 4 unless an active invalidation protocol exists. - Storage Lock Invariants (OPFS & IndexedDB): In the Origin Private File System (OPFS), opening a
FileSystemSyncAccessHandleclaims an exclusive file lock; attempting to open it concurrently from another context throwsNoModificationAllowedError. In IndexedDB, uncoordinated concurrent read-modify-write cycles can overwrite each other's binary chunks. - Abrupt Tab Termination: Users can close, kill, or refresh tabs at any point in time—including halfway through writing a slotted page or committing a WAL transaction. The locking mechanism must never leave orphaned deadlocks.
2. The Two-Tier Concurrency Architecture
To resolve these challenges, WebDB implements a coordinated two-tier concurrency engine:
| Tier | Component | Scope | Responsibility |
|---|---|---|---|
| Tier 1 (Intra-Tab) | ExclusiveLeaseQueue | Single Tab / Worker | Serializes local async operations in FIFO order; coordinates explicit db.transaction() leases with autocommit queries. |
| Tier 2 (Cross-Tab) | Web Locks (navigator.locks) | All Tabs across Origin | Ensures only one tab touches the shared storage and WAL at any given instant. |
3. Tier 1: Local Intra-Tab Concurrency (ExclusiveLeaseQueue)
Within a single tab, JavaScript is single-threaded, but concurrent asynchronous calls (Promise.all(), unawaited queries, UI events) can initiate overlapping storage operations.
The ExclusiveLeaseQueue coordinates local tasks via two pathways:
Autocommit Queries
Single reads and writes (e.g. db.insert(), db.from().toArray()) pass through leaseQueue.enqueue(). If no transaction is active, they execute immediately. If multiple autocommit tasks arrive together, they queue in strict FIFO order.
Explicit Transactions (TransactionLease)
When you call db.transaction(async (tx) => ...):
- The coordinator requests an Exclusive Transaction Lease (
acquireExclusiveLease()). - It waits for any in-flight autocommit queries to drain.
- It assigns a unique
leaseIdandtxId. - All database operations performed using the
txobject bypass the queue and dispatch immediately. - Any operations called on the outer
dbinstance during this window are queued behind the transaction lease untilcommit()orrollback()completes.
4. Tier 2: Cross-Tab Coordination via the Web Locks API
For coordinating across different tabs, windows, and Web Workers, WebDB relies on the native Web Locks API (navigator.locks).
Lock Naming & Acquisition
WebDB scopes locks per storage adapter and database name:
webdb_lock_${vfs.name}_${dbName}(For example: webdb_lock_idb_my_app or webdb_lock_opfs_analytics)
Whenever a tab needs to perform an exclusive operation (such as flushing WAL frames, committing transactions, or running migrations), it requests an exclusive Web Lock:
await navigator.locks.request(lockName, { mode: 'exclusive' }, async () => {
// 1. Sync cross-tab state (detect if other tabs wrote new pages)
await syncCrossTabState();
// 2. Perform transactional read/write operations
await executeTask();
// 3. Flush changes and broadcast update
await broadcastChanges();
});Sub-5ms Instant Failover on Tab Close
Unlike manual lock flags stored in localStorage (which permanently deadlock if a tab is killed before clearing the flag), Web Locks are operating-system-level browser primitives.
If a tab crashes or the user terminates the browser tab:
- The browser runtime automatically and atomically releases the Web Lock within 5 milliseconds.
- The next queued tab in
navigator.locks.requestis instantly granted the lock. - The newly scheduled tab immediately inspects the WAL, rolls back any incomplete writes from the dead tab, and resumes normal execution with zero downtime.
5. Cache Coherency & State Synchronization
When Tab A commits a change, Tab B must invalidate its in-memory buffer pool before executing its next read. WebDB achieves this with a zero-copy version check.
Page 1 Header Flags
Page 1 contains two monotonic 32-bit integers in its system header:
changeCounter(HEADER_OFFSET_CHANGE_COUNTER): Incremented on every committed write transaction.schemaVersion(HEADER_OFFSET_SCHEMA_VERSION): Incremented on every DDL change (CREATE TABLE,ALTER TABLE,DROP TABLE,CREATE INDEX).
The Synchronization Protocol (syncCrossTabState)
Whenever a tab acquires the cross-tab lock lease (or receives an invalidation notification):
- WAL Salt & Frame Verification: The tab compares the
.walheader salt and frame count against its local sequence. If another tab appended frames, it invokesrecoverFromWal()to replay committed frames into Page 1..N and discard any orphaned frames. - Header Comparison: The tab reads Page 1's header bytes:
- If
changeCounterchanged: it invalidates all cached pages in buffer pool slots. Future reads will re-fetch fresh page bytes from storage. - If
schemaVersionchanged: it clears the internal table metadata cache (tableMetaCache), table tail pointers, and index tail pointers, re-parsing catalog definitions from Page 1.
- If
6. Reactive Cross-Tab Invalidation (BroadcastChannel)
WebDB uses BroadcastChannel to propagate mutations across tabs in real time:
webdb_sync_${vfs.name}_${dbName}- Broadcast Message: Upon committing, Tab A broadcasts:json
{ "senderId": "tab_x89a", "tables": ["todos"], "changeCounter": 42, "schemaVersion": 3, "origin": "local" } - Selective Live Query Re-Execution: Tab B receives the message, ignores it if
senderId === this.instanceId, and notifies its reactive subscriptions. Only the live queries observing the modified tables (todos) re-run. Queries observing unaffected tables (settings,users) do not re-run, conserving battery and CPU.
7. Schema Upgrades Across Tabs Running Different App Versions
In real-world web apps, a user may have Tab B open on an older release while Tab A loads a newly deployed version that triggers schema migrations:
How WebDB Handles Version Drift:
- Additive Changes (
ADD COLUMN): WebDB's slotted page record serializer encodes variable-length offset tables and null bitmaps. If Tab A adds a column and writes new rows, Tab B (reading with the old column schema) cleanly reads the known columns and ignores trailing payload bytes. - Backward Compatibility for Existing Rows: Rows stored on disk before a new column was added evaluate to
NULL(or their default values) without requiring an immediate, expensive whole-table rewrite. - In-Flight Isolation: When Tab A runs
Migrator.up(), the entire migration executes inside an exclusive transaction protected by the cross-tab lock. Tab B cannot read half-migrated tables or broken indexes.
Detecting Migrations in the Application Layer
You can listen to cross-tab schema sync events on the WebDB instance to prompt the user to refresh the older tab:
const db = await WebDB.open({
engine: new WasmEngine(),
storage: new IdbStorage('myapp'),
});
// Track the schema version when this tab loaded
const initialSchemaVersion = db.getSyncState().schemaVersion;
// Listen for cross-tab updates
db.onSync((event) => {
if (event.schemaVersion > initialSchemaVersion) {
showBanner('A new app version was deployed in another tab. Click to refresh.', {
onClick: () => window.location.reload(),
});
}
});8. WebDB vs. Native IndexedDB: Multi-Tab Comparison
Developers frequently ask how WebDB's multi-tab coordination compares to the browser's native IndexedDB API. While both operate within browser constraints, their internal models and developer ergonomics differ fundamentally:
| Feature | Native IndexedDB | WebDB |
|---|---|---|
| Execution Architecture | Managed by browser's background storage daemon (LevelDB/SQLite via IPC) | Embedded C/Wasm engine inside each tab sharing disk storage |
| Locking Mechanism | Internal engine locks on object stores (readonly vs readwrite) | Browser navigator.locks API coordinated across tabs |
| Write Concurrency | Queued per object store across tabs by browser process | Single exclusive write lock during WAL commit; zero IPC overhead during reads |
| Cross-Tab Reactivity | ❌ None. Tab B has no native way to know Tab A modified data | ✅ Built-in. BroadcastChannel invalidates buffer cache & auto-triggers useLiveQuery |
| Schema Upgrades | ⚠️ versionchange locks and blocks until all other tabs call db.close() | ✅ DDL is a regular atomic WAL transaction; other tabs reload catalog on the fly |
| In-Memory Caching | Managed inside browser process; structured-clone IPC overhead per read | In-tab buffer pool (4 MB) for zero-copy C/Wasm reads; evicted via invalidation messages |
| Tab Crash Recovery | Handled by browser storage process | Lock released immediately by browser; WAL recovered automatically by next reader/writer |
1. Schema Upgrades: The "Blocked Tab" Trap
In native IndexedDB, upgrading a database schema requires calling indexedDB.open(dbName, newVersion). When this happens:
- The browser fires a
versionchangeevent on every other open connection in other tabs. - The upgrade transaction in the upgrading tab is completely blocked until all other tabs either invoke
db.close()or close their window. - If a dormant or background tab fails to handle
versionchange, the upgrading tab fires anonblockedevent and hangs indefinitely.
In WebDB, schema alterations (createTable, alterTable, createIndex) are standard atomic transactions executed under the Web Locks API:
- The migrating tab acquires the exclusive write lock, writes the new catalog page / WAL frame, increments the monotonic
schemaVersion, and releases the lock. - It broadcasts
{ schemaVersion: N, tables: [...] }overBroadcastChannel. - Other tabs receive the message, invalidate their in-memory catalog cache, and reload the schema on their next query. No tab ever has to close, and user workflows are never blocked.
2. Cross-Tab Reactivity & Live Queries
- Native IndexedDB: Does not offer mutation streams or reactive query invalidation across tabs. If Tab A inserts a record, Tab B continues to show stale UI until the user reloads the page or the developer builds custom pub/sub plumbing using
BroadcastChannelorlocalStorage. - WebDB: Reactivity is built directly into the engine. Every committed transaction broadcasts affected table names and monotonic counters. Reactive hooks like
useLiveQuery("SELECT * FROM todos")running in Tab B detect thattodoschanged, evict dirty pages from their local buffer pool, and re-execute immediately.
3. In-Memory Cache Coherence
- Native IndexedDB: Queries must deserialize data structures via structured cloning across the browser's process boundary, adding latency on every read.
- WebDB: Executes queries directly against an in-tab C/Wasm buffer pool (1024 slots / 4 MB) with microsecond response times. Cache coherence is maintained across tabs by evicting buffer slots when a remote
changeCounterinvalidation is received.
4. Crash Resilience & Stale Locks
- Native IndexedDB: Transactions are coordinated within the browser binary. If a tab crashes, the browser process aborts its active transaction.
- WebDB: Relies on the W3C standard Web Locks API (
navigator.locks). If a writing tab crashes or is closed mid-transaction, the browser's lock manager instantly drops the lock reservation. The next tab opening a connection verifies the WAL header on disk, rolling forward or truncating uncommitted frames automatically.
9. Summary of Guarantees
| Requirement | WebDB Mechanism | Guarantee |
|---|---|---|
| Write Serialization | navigator.locks | No concurrent writes can corrupt pages or WAL frames. |
| Deadlock Immunity | OS-level browser Web Locks | If a tab crashes or is closed, lock releases within 5ms. |
| Cache Invalidation | Page 1 changeCounter + slot reset | Stale page data is never read across tab boundaries. |
| Reactive Sync | BroadcastChannel | Multi-tab UI state updates automatically in sub-millisecond time. |
| Schema Isolation | Page 1 schemaVersion + catalog reload | DDL upgrades invalidate catalog caches safely across tabs. |