Crash-Safe Parallel Downloader for Versioned App Updates
System DesignModel answerLast updated
Design an update downloader that fetches a large versioned release in parallel byte ranges, resumes after crashes without redoing verified work, and publishes only a fully verified result.
A desktop application updater must install the next release of a large application. The release is described by a signed, immutable manifest, one per version. The manifest lists every file with its expected length and content digest. Design the downloader component that fetches the release and exposes it to the application only after every file has been fully and correctly installed.
Given
- A manifest never changes after it is published. A new release means a new version and a new manifest.
- The CDN supports byte-range requests and returns a version identifier (such as an ETag) for each object.
- The application must never load a partial or corrupted release, even if the updater crashes mid-download and is restarted.
- No concrete release size, user count or bandwidth target is given. State the assumptions you design against, and explain how you would use real numbers if you had them.
Questions to clarify before designing
- Is any single file big enough to need splitting into ranges, or is one worker per file enough?
- Are digests provided per file, per chunk, or only for the whole release?
- Could two updater instances on one machine share a disk cache? If so, who owns an in-progress chunk?
- Must the whole release become visible at once, or may files be swapped in individually?
Part 1: Planning and fetching chunks
Describe your chunk plan, how many workers run concurrently, and what a worker checks before accepting a response as valid for a chunk. Explain how you avoid silently combining bytes from two different underlying versions of the same remote file, for example if the CDN object is replaced mid-download.
Part 2: Resuming and publishing safely
Explain what is persisted so that a restart after a crash resumes instead of starting over. Explain how the updater moves from "all files downloaded" to "release is live" without ever exposing a half-written state to the application.
What a strong answer covers
- A stable identity for each chunk.
- Bounds on connections, open file handles and in-flight memory.
- Response validation: status, range, length and version identifier.
- Durable, version-bound records of verified chunks.
- Handling of chunk data on disk whose completion was never recorded.
- Final whole-release verification and an atomic switch-over.
Hints
Hint 1
Give every chunk a key made of file path, manifest version and byte range. This key drives scheduling, retries and the progress records.
Hint 2
Pin the CDN object's version identifier when you first read the manifest, send it as a precondition on every range request, and reject any response whose identifier differs.
Hint 3
Order matters on disk: write to a temporary location, hash, and only then record the chunk as verified. Anything on disk without a record is untrusted.
Hint 4
Do all work in a staging directory. Publish with one atomic operation, such as a directory rename or a pointer-file swap, after every file's full digest matches the manifest.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Assumptions
I design for a release of a few GB to tens of GB, with files from a few KB to several GB, on consumer connections that can drop at any time. Concrete numbers would set three things: chunk size, worker count and the cost of a failed chunk. I would size workers as roughly target bandwidth divided by per-connection throughput, usually 4 to 8 connections per host, and stop adding workers once throughput stops growing.
Clarifying answers I would assume
- Large files exist, so I split by range.
- The manifest has per-file digests. Per-chunk digests are a bonus that makes repair cheaper.
- One updater instance owns the cache directory, enforced by a lock file.
- Atomicity applies to the whole release, not file by file.
Part 1: Chunk plan and validation
Plan. Expand the manifest into a flat list of chunks keyed by (file_path, manifest_version, byte_start, byte_end). Files above a threshold (say 32 to 64 MB) are split into fixed-size ranges. Smaller files are a single chunk. The key is deterministic, so a restart rebuilds the same plan.
Concurrency bounds.
- A fixed worker pool pulls from a queue. The pool size is the cap on connections.
- Each worker streams to disk in small buffers (for example 256 KB to 1 MB) instead of holding a whole chunk. In-flight memory is then roughly
workers × buffer size. - Pre-allocate each target file and write by offset, or write one temp file per chunk. Either way, open file handles are bounded by the pool size.
- Apply a global bandwidth limiter if the updater must not saturate the user's link.
Validating a response. Each request pins the object version captured when the manifest was read, using If-Match or equivalent. A worker accepts the response only if all of these hold:
- Status is
206 Partial Content. A200is accepted only if the worker can clip it to the requested range. Content-Rangeexactly matches the requested range.Content-Lengthequals the expected chunk size.- The ETag equals the pinned one.
If the object is replaced mid-download, the ETag changes and the precondition fails with 412 or a mismatch. That chunk fails instead of being appended to bytes from another version. This is the main defense against mixing two versions of one file.
Retries. Use exponential backoff with jitter and a per-chunk attempt cap. On a version mismatch, re-read the current object metadata and compare it with the manifest's expected length and digest. If the CDN now serves something the manifest does not describe, it is a hard failure surfaced to the user (or a refetch of the manifest), not an endless retry loop. Treat 429 and 5xx as retryable, and 403 or 404 as likely fatal.
Part 2: Durable progress and safe publishing
Progress index. Keep a local SQLite database (WAL mode) or an append-only log with one row per chunk: file_path, manifest_version, etag, byte_range, status, chunk_digest. A row is written as verified only after the chunk's bytes are fully received, flushed (fsync) and hashed. The bytes are always made durable before the record that claims them.
Resume. On startup:
- Load the target manifest and the index.
- Trust a chunk only if its row is
verifiedand itsmanifest_versionandetagmatch the current plan. - Re-fetch everything else.
Rows from an older version are discarded, or kept only if a chunk is provably identical, which a digest comparison can show.
Orphaned data. A chunk file on disk with no verified row, which is the classic crash-during-write case, is untrusted. Because writes go to a temp path or a pre-allocated region and are recorded only after hashing, a half-written chunk can never look finished. For pre-allocated files, "file size is correct" must not be treated as "contents are complete". Only the index says what is done.
Whole-file verification. When every chunk of a file is verified, check the file's full digest against the manifest. If chunk digests are available, a bad chunk is re-fetched alone. With only file-level digests, one bad chunk forces re-hashing the file and a search for the culprit, for example by re-downloading its suspect ranges. That is simpler but costs more CPU and bandwidth on failure.
Publish.
- Everything is staged in a directory, such as
staging/<version>/, on the same filesystem as the live location. - After all files pass,
fsyncthe files and the directory. - Make one atomic switch, either renaming the staged directory into place or rewriting a small
currentpointer file with write-temp, fsync and rename. - The application reads only through the live path or pointer, so it sees the old complete release or the new complete one, never a mix.
Keep the previous release until the new one is confirmed to launch. This gives a cheap rollback. Clean up staging and stale index rows afterward.
Crash windows. Crashing before the switch means a resume. Crashing during the switch is safe, because a rename or pointer swap is atomic and the result is old or new. Crashing after the switch means only cleanup remains.
Trade-offs
- Chunk size. Small chunks give finer resume and cheaper retries but more requests and index writes. Large chunks are the opposite. 8 to 64 MB is a typical range.
- Index durability.
fsyncon every chunk is safest but costs I/O. Batching commits is fine, because losing a few records only means re-downloading those chunks. - Shared cache. If two instances may share a disk, use a lock file with PID and liveness checks, or per-chunk leases. The simplest correct choice is one owner per cache directory.
- Disk space. Staging plus the live release needs about 2x the release size. Check free space up front and fail early.
Common mistakes
- Trusting a file because it exists or has the right size.
- Recording a chunk as done before its bytes are flushed and hashed.
- Not pinning the ETag, which allows mixed-version files.
- Retrying forever when the manifest and the CDN genuinely disagree.
- Unbounded concurrency or buffering, which exhausts memory or file handles.
- Updating the live directory in place, so a crash exposes a half-installed release.
- Skipping the final whole-file digest check and relying only on per-chunk success.