Overlapping CPU Encoding and Network Uploads: Building a Pipelined Concurrency Pool in JavaScript

Discover how a bounded worker pool of depth 2 overlaps in-browser PNG-8 quantization with active network streaming, preventing tab crashes and cellular stalls.

Overlapping CPU Encoding and Network Uploads: Building a Pipelined Concurrency Pool in JavaScript
Table of Contents
Key Takeaways
  • Sequential uploads leave network pipes idle during heavy client-side CPU image encoding, inflating total wait times.
  • Unbounded parallel uploads with Promise.all allocate over 165 MB of uncompressed bitmap memory, crashing mobile tabs.
  • A pipelined concurrency pool of depth 2 overlaps in-browser PNG-8 quantization and SHA-256 hashing with active network streaming.
  • Exponential backoff with randomized jitter prevents thundering herd congestion during transient cellular packet drops.
  • PoofShare implements partial-success assembly, preserving valid uploads in a unified gallery even if one file drops.

The Failure Modes of Upload Extremes

Handling multi-image uploads requires balancing client memory safety against network throughput.

The Sequential Waterfall Bottleneck

Consider dropping five 4K desktop screenshots (3840 x 2160 pixels) into a web uploader. To protect recipient bandwidth, the application quantizes each 32-bit capture into an indexed 8-bit PNG palette using UPNG.js in browser memory.

In a naive sequential pipeline, the browser processes files one after another:

Image 1: [ CPU Encode: 350ms ] -> [ Network Stream: 500ms ]
Image 2:                        -> [ CPU Encode: 350ms ] -> [ Network Stream: 500ms ]
Image 3:                                                 -> [ CPU Encode: 350ms ] -> ...

While Image 1 is streaming over the network, the client CPU sits completely idle. When Image 2 begins encoding, the network uplink goes quiet. Multiplying 850 milliseconds across five files results in a total wall-clock duration of 4,250 milliseconds: over four seconds for a routine multi-image report.

The Unbounded Promise.all Memory Explosion

Developers looking to eliminate sequential delays often swing to the opposite extreme: firing all five files simultaneously using Promise.all.

While this keeps network pipes saturated, it introduces severe memory pressure on mobile devices. A single 4K desktop screenshot contains 8,294,400 pixels. At 32-bit RGBA color representation (4 bytes per pixel), raw uncompressed canvas memory is:

Uncompressed Memory per Image: 3840 x 2160 x 4 bytes = 33,177,600 bytes = 33.18 MB
Total Memory for 5 Images:     33.18 MB x 5 = 165.90 MB raw bitmap RAM

Allocating 165.9 MB simultaneously triggers mobile OS memory watchdogs, terminating the tab. Even if it survives, five concurrent POST streams cause packet contention and cellular timeouts.

Execution Pipeline: Sequential vs. Promise.all vs. Pipelined Pool
Execution Pipeline: Sequential vs. Promise.all vs. Pipelined Pool

Pipelined Concurrency (Pool Size = 2)

A bounded worker pool delivers the high throughput of parallel processing while preserving the strict memory bounds of sequential execution.

Overlapping CPU Work and Network IO

The optimal concurrency architecture for browser-based image ingestion decouples CPU encoding from network transmission using a pool depth of 2.

At PoofShare, client processing runs through two concurrent execution slots. While Slot 1 streams the quantized byte array of Image 1 over the network, Slot 2 executes UPNG.js median-cut quantization and WebCrypto SHA-256 hashing for Image 2 in an isolated Web Worker:

Worker Slot A: [ CPU Encode #1: 350ms ] -> [ Network Stream #1: 500ms ] -> [ CPU Encode #3: 350ms ] -> [ Net #3: 500ms ] -> [ CPU #5: 350ms ] -> [ Net #5: 500ms ]
Worker Slot B: [ CPU Encode #2: 350ms ] -> [ Network Stream #2: 500ms ] -> [ CPU Encode #4: 350ms ] -> [ Net #4: 500ms ] (Idle)
Total Duration: 2,550 ms (40% faster than sequential, zero memory spike)

Because at most two uncompressed canvas bitmaps exist in browser memory at any moment, peak RAM allocation remains bounded at approximately 66.36 MB (33.18 MB x 2), safely beneath mobile memory eviction thresholds.

Task Runner Queue Implementation

The core of the pipelined pool is a lightweight task runner that manages worker slots without external dependencies.

Bounded Async Task Dispatch

Each queued task encapsulates client-side preparation (UPNG quantization, binary header validation, and cryptographic hashing) and the network fetch request:

class PipelinedPool {
  constructor(concurrency = 2) {
    this.concurrency = concurrency;
    this.running = 0;
    this.queue = [];
  }

  enqueue(taskFn) {
    return new Promise((resolve, reject) => {
      this.queue.push({ taskFn, resolve, reject });
      this.pump();
    });
  }

  pump() {
    while (this.running < this.concurrency && this.queue.length > 0) {
      const { taskFn, resolve, reject } = this.queue.shift();
      this.running++;
      taskFn()
        .then(resolve)
        .catch(reject)
        .finally(() => {
          this.running--;
          this.pump();
        });
    }
  }
}

As soon as one upload finishes, the next queued file immediately claims the free slot.

NOTE
UPNG Quantization Engine: PoofShare utilizes self-hosted UPNG.js built on pure JavaScript and typed arrays (Uint8Array and Uint32Array). It executes median-cut palette clustering directly in browser RAM without requiring heavy WebAssembly runtimes or backend compute.

Resilience on Flaky Mobile Connections

In real-world network conditions, cellular uploads frequently suffer transient packet loss and temporary tower handoffs.

Exponential Backoff With Jitter

When an upload request fails due to an HTTP 502 Bad Gateway, network timeout, or dropped socket, immediate retries risk overloading edge gateways during traffic spikes.

PoofShare implements exponential backoff with full randomization jitter across three retry attempts:

async function uploadWithRetry(fileBlob, sessionToken, attempt = 1) {
  const maxAttempts = 3;
  const baseDelay = 1000;

  try {
    return await dispatchStream(fileBlob, sessionToken);
  } catch (err) {
    if (attempt >= maxAttempts) throw err;
    const jitter = Math.random() * 200;
    const delay = Math.min(baseDelay * (2 ** (attempt - 1)) + jitter, 8000);
    await new Promise((r) => setTimeout(r, delay));
    return uploadWithRetry(fileBlob, sessionToken, attempt + 1);
  }
}

The first retry pauses for approximately 1,000 ms, the second for 2,000 ms, and the third for 4,000 ms. Adding randomized jitter prevents reconnecting clients from hitting the gateway simultaneously.

Exponential Backoff and Partial-Success Gallery Assembly
Exponential Backoff and Partial-Success Gallery Assembly

Many uploaders treat batches as all-or-nothing transactions: if four screenshots succeed but the fifth drops, they discard the batch.

PoofShare implements partial-success tolerance. If four out of five images complete upload within retry thresholds, the client gateway assembles the four valid images into a unified /g/:slug gallery drop.

The user receives their link immediately alongside a toast alert noting the failed capture, preventing connection drops from stalling work.

Explore our CDN ghost cache resolution, learn how clipboard paste events operate under the hood, and review our zero-log retention guarantees in our Privacy Policy.

Why does Promise.all crash mobile browsers during multi-image uploads?

When uploading multiple high-resolution screenshots at once, Promise.all decodes and holds all uncompressed bitmap arrays in RAM simultaneously. Five 4K images allocate over 165 MB of memory, triggering operating system tab termination on mobile devices.

What is the ideal concurrency pool depth for browser uploads?

A concurrency depth of 2 provides the optimal balance. It keeps one CPU worker active on image quantization while another streams data over the network, avoiding both network idling and memory exhaustion.

How does UPNG.js reduce PNG file size without quality loss?

UPNG.js quantizes 32-bit truecolor RGBA data down to an optimized 256-color indexed palette with alpha transparency. For software interfaces with limited color diversity, this reduces file size by 70% to 85% with zero visible text distortion.

What happens if one image fails to upload in a multi-image drop?

The platform automatically retries transient errors up to three times with exponential backoff. If the file still fails, the system executes partial-success assembly, generating a valid gallery link for the successful images.

PS

PoofShare Team

Dedicated to building zero-knowledge, ephemeral hosting systems and sharing practical technical guides on privacy.

Link copied to clipboard!