Making large uploads resumable with TurboPush

TurboPush gets multi-gigabyte files from the browser to a PHP server in chunks that survive dropped connections and page reloads.

A row of twelve chunks: the first eight are marked as already held by the server, the last four are highlighted as the ones sent now, with the client asking the server for its status. A row of twelve chunks: the first eight are marked as already held by the server, the last four are highlighted as the ones sent now, with the client asking the server for its status.

Send a multi-gigabyte file as one POST, lose the connection at 90%, and the user starts again from nothing. PHP’s upload_max_filesize and post_max_size get in the way long before that. My TurboPush breaks big files into parallel chunks with retries, so an upload carries on after a dropped connection or a page reload: a TypeScript client sends the chunks, and a PHP endpoint puts them back together.

Chunks

The client slices each File with Blob.slice() into chunks, 1MB by default, and posts each one as an ordinary multipart request. Every chunk carries enough to place it: the upload ID, file name, file size, total number of chunks and its own index.

POST /upload.php
fileId=4f2c...  fileName=video.mp4  fileSize=2147483648
totalChunks=2048  chunkIndex=17  file=<1MB of bytes>

Up to three chunk requests are in flight at once, and that limit applies across all files, not to each file. When every chunk has arrived, the client sends a JSON finalize action, and the server assembles the file.

Backing off between retries

I treat network errors, timeouts and HTTP 408, 429 and 5xx responses as temporary and retry the chunk. Other 4xx responses, such as a file type the server refuses, fail the file at once, because retrying won’t help.

The delay doubles with each attempt, up to a ceiling, with a random part so that many clients retrying after the same outage don’t all come back at the same moment:

private retryDelayFor(attempt: number): number {
    const base = Math.min(this.config.maxRetryDelay, this.config.retryDelay * Math.pow(2, attempt - 1));
    return Math.round(base / 2 + Math.random() * (base / 2));
}

Pausing aborts the chunks in flight. They’re sent again on resume, and they don’t count as retries.

Surviving a reload

With resumable: true, the client stores each upload’s ID in localStorage, under a key made from the endpoint and the file’s name, size and last-modified time:

private storageKey(file: File): string {
    const prefix = this.config.protocol === 'tus' ? 'turbopush:tus:' : 'turbopush:';
    return `${prefix}${this.config.endpoint}:${file.name}:${file.size}:${file.lastModified || 0}`;
}

When the same file is added again, after a reload or in another tab, the client finds the saved ID and sends a status action. The server replies with the chunks it already holds, and the client sends only the rest. If the server has nothing usable, for example because the chunk size has changed, it starts again. The saved ID is removed when the upload completes or is cancelled. Because IDs are random, one user can’t resume into another user’s upload.

A server that expects the worst

Chunks can arrive out of order, in parallel, twice, or from someone who isn’t playing fair. I made the PHP endpoint the strict half for that reason.

Each upload gets its own folder outside the web root, and each chunk or finalize request for that upload takes a lock on it with flock(). Inside the lock, a chunk is first written to a unique temporary name and then renamed into place. A retry or a duplicate request has no way to leave a half-written file behind:

$chunkPath = $fileTempDir . $this->chunkFileName($chunkIndex);
$partPath = $chunkPath . '.' . bin2hex(random_bytes(6)) . '.part';

// ...move the upload to $partPath, check its size...

$previousSize = is_file($chunkPath) ? $this->fileSize($chunkPath) : 0;
$receivedBytes = (int) $meta['receivedBytes'] - $previousSize + $chunkSize;
if ($receivedBytes > $fileSize) {
    throw new TurboPushException('Upload exceeds the declared file size', 400);
}
rename($partPath, $chunkPath);

The running total stops a client storing more than it declared. The first chunk’s metadata is recorded, and later chunks that disagree with it get a 409. Finalising holds the same lock, checks that every chunk is there and lists any missing ones in the error. Then it joins the chunks into a temporary file in the destination directory and renames it into place. Repeating a finalize after success returns the same answer, so a client that lost the response can safely ask again.

File names are cleaned, and the endpoint turns away any extension that a web server could run as code or a browser could run scripts from. It can check the MIME type against the file’s contents, and its errors never show a server path.

Speaking tus too

tus is the open protocol for resumable uploads, with mature servers in many languages, and from version 2.1 the client speaks it too. With protocol: 'tus' it creates uploads with POST, sends chunks with PATCH, and after any failure asks for the offset with HEAD and carries on from there, even in the middle of a chunk. If the server supports concatenation, a file’s chunks go up in parallel as partial uploads that are joined at the end.

Checked against a written protocol

The native protocol is written down in PROTOCOL.md, and a conformance suite of 74 checks tests any server against it, as Writing down an upload protocol describes. The PHP tests include concurrency tests that start real parallel PHP processes against the same upload. Interop tests run the client against tusd and tus-php in Docker. tus-php announces concatenation but doesn’t follow the spec for the final request. It runs with tus: { parallel: false }, and a test checks that the client fails cleanly if it tries concatenation there.

To watch the retries happen, the demo can simulate failing chunks.

More on TurboPush

All writing about TurboPush

Featured projects

All projects

More writing

All writing