Actor-over-Worker Architecture (Off-Main-Thread UI)
Move state machines, indexed caches, and sync engines off the main thread into Dedicated Web Workers modeled as actors. The main thread only dispatches intent messages and renders diffs, preventing 100ms computation spikes from dropping input frames.
Actor-over-Worker Mailbox & Zero-Copy Reply
Trace how the main thread dispatches a command over MessageChannel while staying responsive for 60fps rendering.
UI dispatches a search/query intent with a dedicated MessageChannel port, immediately yielding back to rAF and input.
worker.postMessage({ method: 'query', payload, replyPort: port2 }, [port2]);- mainThread
- 60fps idle
- workerMailbox
- [ "query#1" ]
Under the Hood
Blink runs each Worker in a separate OS thread with its own V8 isolate and event loop, so actor memory is physically unreachable from window. Actors communicate strictly through asynchronous postMessage mailboxes and MessageChannel ports.
- Single-Writer Mailbox: Incoming commands queue in FIFO order on the Worker event loop, eliminating mutexes and race conditions around local caches or OPFS handles.
- Request-Response Correlation: Attaching a
MessageChannelport or monotonicrequestIdturns fire-and-forgetpostMessageinto typedPromiseRPC. - Zero-Copy Diffs: Passing Transferable
ArrayBuffers detaches ownership from the Worker to the main thread in O(1) time without structured-clone overhead.
// main.js: typed RPC proxy over a MessageChannel mailbox
function callActor(worker, method, payload, transfer = []) {
return new Promise((resolve, reject) => {
const { port1, port2 } = new MessageChannel();
port1.onmessage = ({ data }) => {
port1.close();
data.error ? reject(new Error(data.error)) : resolve(data.result);
};
worker.postMessage({ method, payload, replyPort: port2 }, [port2, ...transfer]);
});
}Problem Genealogy
- 2015 (Main-Thread Monolith): Single-page apps ran 40 KB+ state stores, normalizers, and optimistic-UI queues on the UI thread, causing 50-200ms INP stalls during user typing.
- 2018 (Comlink & RPC Proxies): ES6
Proxywrappers hidpostMessageplumbing, cutting worker boilerplate by ~80% while exposing async boundaries. - 2024 (Local-First Actor Engines): Apps pair Worker actors with OPFS SQLite, keeping 60fps scroll even under 50,000-row client queries.
Production Pitfalls
- Chatty Per-Keystroke RPC: Sending an actor message on every
pointermoveor keystroke saturates structured cloning and adds 0.5-2ms IPC latency per hop. Batch high-frequency inputs or keep ephemeral hover state on the main thread. - Unbounded Mailbox Growth: Firing
postMessagefaster than the Worker drains its queue bloats memory silently. Track pendingrequestIdcounts and drop stale intermediate commands.
- When not to use Actor-over-Worker: Do not offload trivial CRUD apps or sub-1ms state updates where
postMessageserialization plus async frame delay (1-4ms) exceeds the computation itself.
Connections & Logical Next Steps
Actor isolation relies on dedicated OS-backed worker threads with independent V8 isolates.
Stateful worker actors persist snapshots synchronously using OPFS createSyncAccessHandle.
Large state diffs transfer across worker mailboxes via Transferable ArrayBuffers without copying.