Resumable Large-File Transfer Service with Verified Chunks
System DesignModel answerLast updated
Design a storage-backed service that moves very large files in resumable pieces, checks integrity end to end, serves byte ranges, and only exposes files once fully committed.
Problem
Your team needs a service for moving files that are far too large to send in a single HTTP request without failing. Users on flaky networks should be able to start a transfer, lose connectivity, and pick it up later, possibly from a different process or device.
Design the service end to end.
Requirements
- The file contents live in object storage. Metadata (ownership, status, hashes, versions) lives in a separate store.
- A client can drop off after any chunk and resume later from a different process.
- Downloads must support byte ranges.
- A download must never return data from a file that has not been committed.
- Re-sending a chunk, or re-sending the "finish" request, must be safe (idempotent).
- Multiple clients can be active at once, and access must be authorized.
- Abandoned, unfinished transfers must eventually be cleaned up.
What to cover
- The API surface for starting, continuing, finishing, and aborting an upload, plus the download API.
- How chunks are identified and addressed, and what happens on duplicates or out-of-order arrival.
- How integrity is verified for each chunk and for the whole file.
- How completion is made atomic, and how in-progress state is kept apart from committed state.
- Authorization for uploads and downloads.
- The range-download flow, including caching and load protection.
- Cleanup, monitoring, abuse controls, and failure recovery.
Questions worth raising with the interviewer
- What are the maximum file size, the allowed chunk size range, and the acceptable time to finalize an upload?
- Can chunks arrive in any order? Are committed versions immutable?
- Which hash algorithm is expected for chunks and for the full object?
Hints
Hint 1
Model an upload as a session with its own ID and a state machine (open, completing, committed, aborted, expired). Chunks are addressed by (session, index), so a retry overwrites or matches the same slot instead of creating a new one.
Hint 2
Keep two worlds apart: staging objects and session rows for in-progress data, and a committed file/version record that downloads read from. Completion should be a single conditional metadata write, so readers either see the whole file or nothing.
Hint 3
For cleanup and scale, think about expiry timestamps with a sweeper, storage lifecycle rules for incomplete multipart uploads, and having clients send bytes directly to object storage using short-lived signed URLs rather than proxying them.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Assumptions
I would confirm these first and state defaults: max file size around 5 TB, chunk size 8 MiB to 100 MiB (fixed per session), chunks may arrive in any order and in parallel, committed versions are immutable, and SHA-256 is used for both chunks and the whole object. Finalization should take seconds, not minutes, even for huge files.
Architecture
- API service (stateless): authenticates callers, manages sessions, issues signed URLs.
- Metadata store (e.g. a relational or strongly consistent KV database): sessions, chunk records, file and version records.
- Object storage: holds staging chunks and final objects. Clients upload bytes directly to it via short-lived presigned URLs, so the API tier never carries the bulk traffic.
- Background workers: expire sessions, reconcile orphaned objects, and run optional post-processing such as scanning.
Upload flow
- Create session:
POST /uploadswith file name, total size, chunk size, and the expected whole-file hash if known. The server returnsupload_id, the chunk count, and an expiry. A client-supplied idempotency key makes repeated create calls return the same session. - Get chunk URL:
PUT /uploads/{id}/chunks/{index}(or a request for a presigned URL). The client sends its chunk's SHA-256 so the server can bind it to a signed URL that requires that checksum. The object store rejects bytes that do not match. - Resume:
GET /uploads/{id}returns which chunk indexes are already stored (with sizes and hashes). The client uploads only the missing ones. This works from any process that holds valid credentials, because state lives on the server, not the client. - Complete:
POST /uploads/{id}/completewith the ordered list of chunk indexes and hashes. - Abort:
DELETE /uploads/{id}marks the session aborted and schedules the staging data for deletion.
Chunk identity and duplicates
A chunk is keyed by (upload_id, index), and its content is verified by hash. Staging path: staging/{upload_id}/{index}. Re-uploading the same index with the same hash is a no-op success. Re-uploading with a different hash is either rejected (strict mode, my default, returns 409) or replaces the old one if the session is still open. Out-of-order arrival is fine because the index fully determines position. Every chunk except the last must be exactly the chunk size, which is validated at completion.
Integrity
- Per-chunk: the checksum is verified by object storage on write, and recorded in metadata.
- Whole file: computing a hash over many TB on the server is slow. Options are (a) the client sends a final hash and the server verifies it asynchronously before marking the file available, or (b) use a hash-of-hashes (Merkle-style) over the chunk hashes, which can be verified instantly at completion. I would use the hash-of-hashes for the commit gate, and offer an optional full-content verification in a background worker for clients that need it.
Atomic, idempotent completion
Completion does the following:
- Load the session. If it is already
committedwith the same manifest, return the existing result (idempotent retry). - Check that all indexes are present, hashes match the manifest, and sizes add up.
- Assemble the object. With object-storage multipart semantics, the final "complete multipart" call stitches parts without copying bytes. Otherwise, store a manifest object listing the chunk objects, and serve downloads by reading through the manifest.
- In a single transaction (or conditional write), flip the session from
opentocommittedand insert the file version row pointing to the final object. The conditionstatus = openguarantees only one completer wins; concurrent retries see the committed result.
Because readers only look at the committed file/version table, a half-finished upload is invisible. If the process crashes between assembling the object and the metadata write, the object is unreferenced and the retry or the sweeper handles it. The metadata write is the commit point.
Download flow
GET /files/{id}withRange: bytes=a-b. The API checks authorization, resolves the latest committed version, and either streams from object storage or redirects to a short-lived signed URL for that object.- Only committed versions are resolvable, so uncommitted data is unreachable by construction.
- Return
206 Partial ContentwithContent-Range,ETag(the version or content hash), andAccept-Ranges: bytes. SupportIf-Rangeso a resumed download fails over cleanly if the file changed. Since versions are immutable, ranges are safely cacheable by a CDN keyed on version. - Backpressure: per-user and per-file concurrency limits, bandwidth shaping, and a cap on the number of ranges per request.
Authorization
- Sessions are bound to the creating principal and to a target path or namespace. Every API call checks that the caller owns or may write to the session.
- Signed URLs are scoped to one object key, one method, and a short TTL (minutes), and carry the required checksum.
- Downloads check read permission at request time, and signed download URLs are short-lived so revocation takes effect quickly.
Cleanup and failure recovery
- Sessions have an
expires_at, refreshed on activity (e.g. 7 days idle). A sweeper marks expired sessions, deletes staging objects, and removes chunk rows. - Add storage lifecycle rules to abort incomplete multipart uploads after N days as a safety net.
- A reconciliation job lists staging prefixes without a live session and deletes them.
- Per-user quotas on open sessions and total staged bytes limit abuse and cost.
Observability
Track session creation, completion rate, time to complete, bytes staged versus committed, hash-mismatch rate, retry rates, orphan bytes, and download error and 206 ratios. Alert on growing orphan storage and falling completion rates.
Trade-offs and common mistakes
- Proxying all bytes through the API limits scalability. Direct-to-storage with signed URLs is better, at the cost of more complex auth.
- Small chunks mean more requests and metadata; large chunks make retries costly. Pick based on network quality, and let the server dictate bounds.
- Making completion non-idempotent, or writing the final record before verifying chunks, yields duplicates or partially visible files.
- Trusting client-reported hashes without verifying them in storage or at commit time.
- Forgetting cleanup, which silently grows storage costs.
- Serving by path rather than by immutable version, which breaks range consistency when a file is replaced mid-download.