Chunked Resumable Upload
Split a large file into chunks, upload each with its own retry, and resume from the last confirmed chunk after a network drop without restarting.
On this page
Introduction
This guide covers chunked resumable upload for large files, a recurring concern in the file-upload scenario. A 5 GB upload over a flaky connection will fail near the end and force a restart, which is a waste of bandwidth and patience. Splitting the file into chunks, uploading each with its own retry, and tracking the last confirmed chunk lets you resume from where it stopped, turning a fatal drop into a brief pause.
Split on the client
Slice the File into chunks of a fixed size using blob.slice, so the upload is a sequence of small requests instead of one giant one.
The trade-off worth naming is that slice the file into chunks of a fixed size using blob.slice, so the upload is a sequence of small requests instead of one giant one 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.
const SIZE = 8 * 1024 * 1024;
function chunks(file) {
const out = [];
for (let s = 0; s < file.size; s += SIZE) {
out.push({ id: s / SIZE, blob: file.slice(s, s + SIZE) });
}
return out;
}
Upload with retry
Send each chunk with a bounded retry and backoff, so a transient 5xx or a dropped socket retransmits the one chunk instead of failing the whole upload.
The trade-off worth naming is that send each chunk with a bounded retry and backoff, so a transient 5xx or a dropped socket retransmits the one chunk instead of failing the whole upload 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 uploadChunk(url, chunk, tries = 3) {
for (let i = 0; i < tries; i++) {
const r = await fetch(url, { method: 'POST', body: chunk.blob });
if (r.ok) return await r.json();
await new Promise(r => setTimeout(r, 200 * 2 ** i));
}
throw new Error('chunk failed');
}
Verify with a checksum
Send a checksum per chunk and have the server verify before acknowledging, so a corrupt chunk is retransmitted rather than silently breaking the assembled file.
The trade-off worth naming is that send a checksum per chunk and have the server verify before acknowledging, so a corrupt chunk is retransmitted rather than silently breaking the assembled file 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.
Track progress server-side
Record the last confirmed chunk id per upload so a reconnect can ask where to resume, which is what makes the upload resumable rather than merely chunked.
The trade-off worth naming is that record the last confirmed chunk id per upload so a reconnect can ask where to resume, which is what makes the upload resumable rather than merely chunked 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.
app.post('/upload/:id/chunk', async (req, res) => {
const { id } = req.params;
const cid = Number(req.headers['x-chunk-id']);
await appendChunk(id, cid, req.body);
await setProgress(id, cid);
res.json({ next: cid + 1 });
});
Resume on reconnect
On reconnect, ask the server for the next chunk id and continue from there, so a drop in the middle costs only the unconfirmed chunks, not the whole file.
The trade-off worth naming is that on reconnect, ask the server for the next chunk id and continue from there, so a drop in the middle costs only the unconfirmed chunks, not the whole file 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 a mid-upload drop
Kill the connection after half the chunks, reconnect, and assert the upload completes and the assembled file matches the original checksum, so resume is real.
The trade-off worth naming is that kill the connection after half the chunks, reconnect, and assert the upload completes and the assembled file matches the original checksum, so resume is real 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: tus protocol, S3 multipart.
FAQ
- What chunk size balances overhead and resumability?
- 4 to 8 MB is a common choice; small enough that a retry is cheap and a resume skips little, large enough that per-chunk HTTP overhead is negligible.
- How do I know a chunk arrived intact?
- Send a checksum with each chunk and have the server verify it before acknowledging, so a corrupt chunk is retransmitted instead of silently breaking the final file.
Report a problem with this articleOpens a pre-filled issue on GitHub.