Crash-Safe Parallel Downloader for Versioned Data Bundles
System DesignModel answerLast updated
Design a per-node client that pulls huge versioned file bundles over ranged HTTP, resumes after failures using verified chunks, and exposes only fully validated bundles.
A data platform team ships bundles: named, versioned collections of files such as [email protected]. A bundle totals 50 GB to 3 TB and holds anywhere from a handful to several thousand files, each 100 MB to 20 GB. Hundreds of compute nodes must copy a bundle to local NVMe before a job can start. Files are served by an HTTP object store or CDN that supports Range requests.
Design the node-side downloader, a daemon or library that:
- Exposes
fetch(bundle_id) -> local_path, which returns (or reports progress until it can return) a directory that jobs can read. - Downloads files concurrently and splits large files into byte ranges fetched in parallel.
- After a process crash, reboot, or dropped connection, reuses every chunk that was already verified instead of starting over.
- Never lets a job see a partially written file or a blend of two different bundle versions.
- Downloads a bundle only once per node, even if several jobs request it simultaneously.
Non-functional constraints
- Try to saturate a 25 Gbps link while keeping roughly 1 GB of buffer memory, a sane number of file descriptors, and the origin's rate limits in mind.
- Every byte must be checked against a checksum supplied by the publisher.
- Local disk overhead is bounded: do not hold two full copies of a bundle.
- Bundles are immutable once published, but the store may later hold a different object at the same URL after a republish. Bytes from two object versions must never be combined.
What to cover
- Part 1 - Planning and transfer: the manifest format, how it becomes a chunk plan, how concurrency is scheduled and bounded, and what you check on every HTTP response.
- Part 2 - Resume and publish: what progress state is persisted and where, how a restarted process decides which chunks to trust, and the precise steps that turn a staging area into a visible, complete bundle.
- Part 3 - Scale and trade-offs: what changes when 500 nodes fetch the same bundle at once, and how you would tune chunk size and parallelism.
Good clarifying questions: Are checksums per file, per chunk, or both? Does the store give a version identifier (ETag or generation) per object? Must the whole bundle appear at once, or may jobs stream individual files?
Hints
Hint 1
Make the manifest the root of identity: address it by its own digest, include per-chunk checksums and the object version, and key all local state by that digest rather than by the bundle name.
Hint 2
Think about ordering for durability: data must be on disk before the progress record that claims it. Then ask what single filesystem operation can make a whole directory appear atomically.
Hint 3
For many nodes, origin egress (hundreds of nodes times terabytes) is the bottleneck, not the node link. Since chunks are content-verified, any cache or peer can serve them safely.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Clarifying assumptions
I would assume the publisher provides per-chunk and per-file SHA-256 digests plus an object version (ETag or generation) in a manifest. If only whole-file digests exist, I fall back to verifying whole files and binding progress to the object version. I would also assume jobs need the entire bundle visible at once, and mention a per-file mode as a variant.
1. Manifest and identity
The publisher writes one immutable manifest per bundle and the manifest is addressed by its own digest:
bundle: [email protected]
manifest_sha256: 9f3c...
files:
- path: grids/2019.nc
size: 12884901888
sha256: <whole-file digest>
chunk_size: 67108864 # 64 MiB
chunk_sha256: [ ... ] # one per chunk
object: {url: ..., version: "gen-1717..."}
All local state is keyed by manifest_sha256, not by the human-readable name. A republish yields a new digest and therefore a new, separate staging area, so mixing versions is structurally impossible. Per-chunk digests make resume safe: a chunk is either proven good or discarded.
2. Chunk plan and scheduling
- Split each file into fixed-size chunks (32-128 MiB); small files are one chunk. The plan is a pure function of the manifest, so it is recomputed after a crash and never stored.
- Use a worker pool over a queue. Roughly 32-64 concurrent streams reach about 25 Gbps, spread over a few connections per host (HTTP/2 multiplexing helps).
- Bound memory by streaming each response directly to disk with
pwriteat the chunk's offset in a preallocated sparse file (fallocate). With a 4 MiB buffer per stream, 64 streams use about 256 MiB, well under 1 GB. - Be polite to the origin: a per-origin token bucket, exponential backoff with jitter on 429/503, honoring
Retry-After, and a circuit breaker when the origin is failing. - Schedule the largest files first so the tail is short. Near the end, speculatively re-request the slowest outstanding chunks and keep whichever copy finishes first.
Validate every response
- Status must be
206. A200means the range was ignored, so reject it. Content-Rangemust match the requested range andContent-Lengththe expected length.- The
ETag/generation must equal the manifest's. SendIf-Match(orIf-Range) so the server refuses to serve a different version rather than relying on the client to notice. - Hash while streaming and compare to
chunk_sha256before marking the chunk done. On mismatch, discard and retry, with a cap on attempts.
3. Durable progress and resume
Layout, on the same filesystem as the final location:
cache/staging/<manifest_sha256>/
manifest.json # verified against its own digest
data/<path> # preallocated sparse files
progress.log # append-only: "<file_idx> <chunk_idx> <sha256>\n"
lock # flock held by the active fetcher
Completing a chunk: write the bytes, fdatasync the data file, then append the record to progress.log and fsync it. This order guarantees the log never claims bytes that are not on disk. A torn final line is ignored on replay.
Restart: take the lock, re-verify the manifest digest, replay the log into a bitmap, and schedule the missing chunks. To avoid trusting the log blindly, re-hash chunks written shortly before the crash, or rely on the whole-file verification before publishing. If the manifest digest or object version differs from what staging recorded, delete the staging directory.
One download per node: callers take the same flock; the holder downloads while the others wait and watch the log. The kernel releases the lock if the holder dies, which is why flock beats a PID file.
4. Atomic publication
- When every chunk is logged, verify each whole-file digest (this also catches silent disk corruption), then
fsyncfiles and directories. - Write a
COMPLETEmarker containing the manifest digest. rename(staging/<digest>, releases/<digest>), a single atomic directory rename.- Swing the name link
releases/[email protected] -> <digest>by creating a temporary symlink and renaming it over the old one.
Jobs only open paths under releases/, so they see nothing or the entire bundle. If streaming is required, publish per file with one rename each into a partial view and document the weaker guarantee.
Garbage collection removes staging directories idle for N hours and old bundles with no active lease, keeping usage at about one copy per bundle.
5. Scale: 500 nodes at once
500 nodes x 3 TB is about 1.5 PB of origin egress, which is the real bottleneck. Options, simplest first:
- A regional caching proxy or CDN in front of the origin. Digests in the manifest mean any cache is trustworthy.
- Staggered starts, plus rack-local peer-to-peer sharing of chunks (BitTorrent-style with a tracker). Verification is unchanged because chunks are content-addressed.
- Pre-staging popular bundles on a shared high-throughput filesystem.
Trade-offs
- Chunk size: larger means fewer requests and a smaller log; smaller loses less work on failure and balances the tail better. Start at 64 MiB and keep per-request overhead under about 1% of transfer time.
- Parallelism: increase streams until the link saturates or the origin starts throttling; adapt with AIMD (additive increase, multiplicative decrease) on errors and 429s.
- Sparse preallocation vs one file per chunk: preallocation avoids a concatenation step and a second copy but requires
pwrite; per-chunk files are simpler but need extra disk and I/O to assemble. - fsync frequency: syncing per 64 MiB chunk is cheap relative to transfer; syncing every buffer is not.
Common mistakes
- Logging progress before the data is durable.
- Accepting a
200response to a ranged request. - Keying staging by bundle name, which risks mixing versions after a republish.
- Using a PID-file lock that survives crashes.
- Publishing by copying files rather than an atomic rename, or staging on a different filesystem so rename is not atomic.
- Ignoring origin egress when scaling to hundreds of nodes.