Tutorial

Multi-File Upload Queue

Queue multiple uploads with a bounded concurrency, retry each independently, and report per-file and overall progress without blocking the UI thread.

On this page

  1. Introduction
  2. Cap concurrency
  3. Per-file retry
  4. Aggregate progress
  5. Failure isolation
  6. Test with flaky
  7. Sources

Introduction

This guide covers a multi-file upload queue with bounded concurrency, a recurring concern in the file-upload scenario. Uploading twenty files at once either saturates the bandwidth or saturates the server connection limit. A queue with a bounded concurrency, per-file retry, and aggregated progress lets the user drop a folder and walk away, while the queue chews through it at a safe rate.

Cap concurrency

Run at most a few uploads at once so you do not trip server connection limits or saturate bandwidth, and let the rest wait in the queue.

The trade-off worth naming is that run at most a few uploads at once so you do not trip server connection limits or saturate bandwidth, and let the rest wait in the queue adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

async function pool(files, limit = 3) {
  const i = files.values();
  const workers = Array.from({ length: limit }, async () => {
    for (const f of i) await upload(f);
  });
  await Promise.all(workers);
}

Per-file retry

Give each upload its own bounded retry so a transient failure on one file retransmits that file, not the whole batch, and does not block the queue.

The trade-off worth naming is that give each upload its own bounded retry so a transient failure on one file retransmits that file, not the whole batch, and does not block the queue adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Aggregate progress

Sum the per-file bytes to show an overall percent alongside each file status, so the user sees both the forest and the trees without polling.

The trade-off worth naming is that sum the per-file bytes to show an overall percent alongside each file status, so the user sees both the forest and the trees without polling adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

let total = 0, done = 0;
files.forEach(f => { total += f.size; f.onProgress = (b) => { done += b; render(done / total); }; });

Failure isolation

Mark a file failed and continue, then let the user retry just that file, so one bad file does not stop the rest of the batch from completing.

The trade-off worth naming is that mark a file failed and continue, then let the user retry just that file, so one bad file does not stop the rest of the batch from completing adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Test with flaky

Run 20 files against an endpoint that fails 10 percent of the time and assert the queue completes the good ones and marks the bad ones retryable, so the queue degrades gracefully.

The trade-off worth naming is that run 20 files against an endpoint that fails 10 percent of the time and assert the queue completes the good ones and marks the bad ones retryable, so the queue degrades gracefully adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Sources

Sources: p-limit, async queue.

FAQ

What concurrency is safe?
Three to five is a common cap; enough to keep the pipe full, low enough not to trip server connection limits or saturate the client bandwidth so one slow file stalls the rest.
Should a failed file block the queue?
No; mark it failed and move on, then let the user retry just that file, so one bad file does not stop nineteen good ones.

Related Examples