Resumable Parallel Fetcher for Versioned ML Model Files
System DesignModel answerLast updated
Design a fetcher that pulls a multi-file model in parallel byte ranges, survives failures without redoing verified work, and exposes the model only when complete and consistent.
You are asked to design a component that retrieves a large machine-learning model from a remote store onto local disk, so that a serving process can load it.
A published model version has an immutable manifest. The manifest lists each file in the model, its expected length, and an integrity digest. The remote store may support byte-range requests. Both the network and the fetcher process can fail at any time.
Requirements
- Fetch the files in parallel, using file-level concurrency and byte-range pieces where the server allows it.
- After an interruption, continue from verified work rather than starting over, but only if that work still belongs to the same artifact version.
- Never let the consumer see a partial, corrupted, or mixed-version model.
- Keep network, disk, and memory use bounded.
No artifact size, concurrency limit, bandwidth target, or distribution topology is given. Explain how those inputs would shape your design.
Part 1: Plan and transfer. Describe the manifest handling, how you split work into pieces, how you control concurrency, and how you validate each transfer. Explain how you prevent ranges from different remote object versions from being stitched together.
Part 2: Resume and publish. Describe how progress is stored durably, what happens on restart, how stale or partially written state is handled, and how a finished download becomes visible to the consumer.
State any clarifying questions you would ask and the assumptions you make.
Hints
Hint 1
Give every download a stable identity (model name, version, manifest digest) and make every request and every progress record carry that identity plus the remote object's version token (for example an ETag or generation number).
Hint 2
Treat a piece as done only after its bytes are written, flushed, and checked against a digest or the expected length; a progress record that is written before the data is durable can lie after a crash.
Hint 3
Download into a staging directory that the consumer never reads, validate the complete set of files there, and then make it visible with a single atomic rename or pointer swap.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Clarifying questions and assumptions
- Is the source immutable per version, and does it return a version token (ETag, generation) and honor conditional range requests (
If-Match,If-Range)? - Are digests available per file only, or also per chunk? This decides how fine-grained verification and resume can be.
- Can several processes or hosts share one cache? Who owns an in-progress download?
- Does the consumer need the whole model version to appear at once, or can it read files independently? I assume all-or-nothing, since a model with mixed shards is usually unusable or silently wrong.
I assume immutable manifests, range support with a version token, a per-file digest, and a single local cache directory that several processes may touch.
Part 1: Plan and download pieces
Identity. The unit of work is keyed by (model, version, manifest_digest). Fetch the manifest first and verify it (signature or pinned digest). Everything else derives from it, so nothing is trusted from the network beyond what the manifest allows.
Piece plan. For each file with length L, create pieces of fixed size P with explicit ranges [i*P, min((i+1)*P, L) - 1]. Choose P to balance request overhead against retry cost and parallelism: typically 8-64 MiB for multi-GB shards. Very small files are a single piece. If the server does not support ranges, fall back to one whole-file stream per file, and parallelize only across files; resume then restarts at file granularity.
Version binding. On the first request for a file, record its version token. Every later range request sends If-Match: <token> (or If-Range). A mismatch returns 412 or a full 200 instead of 206, which we treat as "remote object changed" and fail the piece, never write it. Also check the Content-Range header equals what we asked for and the total length equals the manifest length.
Concurrency control. Use a bounded work queue with:
- a global cap on in-flight requests (for example 8-32), plus a per-host cap to avoid throttling;
- a memory budget: each worker streams to disk in small buffers (1-4 MiB), so memory is roughly
workers x buffer, notworkers x piece; - disk preallocation (
fallocate) so we fail early on lack of space, and positional writes (pwrite) so pieces can land in any order; - optional token-bucket bandwidth limiting so the fetch does not starve serving traffic.
Order work so that large files start first (reducing the tail), and use retries with exponential backoff and jitter; honor Retry-After on 429/503. Treat 5xx, timeouts, and short reads as retryable; treat 404 or digest mismatch on a fresh piece as suspicious and bounded in retries.
Per-transfer validation. For each piece check: status is 206 (or 200 for whole-file mode), the returned range and length match, bytes received equal expected bytes, and the version token matches. If chunk digests exist, verify the piece immediately. If only a file digest exists, hash incrementally as pieces complete, or verify at the end of the file.
Part 2: Resume and publish safely
Staging layout.
cache/<model>/<version>/
.staging-<manifest_digest>/ # partial data + state
files/<name>.part
state.json (or small SQLite/WAL)
current -> versions/<version> # what the consumer reads
The consumer reads only the published path, never staging.
Durable progress. Keep a per-file bitmap or list of verified pieces, along with the manifest digest and remote version token. Ordering matters: write data, fsync the file (or batch with periodic fsync), then append the piece record. A record then implies durable data. Use an append-only log or SQLite in WAL mode so a crash mid-update cannot corrupt state; with a rewrite-style state file, write to a temp file, fsync, and rename.
Restart.
- Acquire a lock (
flockor lease file) so two processes do not own the same staging directory; a second process either waits or joins as a reader of progress. - Load state. If the manifest digest or remote version token differs from what is recorded, discard that state for the affected files. Never trust progress across versions.
- For pieces marked verified, optionally spot-check (or fully re-hash when cheap) in case the disk or file was altered. For unmarked pieces, re-fetch, even if bytes exist on disk, since the tail of a partially written piece is unreliable.
- Truncate or overwrite as needed; the file length is preallocated, so presence of bytes proves nothing.
Final validation. When all pieces are recorded, verify each file's length and full digest (hashing can be pipelined with download to avoid a second full read). Optionally run a cheap load check, such as parsing shard headers or checking that every tensor named in the index exists.
Atomic publication. Rename each .part into its final name inside the staging directory, fsync the directory, then publish the whole version with one atomic operation: rename of the staging directory to versions/<version>, followed by an atomic symlink swap of current (create a temp symlink, then rename over). If the consumer only needs independent files, per-file rename after verification is enough, but the manifest-level atomic swap is safer for sharded models. Readers holding the previous version keep working, and old versions are garbage-collected later with a reference check.
Failure cases.
- Crash after data write, before record: piece is re-fetched, harmless.
- Crash after record, before fsync: avoided by the ordering above.
- Remote object replaced mid-download: conditional requests fail, we restart affected files against the new token, or abort if the manifest itself should be immutable.
- Digest mismatch at the end: delete the file's state, refetch, and alert if it repeats (possible corruption at source).
- Disk full: caught at preallocation; clean up staging and report.
How inputs shape the design
- Size: a few hundred MB makes per-file parallelism enough; tens or hundreds of GB needs range pieces, chunk digests, and incremental hashing.
- Concurrency/bandwidth: set workers so
workers x per-connection throughputapproaches the link or server limit, and stop adding when throughput plateaus or 429s begin. - Topology: for many hosts, add a node-local shared cache or peer-to-peer/regional mirror so each model crosses the WAN once, with the same manifest checks at every hop.
Common mistakes
- Marking pieces done before data is durable.
- Resuming with stale progress after the remote object changed.
- Accepting 200 where 206 was expected and writing the full body at an offset.
- Exposing files as they finish, so consumers see mixed versions.
- Unbounded buffering of whole pieces in memory.
- No lock, so two processes corrupt the same staging files.