Skip to content

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:

Browser Tab A (Writer)WebDB Instance ABuffer Pool A1024 Page Slots (4 MB)Catalog Cache ATable & Index MetadataBrowser Tab B (Reader / UI)WebDB Instance BBuffer Pool B1024 Page Slots (4 MB)Catalog Cache BTable & Index MetadataBroadcastChannelwebdb_sync_${vfs}_${db}Shared Origin Storage (IndexedDB / OPFS)Database PagesPage 1 (Catalog) .. Page NWrite-Ahead Log (.wal)CRC32 Atomic Commit Framesnavigator.locks (Exclusive)
  1. 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.
  2. Storage Lock Invariants (OPFS & IndexedDB): In the Origin Private File System (OPFS), opening a FileSystemSyncAccessHandle claims an exclusive file lock; attempting to open it concurrently from another context throws NoModificationAllowedError. In IndexedDB, uncoordinated concurrent read-modify-write cycles can overwrite each other's binary chunks.
  3. 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:

TierComponentScopeResponsibility
Tier 1 (Intra-Tab)ExclusiveLeaseQueueSingle Tab / WorkerSerializes 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 OriginEnsures 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) => ...):

  1. The coordinator requests an Exclusive Transaction Lease (acquireExclusiveLease()).
  2. It waits for any in-flight autocommit queries to drain.
  3. It assigns a unique leaseId and txId.
  4. All database operations performed using the tx object bypass the queue and dispatch immediately.
  5. Any operations called on the outer db instance during this window are queued behind the transaction lease until commit() or rollback() 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:

ts
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.request is 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):

  1. WAL Salt & Frame Verification: The tab compares the .wal header salt and frame count against its local sequence. If another tab appended frames, it invokes recoverFromWal() to replay committed frames into Page 1..N and discard any orphaned frames.
  2. Header Comparison: The tab reads Page 1's header bytes:
    • If changeCounter changed: it invalidates all cached pages in buffer pool slots 1…N. Future reads will re-fetch fresh page bytes from storage.
    • If schemaVersion changed: it clears the internal table metadata cache (tableMetaCache), table tail pointers, and index tail pointers, re-parsing catalog definitions from Page 1.

6. Reactive Cross-Tab Invalidation (BroadcastChannel) ​

WebDB uses BroadcastChannel to propagate mutations across tabs in real time:

webdb_sync_${vfs.name}_${dbName}
Tab A (Writer)Web Locksnavigator.locksStorageIDB / OPFSBroadcastChannelwebdb_sync_*Tab B (Reader)UI & Live Queries1. Acquire exclusive lock2. Write WAL frames & commit3. Release lock4. postMessage({ tables: ['todos'], changeCounter: 42 })5. onmessage(event)6. Invalidate buffer slots7. notifySubscriptions()useLiveQuery re-evaluates
  1. Broadcast Message: Upon committing, Tab A broadcasts:
    json
    {
      "senderId": "tab_x89a",
      "tables": ["todos"],
      "changeCounter": 42,
      "schemaVersion": 3,
      "origin": "local"
    }
  2. 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: ​

  1. 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.
  2. 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.
  3. 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:

ts
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:

FeatureNative IndexedDBWebDB
Execution ArchitectureManaged by browser's background storage daemon (LevelDB/SQLite via IPC)Embedded C/Wasm engine inside each tab sharing disk storage
Locking MechanismInternal engine locks on object stores (readonly vs readwrite)Browser navigator.locks API coordinated across tabs
Write ConcurrencyQueued per object store across tabs by browser processSingle 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 CachingManaged inside browser process; structured-clone IPC overhead per readIn-tab buffer pool (4 MB) for zero-copy C/Wasm reads; evicted via invalidation messages
Tab Crash RecoveryHandled by browser storage processLock 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 versionchange event 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 an onblocked event 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: [...] } over BroadcastChannel.
  • 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 BroadcastChannel or localStorage.
  • 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 that todos changed, 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 changeCounter invalidation 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 ​

RequirementWebDB MechanismGuarantee
Write Serializationnavigator.locksNo concurrent writes can corrupt pages or WAL frames.
Deadlock ImmunityOS-level browser Web LocksIf a tab crashes or is closed, lock releases within 5ms.
Cache InvalidationPage 1 changeCounter + slot resetStale page data is never read across tab boundaries.
Reactive SyncBroadcastChannelMulti-tab UI state updates automatically in sub-millisecond time.
Schema IsolationPage 1 schemaVersion + catalog reloadDDL upgrades invalidate catalog caches safely across tabs.

Released under the MIT License.