State & ArchitectureBaseline · Widely

Multi-Tab State Synchronization & Leader Election

Mental Model

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.

Interactive Simulator

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.

Lock: Held by Tab A
Tab A (Leader + WS)
Tab B (Follower)
Step 1 / 3: 1. Tab A Acquires Leader Lock

Tab A wins navigator.locks.request('tab-leader'), opens the WebSocket, and posts updates to BroadcastChannel.

navigator.locks.request('tab-leader', () => runLeaderLoop());
Internal State / Memory
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.

  1. Zero-polling election: The leader holds the lock inside an unresolved Promise until the tab unloads or yields, eliminating 500 ms localStorage polling timers.
  2. 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.
  3. Instant failover: When the leader tab closes, the Lock Manager wakes the next queued tab immediately to resume sync.
TS
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 (localStorage Heartbeat Hacks): Apps wrote timestamps to localStorage every 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

Production Pitfall
  • 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 bfcache tab retains leader lock: Holding a Web Lock across pagehide without releasing it blocks the page from entering Back/Forward Cache and stalls follower tabs.
When Not to Use
  • When not to use cross-tab leader election: On isolated single-tab ephemeral forms or when a SharedWorker is already available across all target browsers (excluding Android Chrome, where SharedWorker is unsupported and navigator.locks is mandatory).

Connections & Logical Next Steps

State & ArchitectureMulti-Tab State Synchronization & Leader Election
What This Unlocks
Leaf concept

Seen in Architecture Autopsies