SYSTEM DESIGN SKETCHBOOK / PRACTICE SHEET / NO. 43
Design a file downloader library
App developers on your platform keep writing their own download code for game assets, offline videos, and document bundles, and keep getting it wrong. You are designing a shared library they can call to download files reliably on phones and desktops, including on bad connections and when the app is killed mid-download.
Requirements
- Enqueue a download (URL, destination, optional expected checksum and headers) and get a handle to pause, resume, cancel and observe it.
- Resume interrupted downloads without starting over, including after the app process is killed and relaunched.
- Limit how many downloads run at once, overall and per host, and respect a "Wi-Fi only" option.
- Retry transient failures automatically and report permanent ones clearly.
- Verify file integrity before reporting success.
Constraints and assumptions
- Files range from 10 KB to 20 GB; an app may queue up to 500 downloads.
- Default concurrency: 4 downloads at once, at most 2 per host.
- Mobile networks drop or switch often; assume a 20 GB file over a 20 Mbps link takes more than 2 hours.
- A resume should re-download at most about 1 MB of data that was already received.
- Progress callbacks go to the app's UI thread at most 10 times per second per download.
Out of scope for the first sketch
Uploads · Peer-to-peer or CDN selection · DRM and decryption of downloaded content
Draw your design
Rough math and notes
SYSTEM DESIGN SKETCHBOOK / FOLLOW-UPS
Design a file downloader library
Finish your first sketch before answering these. Cross out freely.
- Sketch the public API. What does enqueue return, and how does a caller learn about progress, completion and failure?
- The app is killed at 60% of a 4 GB download. What happens on the next launch, and what if the file changed on the server in the meantime?
- How do you decide when to retry, how long to wait, and when to give up?
- Should one large file be downloaded as several parallel ranges? What does that cost you, including for the integrity check?
- The phone moves from Wi-Fi to cellular mid-download and the request says Wi-Fi only. What happens, step by step?
Compare afterwards: worked approaches and reading at sketchbook /questions/file-downloader-library/review