Download manager design

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2024-11-30

Problem Statement

Design the Low-Level Design (LLD) of a Download Manager System, similar to tools like Internet Download Manager (IDM) or Chrome’s Download Service.


The system should allow users to download files from URLs, pause/resume downloads, handle multiple concurrent downloads, and support retry logic and progress tracking.


Your focus should be on object-oriented design, thread management, state tracking, and scalability.


🎯 Core Requirements


1. Download Management


•The system should allow:
•Starting a new download (given a URL and destination path).
•Pausing an ongoing download.
•Resuming a paused download.
•Cancelling a download.


2. Parallel Downloads


•Support downloading multiple files simultaneously.
•Limit the number of concurrent downloads (configurable).


3. Multi-Threaded Chunk Download (Optional Extension)


•A single file download can be split into multiple chunks for parallel downloading.
•Chunks can be merged once all are complete.


4. Download Progress & Status


•Track and display progress (% complete) for each download.
•Maintain download status — e.g.:
•QUEUED, IN_PROGRESS, PAUSED, COMPLETED, FAILED, CANCELLED.


5. Retry Mechanism


•Automatically retry failed downloads up to a defined retry limit.
•Implement exponential backoff or delay between retries.


6. Persistence (Optional)


•Save download metadata (e.g., URL, progress, state) to disk.
•On restart, the system should resume incomplete downloads.


⚙️ Key Operations (APIs)


MethodDescription


startDownload(String url, String destinationPath)


Begins a new file download.


pauseDownload(String downloadId)


Pauses the ongoing download.


resumeDownload(String downloadId)


Resumes a paused download.


cancelDownload(String downloadId)


Cancels a download.


getStatus(String downloadId)


Returns current status and progress.


listAllDownloads()


Returns all active and completed downloads.


🧱 Suggested Class Design


ClassResponsibility


DownloadManager


Orchestrates downloads, manages queue, and concurrency limits.


DownloadTask


Represents an individual download job, manages state and progress.


DownloadWorker


Thread responsible for downloading a chunk or file.


DownloadState (Enum)


Represents states: QUEUED, IN_PROGRESS, PAUSED, COMPLETED, etc.


ProgressTracker


Monitors and reports progress updates.


RetryHandler


Handles retry logic for failed downloads.


FileMerger (Optional)


Combines downloaded chunks into final file.


PersistenceManager (Optional)


Stores metadata for resuming downloads after restart.


🔄 Example Flow


Example 1: Basic Download


1.User starts a download:
2.downloadManager.startDownload("https://example.com/file.mp4", "C:/Downloads/file.mp4")
3.The manager creates a DownloadTask, spawns worker threads, and starts downloading.
4.The system periodically reports progress:
5.Progress: 35% → 70% → 100%
6.Upon completion, the status updates to COMPLETED.


Example 2: Pause and Resume


1.User pauses download halfway:
2.downloadManager.pauseDownload("task123")
3.Download stops gracefully, state stored as PAUSED.
4.Later, user resumes:
5.downloadManager.resumeDownload("task123")
6.Download continues from where it left off.


🧠 Design Considerations


Thread Safety:

•Ensure multiple downloads can occur concurrently without race conditions.

Concurrency Control:

•Use a thread pool or executor service to manage parallel tasks efficiently.

Extensibility:

•Design should support adding new protocols (FTP, S3, etc.) or new features (speed limiter, scheduling).

Failure Recovery:

•Handle network interruptions and retries gracefully.

Resumable Downloads:

•Use HTTP range requests to resume from partial data.


🚀 Possible Extensions


•Speed Limiting – cap download speed per file or globally.
•Priority Queueing – prioritize specific downloads.
•Notification System – notify when a download completes or fails.
•Chunk-based Parallel Downloads – for faster large file downloads.
•UI Integration – provide progress bars for each download.
•Logging & Metrics – track performance, errors, and throughput.


🧩 Example Entities


DownloadTask

1 2id: "task123" 3url: "https://example.com/movie.mp4" 4destination: "C:/Downloads/movie.mp4" 5state: IN_PROGRESS 6progress: 58% 7retryCount: 1

DownloadManager

1 2maxConcurrentDownloads: 3 3activeDownloads: [task1, task2] 4queuedDownloads: [task3, task4]


✅ Goal:


Design a thread-safe, extensible, and reliable Download Manager that supports multiple concurrent downloads, progress tracking, and pause/resume capabilities with clean object-oriented principles.

Solution

Comments