Multi-Tab State Synchronization & Leader Election
Opening 10 tabs of a web app multiplies WebSocket connections, triggers parallel OAuth refresh races, and corrupts shared IndexedDB state. Acquiring an exclusive navigator.locks.request() lease elects one active leader tab that owns network I/O and broadcasts snapshots via BroadcastChannel.
Web Locks Leader Election & Crash Failover
Trace how Tab A holds an exclusive Web Lock, broadcasts updates to Tab B, and how Tab B promotes on Tab A close.
Tab A wins navigator.locks.request('tab-leader'), opens the WebSocket, and posts updates to BroadcastChannel.
navigator.locks.request('tab-leader', () => runLeaderLoop());- tabA
- leader
- tabB
- queued-follower
- openSockets
- 1
Under the Hood
The browser's Lock Manager grants an exclusive lock to the first tab calling navigator.locks.request() and queues all other tabs in FIFO order. Unlike localStorage heartbeat timers, the engine releases the lock automatically when a tab crashes or closes.
- Zero-polling election: The leader holds the lock inside an unresolved Promise until the tab unloads or yields, eliminating 500 ms
localStoragepolling timers. - Fan-out over
BroadcastChannel: The elected leader owns the WebSocket, token refresh, and IndexedDB writes, pushing state deltas to follower tabs via BroadcastChannel & Web Locks. - Instant failover: When the leader tab closes, the Lock Manager wakes the next queued tab immediately to resume sync.
const channel = new BroadcastChannel('app-sync');
navigator.locks.request('tab-leader', async () => {
const ws = new WebSocket('wss://api.example.com/stream');
ws.onmessage = (e) => channel.postMessage(e.data);
// Hold lock until tab closes or is evicted
await new Promise<void>((resolve) => {
window.addEventListener('pagehide', () => { ws.close(); resolve(); });
});
});Problem Genealogy
- 2014–2018 (
localStorageHeartbeat Hacks): Apps wrote timestamps tolocalStorageevery 1,000 ms to detect dead tabs, causing split-brain elections when background tabs were throttled to 1-minute timers. - 2019+ (Web Locks API): Moved mutex scheduling into the browser C++ process so crashed tabs release locks in milliseconds with zero polling overhead.
Production Pitfalls
- Users logged out randomly with 10 tabs open: Without a Web Lock around OAuth refresh-token rotation, two tabs send the same single-use refresh token simultaneously, revoking the entire session family.
- Frozen
bfcachetab retains leader lock: Holding a Web Lock acrosspagehidewithout releasing it blocks the page from entering Back/Forward Cache and stalls follower tabs.
- When not to use cross-tab leader election: On isolated single-tab ephemeral forms or when a
SharedWorkeris already available across all target browsers (excluding Android Chrome, whereSharedWorkeris unsupported andnavigator.locksis mandatory).
Connections & Logical Next Steps
navigator.locks provides browser-managed mutual exclusion while BroadcastChannel fans out leader state to followers.
Single-leader coordination prevents concurrent tabs from interleaving read-modify-write IndexedDB transactions.
Seen in Architecture Autopsies
Without a single elected sync leader, competing tabs open duplicate WebSockets and apply out-of-order server snapshots.
Elects a single active tab to hold the upstream WebSocket and transfers ownership on tab close.