Multi-Worker Parallel Encryption: Bounding RAM < 150 MB

An architectural exploration of ZevSafe's elastic Web Worker pool, credit-based backpressure pipeline, and zero-copy transferable buffers that make 5 GB+ in-browser encryption possible.

1. The Single-Threaded RAM Trap in Browsers

Browsers enforce strict limits on single-tab memory consumption. On mobile operating systems (like Android and iOS), a web tab that exceeds 300–400 MB of heap memory is aggressively terminated by the low-memory killer (OOM).

If a web encryptor attempts to read a 4 GB video file into memory using FileReader.readAsArrayBuffer(), the browser immediately crashes. Even if chunking is used, naive Web Worker implementations copy buffers via structured cloning, duplicating memory usage across threads.

2. The Elastic WorkerPool Architecture

ZevSafe implements an elastic Web Worker pool (js/worker-bridge.js) that scales dynamically to match user hardware concurrency:

JavaScript Architecture · Elastic Pool Scaling
// Dynamic pool dimensioning clamped between 4 and 8 workers
const coreCount = navigator.hardwareConcurrency || 4;
const poolSize = Math.max(4, Math.min(8, coreCount));

class WorkerPool {
    constructor(size) {
        this.workers = [];
        for (let i = 0; i < size; i++) {
            this.workers.push(new Worker('js/crypto-worker.js'));
        }
    }
}

3. Zero-Copy Transferable ArrayBuffers

Traditional worker.postMessage(data) performs a deep structured clone of the buffer, doubling heap allocations.

ZevSafe utilizes the HTML5 Transferable Objects specification:

Zero-Copy Transfer Hook
// In js/worker-bridge.js:
function dispatchChunkToWorker(worker, chunkPayload) {
    const rawBuffer = chunkPayload.bytes.buffer;
    // Hand over buffer ownership instantly without memory cloning
    worker.postMessage({
        action: 'ENCRYPT_CHUNK',
        chunkIndex: chunkPayload.index,
        data: rawBuffer
    }, [rawBuffer]); // Transfer list
}

Transferring ownership is an instantaneous $O(1)$ pointer exchange. On a 5 GB vault, this prevents allocating over 20 GB of intermediate garbage in the V8 engine!

4. The OrderedReassemblyQueue: Solving Race Conditions

Because pool workers process chunks concurrently, chunks finish non-deterministically (e.g. Worker 3 may finish Chunk 7 before Worker 1 finishes Chunk 5).

Writing chunks out of order would completely corrupt the archive wire format. ZevSafe introduces the OrderedReassemblyQueue:

5. Credit-Based Flow Control: Guaranteeing < 150 MB Peak Heap

If the file reader reads faster than the disk writer can flush to storage, unwritten chunks accumulate in memory, causing heap runaway.

🛡️ The Deferred Credit Window

ZevSafe enforces an adaptive credit window: $W = \max(2, \min(12, N \times 2))$. The reader cannot read Chunk $K$ until the writer has finished flushing Chunk $K - W$ to disk. Credits are released strictly after disk I/O completes.

This ensures that at any given millisecond, only a small, fixed number of 4 MB buffers exist in RAM, capping peak heap usage strictly below 105 MB regardless of whether the file being encrypted is 500 MB or 50 GB.